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.openecomp.appc.lockmanager.impl.sql.optimistic;
27 import java.sql.Connection;
28 import java.sql.SQLException;
29 import java.util.concurrent.ConcurrentHashMap;
30 import java.util.concurrent.ConcurrentMap;
32 import org.openecomp.appc.lockmanager.impl.sql.Synchronizer;
33 import org.openecomp.appc.lockmanager.impl.sql.SynchronizerReceiver;
34 import org.openecomp.appc.lockmanager.impl.sql.optimistic.LockRecord;
35 import org.openecomp.appc.lockmanager.impl.sql.optimistic.MySqlLockManager;
37 class MySqlLockManagerMock extends MySqlLockManager implements SynchronizerReceiver {
39 private final ConcurrentMap<String, LockRecord> locks = new ConcurrentHashMap<>();
40 private boolean useReal;
41 private Synchronizer synchronizer;
43 MySqlLockManagerMock(boolean useReal) {
44 this.useReal = useReal;
48 public void setSynchronizer(Synchronizer synchronizer) {
49 this.synchronizer = synchronizer;
53 protected Connection openDbConnection() {
55 return super.openDbConnection();
61 protected void closeDbConnection(Connection connection) {
63 super.closeDbConnection(connection);
68 protected LockRecord loadLockRecord(Connection connection, String resource) throws SQLException {
71 res = super.loadLockRecord(connection, resource);
73 res = locks.get(resource);
75 if(synchronizer != null) {
76 synchronizer.postLoadLockRecord(resource, (res == null) ? null : res.getOwner());
82 protected void addLockRecord(Connection connection, String resource, String owner, long timeout) throws SQLException {
83 if(synchronizer != null) {
84 synchronizer.preAddLockRecord(resource, owner);
88 super.addLockRecord(connection, resource, owner, timeout);
91 LockRecord lockRecord = new LockRecord(resource);
92 lockRecord.setOwner(owner);
93 lockRecord.setUpdated(System.currentTimeMillis());
94 lockRecord.setTimeout(timeout);
96 LockRecord prevLockRecord = locks.putIfAbsent(resource, lockRecord);
97 if(prevLockRecord != null) {
98 // simulate unique constraint violation
99 throw new SQLException("Duplicate PK exception", "23000", 1062);
102 if(synchronizer != null) {
103 synchronizer.postAddLockRecord(resource, owner);
109 protected boolean updateLockRecord(Connection connection, String resource, String owner, long timeout, long ver) throws SQLException {
110 if(synchronizer != null) {
111 synchronizer.preUpdateLockRecord(resource, owner);
115 return super.updateLockRecord(connection, resource, owner, timeout, ver);
117 LockRecord lockRecord = loadLockRecord(connection, resource);
118 synchronized(lockRecord) {
119 // should be atomic operation
120 if(ver != lockRecord.getVer()) {
123 lockRecord.setOwner(owner);
124 lockRecord.setUpdated(System.currentTimeMillis());
125 lockRecord.setTimeout(timeout);
126 lockRecord.setVer(ver + 1);
130 if(synchronizer != null) {
131 synchronizer.postUpdateLockRecord(resource, owner);