b54e3e62253f08f0f2bf8ad2376ab9afaa2863ea
[appc.git] / appc-dispatcher / appc-dispatcher-common / lock-manager-lib / lock-manager-impl / src / main / java / org / openecomp / appc / lockmanager / impl / sql / pessimistic / MySqlLockManager.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.openecomp.appc.lockmanager.impl.sql.pessimistic;
26
27 import java.sql.CallableStatement;
28 import java.sql.Connection;
29 import java.sql.ResultSet;
30 import java.sql.SQLException;
31
32 import org.openecomp.appc.lockmanager.api.LockRuntimeException;
33
34 public class MySqlLockManager extends SqlLockManager {
35
36         private static final int DEF_CRITICAL_SECTION_WAIT_TIMEOUT = 3;
37
38         protected int criticalSectionWaitTimeoutSecs = DEF_CRITICAL_SECTION_WAIT_TIMEOUT;
39
40         public void setCriticalSectionWaitTimeoutSecs(int criticalSectionWaitTimeoutSecs) {
41                 this.criticalSectionWaitTimeoutSecs = criticalSectionWaitTimeoutSecs;
42         }
43
44         @Override
45         protected void enterCriticalSection(Connection connection, String resource) {
46                 try {
47                         CallableStatement statement = connection.prepareCall("SELECT COALESCE(GET_LOCK(?,?),0)");
48                         try {
49                                 statement.setString(1, resource);
50                                 statement.setInt(2, criticalSectionWaitTimeoutSecs);
51                                 boolean execRes = statement.execute();
52                                 int result = 0;
53                                 if(execRes) {
54                                         ResultSet resultSet = statement.getResultSet();
55                                         try {
56                                                 if(resultSet.next()) {
57                                                         result = resultSet.getInt(1);
58                                                 }
59                                         } finally {
60                                                 resultSet.close();
61                                         }
62                                 }
63                                 if(result != 1) { // lock is not obtained
64                                         throw new LockRuntimeException("Cannot obtain critical section lock for resource [" + resource + "].");
65                                 }
66                         } finally {
67                                 statement.close();
68                         }
69                 } catch(SQLException e) {
70             throw new LockRuntimeException("Cannot obtain critical section lock for resource [" + resource + "].", e);
71                 }
72         }
73
74         @Override
75         protected void leaveCriticalSection(Connection connection, String resource) {
76                 try {
77                         CallableStatement statement = connection.prepareCall("SELECT RELEASE_LOCK(?)");
78                         try {
79                                 statement.setString(1, resource);
80                                 statement.execute();
81                         } finally {
82                                 statement.close();
83                         }
84                 } catch(SQLException e) {
85                         throw new LockRuntimeException("Error releasing critical section lock.", e);
86                 }
87         }
88 }