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