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.optimistic;
24 import java.sql.Connection;
25 import java.sql.SQLException;
26 import java.util.concurrent.ConcurrentHashMap;
27 import java.util.concurrent.ConcurrentMap;
29 import org.openecomp.appc.lockmanager.impl.sql.Synchronizer;
30 import org.openecomp.appc.lockmanager.impl.sql.SynchronizerReceiver;
31 import org.openecomp.appc.lockmanager.impl.sql.optimistic.LockRecord;
32 import org.openecomp.appc.lockmanager.impl.sql.optimistic.MySqlLockManager;
34 class MySqlLockManagerMock extends MySqlLockManager implements SynchronizerReceiver {
36 private final ConcurrentMap<String, LockRecord> locks = new ConcurrentHashMap<>();
37 private boolean useReal;
38 private Synchronizer synchronizer;
40 MySqlLockManagerMock(boolean useReal) {
41 this.useReal = useReal;
45 public void setSynchronizer(Synchronizer synchronizer) {
46 this.synchronizer = synchronizer;
50 protected Connection openDbConnection() {
52 return super.openDbConnection();
58 protected void closeDbConnection(Connection connection) {
60 super.closeDbConnection(connection);
65 protected LockRecord loadLockRecord(Connection connection, String resource) throws SQLException {
68 res = super.loadLockRecord(connection, resource);
70 res = locks.get(resource);
72 if(synchronizer != null) {
73 synchronizer.postLoadLockRecord(resource, (res == null) ? null : res.getOwner());
79 protected void addLockRecord(Connection connection, String resource, String owner, long timeout) throws SQLException {
80 if(synchronizer != null) {
81 synchronizer.preAddLockRecord(resource, owner);
85 super.addLockRecord(connection, resource, owner, timeout);
88 LockRecord lockRecord = new LockRecord(resource);
89 lockRecord.setOwner(owner);
90 lockRecord.setUpdated(System.currentTimeMillis());
91 lockRecord.setTimeout(timeout);
93 LockRecord prevLockRecord = locks.putIfAbsent(resource, lockRecord);
94 if(prevLockRecord != null) {
95 // simulate unique constraint violation
96 throw new SQLException("Duplicate PK exception", "23000", 1062);
99 if(synchronizer != null) {
100 synchronizer.postAddLockRecord(resource, owner);
106 protected boolean updateLockRecord(Connection connection, String resource, String owner, long timeout, long ver) throws SQLException {
107 if(synchronizer != null) {
108 synchronizer.preUpdateLockRecord(resource, owner);
112 return super.updateLockRecord(connection, resource, owner, timeout, ver);
114 LockRecord lockRecord = loadLockRecord(connection, resource);
115 synchronized(lockRecord) {
116 // should be atomic operation
117 if(ver != lockRecord.getVer()) {
120 lockRecord.setOwner(owner);
121 lockRecord.setUpdated(System.currentTimeMillis());
122 lockRecord.setTimeout(timeout);
123 lockRecord.setVer(ver + 1);
127 if(synchronizer != null) {
128 synchronizer.postUpdateLockRecord(resource, owner);