001// -------------------------------------------------------------------------------- 002// Copyright 2002-2026 Echo Three, LLC 003// 004// Licensed under the Apache License, Version 2.0 (the "License"); 005// you may not use this file except in compliance with the License. 006// You may obtain a copy of the License at 007// 008// http://www.apache.org/licenses/LICENSE-2.0 009// 010// Unless required by applicable law or agreed to in writing, software 011// distributed under the License is distributed on an "AS IS" BASIS, 012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013// See the License for the specific language governing permissions and 014// limitations under the License. 015// -------------------------------------------------------------------------------- 016 017package com.echothree.util.server.persistence.mutex; 018 019import com.echothree.util.server.persistence.Session; 020import javax.enterprise.context.ApplicationScoped; 021import javax.inject.Inject; 022import org.jooq.impl.DSL; 023 024@ApplicationScoped 025@DatabaseMutexFlavor(DatabaseMutexFlavor.Flavor.MYSQL) 026public class DatabaseMySqlMutex 027 implements DatabaseMutex { 028 029 @Inject 030 Session session; 031 032 @Override 033 public boolean acquireLock(final String lockName, final int timeoutSeconds) { 034 var result = session.getDslContext() 035 .select(DSL.function("GET_LOCK", Integer.class, DSL.val(lockName), DSL.val(timeoutSeconds))) 036 .fetchOne(0, Integer.class); 037 038 return Integer.valueOf(1).equals(result); 039 } 040 041 @Override 042 public boolean releaseLock(final String lockName) { 043 var result = session.getDslContext() 044 .select(DSL.function("RELEASE_LOCK", Integer.class, DSL.val(lockName))) 045 .fetchOne(0, Integer.class); 046 047 return Integer.valueOf(1).equals(result); 048 } 049 050}