2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.lockmanager.impl.sql.pessimistic;
26 import java.sql.CallableStatement;
27 import java.sql.Connection;
28 import java.sql.ResultSet;
29 import java.sql.SQLException;
31 import org.onap.appc.lockmanager.api.LockRuntimeException;
33 public class MySqlLockManager extends SqlLockManager {
35 private static final int DEF_CRITICAL_SECTION_WAIT_TIMEOUT = 3;
37 protected int criticalSectionWaitTimeoutSecs = DEF_CRITICAL_SECTION_WAIT_TIMEOUT;
39 public void setCriticalSectionWaitTimeoutSecs(int criticalSectionWaitTimeoutSecs) {
40 this.criticalSectionWaitTimeoutSecs = criticalSectionWaitTimeoutSecs;
44 protected void enterCriticalSection(Connection connection, String resource) {
46 CallableStatement statement = connection.prepareCall("SELECT COALESCE(GET_LOCK(?,?),0)");
48 statement.setString(1, resource);
49 statement.setInt(2, criticalSectionWaitTimeoutSecs);
50 boolean execRes = statement.execute();
53 ResultSet resultSet = statement.getResultSet();
55 if(resultSet.next()) {
56 result = resultSet.getInt(1);
62 if(result != 1) { // lock is not obtained
63 throw new LockRuntimeException("Cannot obtain critical section lock for resource [" + resource + "].");
68 } catch(SQLException e) {
69 throw new LockRuntimeException("Cannot obtain critical section lock for resource [" + resource + "].", e);
74 protected void leaveCriticalSection(Connection connection, String resource) {
76 CallableStatement statement = connection.prepareCall("SELECT RELEASE_LOCK(?)");
78 statement.setString(1, resource);
83 } catch(SQLException e) {
84 throw new LockRuntimeException("Error releasing critical section lock.", e);