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.onap.appc.lockmanager.impl.sql.pessimistic;
27 import java.sql.Connection;
28 import java.sql.PreparedStatement;
29 import java.sql.ResultSet;
30 import java.sql.SQLException;
32 import org.onap.appc.lockmanager.api.LockException;
33 import org.onap.appc.lockmanager.api.LockRuntimeException;
34 import org.onap.appc.lockmanager.impl.sql.JdbcLockManager;
35 import org.onap.appc.lockmanager.impl.sql.Messages;
37 abstract class SqlLockManager extends JdbcLockManager {
39 private static final String SQL_LOAD_LOCK_RECORD = "SELECT * FROM %s WHERE RESOURCE_ID=?";
40 private static final String SQL_INSERT_LOCK_RECORD = "INSERT INTO %s (RESOURCE_ID, OWNER_ID, UPDATED, TIMEOUT) VALUES (?, ?, ?, ?)";
41 private static final String SQL_UPDATE_LOCK_RECORD = "UPDATE %s SET OWNER_ID=?, UPDATED=?, TIMEOUT=? WHERE RESOURCE_ID=?";
42 private static final String SQL_CURRENT_TIMESTAMP = "SELECT CURRENT_TIMESTAMP()";
44 private String sqlLoadLockRecord;
45 private String sqlInsertLockRecord;
46 private String sqlUpdateLockRecord;
49 public boolean acquireLock(String resource, String owner) throws LockException {
50 return acquireLock(resource, owner, 0);
54 public boolean acquireLock(String resource, String owner, long timeout) throws LockException {
56 throw new LockRuntimeException(Messages.ERR_NULL_LOCK_OWNER.format(resource));
59 Connection connection = openDbConnection();
61 enterCriticalSection(connection, resource);
63 res = lockResource(connection, resource, owner, timeout);
65 leaveCriticalSection(connection, resource);
68 closeDbConnection(connection);
74 public void releaseLock(String resource, String owner) throws LockException {
75 Connection connection = openDbConnection();
77 enterCriticalSection(connection, resource);
79 unlockResource(connection, resource, owner);
81 leaveCriticalSection(connection, resource);
84 closeDbConnection(connection);
89 public boolean isLocked(String resource) {
90 Connection connection=openDbConnection();
92 LockRecord lockRecord=loadLockRecord(connection,resource);
96 if(lockRecord.getOwner()==null){
98 }else if(isLockExpired(lockRecord, connection)){
104 } catch (SQLException e) {
105 throw new LockRuntimeException(Messages.EXP_CHECK_LOCK.format(resource));
107 closeDbConnection(connection);
111 private boolean lockResource(Connection connection, String resource, String owner, long timeout) throws LockException {
114 LockRecord lockRecord = loadLockRecord(connection, resource);
115 if(lockRecord != null) {
116 // lock record already exists
117 String currentOwner = lockRecord.getOwner();
118 if(currentOwner != null) {
119 if(isLockExpired(lockRecord, connection)) {
121 } else if(!owner.equals(currentOwner)) {
122 throw new LockException(Messages.ERR_LOCK_LOCKED_BY_OTHER.format(resource, owner, currentOwner));
125 // set new owner on the resource lock record
126 updateLockRecord(connection, resource, owner, timeout);
127 if(currentOwner == null) {
128 // no one locked the resource before
132 // resource record does not exist in lock table => create new record
133 addLockRecord(connection, resource, owner, timeout);
137 } catch(SQLException e) {
138 throw new LockRuntimeException(Messages.EXP_LOCK.format(resource), e);
142 private void unlockResource(Connection connection, String resource, String owner) throws LockException {
144 LockRecord lockRecord = loadLockRecord(connection, resource);
145 if(lockRecord != null) {
147 if(isLockExpired(lockRecord, connection)) {
148 // lock is expired => no lock
152 if((lockRecord == null) || (lockRecord.getOwner() == null)) {
153 // resource is not locked
154 throw new LockException(Messages.ERR_UNLOCK_NOT_LOCKED.format(resource));
156 String currentOwner = lockRecord.getOwner();
157 if(!owner.equals(currentOwner)) {
158 throw new LockException(Messages.ERR_UNLOCK_LOCKED_BY_OTHER.format(resource, owner, currentOwner));
160 updateLockRecord(connection, resource, null, 0);
161 // TODO delete record from table on lock release?
162 // deleteLockRecord(connection, resource);
163 } catch(SQLException e) {
164 throw new LockRuntimeException(Messages.EXP_UNLOCK.format(resource), e);
168 protected abstract void enterCriticalSection(Connection connection, String resource);
170 protected abstract void leaveCriticalSection(Connection connection, String resource);
172 protected LockRecord loadLockRecord(Connection connection, String resource) throws SQLException {
173 LockRecord res = null;
174 if(sqlLoadLockRecord == null) {
175 sqlLoadLockRecord = String.format(SQL_LOAD_LOCK_RECORD, tableName);
177 try(PreparedStatement statement = connection.prepareStatement(sqlLoadLockRecord)) {
178 statement.setString(1, resource);
179 try(ResultSet resultSet = statement.executeQuery()) {
180 if(resultSet.next()) {
181 res = new LockRecord(resource);
182 res.setOwner(resultSet.getString(2));
183 res.setUpdated(resultSet.getLong(3));
184 res.setTimeout(resultSet.getLong(4));
191 protected void addLockRecord(Connection connection, String resource, String owner, long timeout) throws SQLException {
192 if(sqlInsertLockRecord == null) {
193 sqlInsertLockRecord = String.format(SQL_INSERT_LOCK_RECORD, tableName);
195 try(PreparedStatement statement = connection.prepareStatement(sqlInsertLockRecord)) {
196 statement.setString(1, resource);
197 statement.setString(2, owner);
198 statement.setLong(3, getCurrentTime(connection));
199 statement.setLong(4, timeout);
200 statement.executeUpdate();
204 protected void updateLockRecord(Connection connection, String resource, String owner, long timeout) throws SQLException {
205 if(sqlUpdateLockRecord == null) {
206 sqlUpdateLockRecord = String.format(SQL_UPDATE_LOCK_RECORD, tableName);
208 try(PreparedStatement statement = connection.prepareStatement(sqlUpdateLockRecord)) {
209 statement.setString(1, owner);
210 statement.setLong(2, getCurrentTime(connection));
211 statement.setLong(3, timeout);
212 statement.setString(4, resource);
213 statement.executeUpdate();
217 // protected void deleteLockRecord(Connection connection, String resource) throws SQLException {
218 // if(sqlDeleteLockRecord == null) {
219 // sqlDeleteLockRecord = String.format(SQL_DELETE_LOCK_RECORD, tableName);
221 // try(PreparedStatement statement = connection.prepareStatement(sqlDeleteLockRecord)) {
222 // statement.setString(1, resource);
223 // statement.executeUpdate();
227 private boolean isLockExpired(LockRecord lockRecord, Connection connection) throws SQLException {
228 long timeout = lockRecord.getTimeout();
232 long updated = lockRecord.getUpdated();
233 long now = getCurrentTime(connection);
234 long expiration = updated + timeout;
235 return (now > expiration);
238 private long getCurrentTime(Connection connection) throws SQLException {
240 if(connection != null) {
241 try(PreparedStatement statement = connection.prepareStatement(SQL_CURRENT_TIMESTAMP)) {
242 try(ResultSet resultSet = statement.executeQuery()) {
243 if(resultSet.next()) {
244 res = resultSet.getTimestamp(1).getTime();
250 res = System.currentTimeMillis();