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