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