2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.openecomp.appc.lockmanager.impl.sql.pessimistic;
27 import java.sql.CallableStatement;
28 import java.sql.Connection;
29 import java.sql.ResultSet;
30 import java.sql.SQLException;
32 import org.openecomp.appc.lockmanager.api.LockRuntimeException;
34 public class MySqlLockManager extends SqlLockManager {
36 private static final int DEF_CRITICAL_SECTION_WAIT_TIMEOUT = 3;
38 protected int criticalSectionWaitTimeoutSecs = DEF_CRITICAL_SECTION_WAIT_TIMEOUT;
40 public void setCriticalSectionWaitTimeoutSecs(int criticalSectionWaitTimeoutSecs) {
41 this.criticalSectionWaitTimeoutSecs = criticalSectionWaitTimeoutSecs;
45 protected void enterCriticalSection(Connection connection, String resource) {
47 CallableStatement statement = connection.prepareCall("SELECT COALESCE(GET_LOCK(?,?),0)");
49 statement.setString(1, resource);
50 statement.setInt(2, criticalSectionWaitTimeoutSecs);
51 boolean execRes = statement.execute();
54 ResultSet resultSet = statement.getResultSet();
56 if(resultSet.next()) {
57 result = resultSet.getInt(1);
63 if(result != 1) { // lock is not obtained
64 throw new LockRuntimeException("Cannot obtain critical section lock for resource [" + resource + "].");
69 } catch(SQLException e) {
70 throw new LockRuntimeException("Cannot obtain critical section lock for resource [" + resource + "].", e);
75 protected void leaveCriticalSection(Connection connection, String resource) {
77 CallableStatement statement = connection.prepareCall("SELECT RELEASE_LOCK(?)");
79 statement.setString(1, resource);
84 } catch(SQLException e) {
85 throw new LockRuntimeException("Error releasing critical section lock.", e);