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