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.Connection;
26 import java.sql.SQLException;
27 import java.util.HashMap;
29 import java.util.concurrent.TimeUnit;
30 import java.util.concurrent.locks.Lock;
31 import java.util.concurrent.locks.ReentrantLock;
33 import org.openecomp.appc.lockmanager.api.LockRuntimeException;
34 import org.openecomp.appc.lockmanager.impl.sql.Synchronizer;
35 import org.openecomp.appc.lockmanager.impl.sql.SynchronizerReceiver;
36 import org.openecomp.appc.lockmanager.impl.sql.pessimistic.LockRecord;
37 import org.openecomp.appc.lockmanager.impl.sql.pessimistic.MySqlLockManager;
39 class MySqlLockManagerMock extends MySqlLockManager implements SynchronizerReceiver {
41 private final Map<String, LockRecord> locks = new HashMap<>();
42 private final Lock lock = new ReentrantLock();
43 private boolean useReal;
44 private Synchronizer synchronizer;
46 MySqlLockManagerMock(boolean useReal) {
47 this.useReal = useReal;
51 public void setSynchronizer(Synchronizer synchronizer) {
52 this.synchronizer = synchronizer;
56 protected Connection openDbConnection() {
58 return super.openDbConnection();
64 protected void closeDbConnection(Connection connection) {
66 super.closeDbConnection(connection);
71 protected LockRecord loadLockRecord(Connection connection, String resource) throws SQLException {
74 res = super.loadLockRecord(connection, resource);
76 res = locks.get(resource);
78 if(synchronizer != null) {
79 synchronizer.postLoadLockRecord(resource, (res == null) ? null : res.getOwner());
85 protected void addLockRecord(Connection connection, String resource, String owner, long timeout) throws SQLException {
86 if(synchronizer != null) {
87 synchronizer.preAddLockRecord(resource, owner);
91 super.addLockRecord(connection, resource, owner, timeout);
94 LockRecord lockRecord = new LockRecord(resource);
95 lockRecord.setOwner(owner);
96 lockRecord.setUpdated(System.currentTimeMillis());
97 lockRecord.setTimeout(timeout);
98 locks.put(resource, lockRecord);
100 if(synchronizer != null) {
101 synchronizer.postAddLockRecord(resource, owner);
107 protected void updateLockRecord(Connection connection, String resource, String owner, long timeout) throws SQLException {
108 if(synchronizer != null) {
109 synchronizer.preUpdateLockRecord(resource, owner);
113 super.updateLockRecord(connection, resource, owner, timeout);
116 LockRecord lockRecord = loadLockRecord(connection, resource);
117 lockRecord.setOwner(owner);
118 lockRecord.setUpdated(System.currentTimeMillis());
119 lockRecord.setTimeout(timeout);
120 locks.put(resource, lockRecord);
122 if(synchronizer != null) {
123 synchronizer.postUpdateLockRecord(resource, owner);
129 protected void enterCriticalSection(Connection connection, String resource) {
131 super.enterCriticalSection(connection, resource);
135 if(!lock.tryLock(criticalSectionWaitTimeoutSecs, TimeUnit.SECONDS)) {
136 throw new LockRuntimeException("Cannot obtain critical section lock for resource [" + resource + "].");
138 } catch(InterruptedException e) {
139 throw new LockRuntimeException("Cannot obtain critical section lock.", e);
144 protected void leaveCriticalSection(Connection connection, String resource) {
146 super.leaveCriticalSection(connection, resource);