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.Connection;
25 import java.sql.SQLException;
26 import java.util.HashMap;
28 import java.util.concurrent.TimeUnit;
29 import java.util.concurrent.locks.Lock;
30 import java.util.concurrent.locks.ReentrantLock;
32 import org.openecomp.appc.lockmanager.api.LockRuntimeException;
33 import org.openecomp.appc.lockmanager.impl.sql.Synchronizer;
34 import org.openecomp.appc.lockmanager.impl.sql.SynchronizerReceiver;
35 import org.openecomp.appc.lockmanager.impl.sql.pessimistic.LockRecord;
36 import org.openecomp.appc.lockmanager.impl.sql.pessimistic.MySqlLockManager;
38 class MySqlLockManagerMock extends MySqlLockManager implements SynchronizerReceiver {
40 private final Map<String, LockRecord> locks = new HashMap<>();
41 private final Lock lock = new ReentrantLock();
42 private boolean useReal;
43 private Synchronizer synchronizer;
45 MySqlLockManagerMock(boolean useReal) {
46 this.useReal = useReal;
50 public void setSynchronizer(Synchronizer synchronizer) {
51 this.synchronizer = synchronizer;
55 protected Connection openDbConnection() {
57 return super.openDbConnection();
63 protected void closeDbConnection(Connection connection) {
65 super.closeDbConnection(connection);
70 protected LockRecord loadLockRecord(Connection connection, String resource) throws SQLException {
73 res = super.loadLockRecord(connection, resource);
75 res = locks.get(resource);
77 if(synchronizer != null) {
78 synchronizer.postLoadLockRecord(resource, (res == null) ? null : res.getOwner());
84 protected void addLockRecord(Connection connection, String resource, String owner, long timeout) throws SQLException {
85 if(synchronizer != null) {
86 synchronizer.preAddLockRecord(resource, owner);
90 super.addLockRecord(connection, resource, owner, timeout);
93 LockRecord lockRecord = new LockRecord(resource);
94 lockRecord.setOwner(owner);
95 lockRecord.setUpdated(System.currentTimeMillis());
96 lockRecord.setTimeout(timeout);
97 locks.put(resource, lockRecord);
99 if(synchronizer != null) {
100 synchronizer.postAddLockRecord(resource, owner);
106 protected void updateLockRecord(Connection connection, String resource, String owner, long timeout) throws SQLException {
107 if(synchronizer != null) {
108 synchronizer.preUpdateLockRecord(resource, owner);
112 super.updateLockRecord(connection, resource, owner, timeout);
115 LockRecord lockRecord = loadLockRecord(connection, resource);
116 lockRecord.setOwner(owner);
117 lockRecord.setUpdated(System.currentTimeMillis());
118 lockRecord.setTimeout(timeout);
119 locks.put(resource, lockRecord);
121 if(synchronizer != null) {
122 synchronizer.postUpdateLockRecord(resource, owner);
128 protected void enterCriticalSection(Connection connection, String resource) {
130 super.enterCriticalSection(connection, resource);
134 if(!lock.tryLock(criticalSectionWaitTimeoutSecs, TimeUnit.SECONDS)) {
135 throw new LockRuntimeException("Cannot obtain critical section lock for resource [" + resource + "].");
137 } catch(InterruptedException e) {
138 throw new LockRuntimeException("Cannot obtain critical section lock.", e);
143 protected void leaveCriticalSection(Connection connection, String resource) {
145 super.leaveCriticalSection(connection, resource);