2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2017 Amdocs
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=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.openecomp.appc.lockmanager.impl.sql.pessimistic;
25 import java.sql.CallableStatement;
26 import java.sql.Connection;
27 import java.sql.ResultSet;
28 import java.sql.SQLException;
30 import org.openecomp.appc.lockmanager.api.LockRuntimeException;
32 public class MySqlLockManager extends SqlLockManager {
34 private static final int DEF_CRITICAL_SECTION_WAIT_TIMEOUT = 3;
36 protected int criticalSectionWaitTimeoutSecs = DEF_CRITICAL_SECTION_WAIT_TIMEOUT;
38 public void setCriticalSectionWaitTimeoutSecs(int criticalSectionWaitTimeoutSecs) {
39 this.criticalSectionWaitTimeoutSecs = criticalSectionWaitTimeoutSecs;
43 protected void enterCriticalSection(Connection connection, String resource) {
45 CallableStatement statement = connection.prepareCall("SELECT COALESCE(GET_LOCK(?,?),0)");
47 statement.setString(1, resource);
48 statement.setInt(2, criticalSectionWaitTimeoutSecs);
49 boolean execRes = statement.execute();
52 ResultSet resultSet = statement.getResultSet();
54 if(resultSet.next()) {
55 result = resultSet.getInt(1);
61 if(result != 1) { // lock is not obtained
62 throw new LockRuntimeException("Cannot obtain critical section lock for resource [" + resource + "].");
67 } catch(SQLException e) {
68 throw new LockRuntimeException("Cannot obtain critical section lock for resource [" + resource + "].", e);
73 protected void leaveCriticalSection(Connection connection, String resource) {
75 CallableStatement statement = connection.prepareCall("SELECT RELEASE_LOCK(?)");
77 statement.setString(1, resource);
82 } catch(SQLException e) {
83 throw new LockRuntimeException("Error releasing critical section lock.", e);