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.optimistic;
25 import java.sql.Connection;
26 import java.sql.SQLException;
27 import java.util.concurrent.ConcurrentHashMap;
28 import java.util.concurrent.ConcurrentMap;
30 import org.openecomp.appc.lockmanager.impl.sql.Synchronizer;
31 import org.openecomp.appc.lockmanager.impl.sql.SynchronizerReceiver;
32 import org.openecomp.appc.lockmanager.impl.sql.optimistic.LockRecord;
33 import org.openecomp.appc.lockmanager.impl.sql.optimistic.MySqlLockManager;
35 class MySqlLockManagerMock extends MySqlLockManager implements SynchronizerReceiver {
37 private final ConcurrentMap<String, LockRecord> locks = new ConcurrentHashMap<>();
38 private boolean useReal;
39 private Synchronizer synchronizer;
41 MySqlLockManagerMock(boolean useReal) {
42 this.useReal = useReal;
46 public void setSynchronizer(Synchronizer synchronizer) {
47 this.synchronizer = synchronizer;
51 protected Connection openDbConnection() {
53 return super.openDbConnection();
59 protected void closeDbConnection(Connection connection) {
61 super.closeDbConnection(connection);
66 protected LockRecord loadLockRecord(Connection connection, String resource) throws SQLException {
69 res = super.loadLockRecord(connection, resource);
71 res = locks.get(resource);
73 if(synchronizer != null) {
74 synchronizer.postLoadLockRecord(resource, (res == null) ? null : res.getOwner());
80 protected void addLockRecord(Connection connection, String resource, String owner, long timeout) throws SQLException {
81 if(synchronizer != null) {
82 synchronizer.preAddLockRecord(resource, owner);
86 super.addLockRecord(connection, resource, owner, timeout);
89 LockRecord lockRecord = new LockRecord(resource);
90 lockRecord.setOwner(owner);
91 lockRecord.setUpdated(System.currentTimeMillis());
92 lockRecord.setTimeout(timeout);
94 LockRecord prevLockRecord = locks.putIfAbsent(resource, lockRecord);
95 if(prevLockRecord != null) {
96 // simulate unique constraint violation
97 throw new SQLException("Duplicate PK exception", "23000", 1062);
100 if(synchronizer != null) {
101 synchronizer.postAddLockRecord(resource, owner);
107 protected boolean updateLockRecord(Connection connection, String resource, String owner, long timeout, long ver) throws SQLException {
108 if(synchronizer != null) {
109 synchronizer.preUpdateLockRecord(resource, owner);
113 return super.updateLockRecord(connection, resource, owner, timeout, ver);
115 LockRecord lockRecord = loadLockRecord(connection, resource);
116 synchronized(lockRecord) {
117 // should be atomic operation
118 if(ver != lockRecord.getVer()) {
121 lockRecord.setOwner(owner);
122 lockRecord.setUpdated(System.currentTimeMillis());
123 lockRecord.setTimeout(timeout);
124 lockRecord.setVer(ver + 1);
128 if(synchronizer != null) {
129 synchronizer.postUpdateLockRecord(resource, owner);