summaryrefslogtreecommitdiffstats
path: root/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/DistributedLockingFeature.java
diff options
context:
space:
mode:
Diffstat (limited to 'feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/DistributedLockingFeature.java')
-rw-r--r--feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/DistributedLockingFeature.java54
1 files changed, 42 insertions, 12 deletions
diff --git a/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/DistributedLockingFeature.java b/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/DistributedLockingFeature.java
index 019452a0..b30fca76 100644
--- a/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/DistributedLockingFeature.java
+++ b/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/DistributedLockingFeature.java
@@ -20,15 +20,16 @@
package org.onap.policy.distributed.locking;
import java.sql.Connection;
-import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
+import java.util.Properties;
import java.util.UUID;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
-
+import org.apache.commons.dbcp2.BasicDataSource;
+import org.apache.commons.dbcp2.BasicDataSourceFactory;
import org.onap.policy.common.utils.properties.exception.PropertyException;
import org.onap.policy.drools.core.lock.LockRequestFuture;
import org.onap.policy.drools.core.lock.PolicyResourceLockFeatureAPI;
@@ -61,6 +62,11 @@ public class DistributedLockingFeature implements PolicyEngineFeatureAPI, Policy
private ScheduledExecutorService scheduledExecutorService;
/**
+ * Data source used to connect to the DB containing locks.
+ */
+ private BasicDataSource dataSource;
+
+ /**
* UUID
*/
private static final UUID uuid = UUID.randomUUID();
@@ -79,7 +85,7 @@ public class DistributedLockingFeature implements PolicyEngineFeatureAPI, Policy
@Override
public Future<Boolean> beforeLock(String resourceId, String owner, Callback callback) {
- TargetLock tLock = new TargetLock(resourceId, uuid, owner, lockProps);
+ TargetLock tLock = new TargetLock(resourceId, uuid, owner, lockProps, dataSource);
return new LockRequestFuture(resourceId, owner, tLock.lock());
@@ -87,21 +93,21 @@ public class DistributedLockingFeature implements PolicyEngineFeatureAPI, Policy
@Override
public OperResult beforeUnlock(String resourceId, String owner) {
- TargetLock tLock = new TargetLock(resourceId, uuid, owner, lockProps);
+ TargetLock tLock = new TargetLock(resourceId, uuid, owner, lockProps, dataSource);
return(tLock.unlock() ? OperResult.OPER_ACCEPTED : OperResult.OPER_DENIED);
}
@Override
public OperResult beforeIsLockedBy(String resourceId, String owner) {
- TargetLock tLock = new TargetLock(resourceId, uuid, owner, lockProps);
+ TargetLock tLock = new TargetLock(resourceId, uuid, owner, lockProps, dataSource);
return(tLock.isActive() ? OperResult.OPER_ACCEPTED : OperResult.OPER_DENIED);
}
@Override
public OperResult beforeIsLocked(String resourceId) {
- TargetLock tLock = new TargetLock(resourceId, uuid, "dummyOwner", lockProps);
+ TargetLock tLock = new TargetLock(resourceId, uuid, "dummyOwner", lockProps, dataSource);
return(tLock.isLocked() ? OperResult.OPER_ACCEPTED : OperResult.OPER_DENIED);
}
@@ -111,15 +117,23 @@ public class DistributedLockingFeature implements PolicyEngineFeatureAPI, Policy
try {
this.lockProps = new DistributedLockingProperties(SystemPersistence.manager.getProperties(DistributedLockingFeature.CONFIGURATION_PROPERTIES_NAME));
+ this.dataSource = makeDataSource();
} catch (PropertyException e) {
logger.error("DistributedLockingFeature feature properies have not been loaded", e);
throw new DistributedLockingFeatureException(e);
+ } catch(InterruptedException e) {
+ logger.error("DistributedLockingFeature failed to create data source", e);
+ Thread.currentThread().interrupt();
+ throw new DistributedLockingFeatureException(e);
+ } catch(Exception e) {
+ logger.error("DistributedLockingFeature failed to create data source", e);
+ throw new DistributedLockingFeatureException(e);
}
long heartbeatInterval = this.lockProps.getHeartBeatIntervalProperty();
cleanLockTable();
- initHeartbeat(lockProps);
+ initHeartbeat();
this.scheduledExecutorService = Executors.newScheduledThreadPool(1);
this.scheduledExecutorService.scheduleAtFixedRate(heartbeat, heartbeatInterval, heartbeatInterval, TimeUnit.MILLISECONDS);
@@ -127,6 +141,24 @@ public class DistributedLockingFeature implements PolicyEngineFeatureAPI, Policy
}
/**
+ * @return a new, pooled data source
+ * @throws Exception
+ */
+ private BasicDataSource makeDataSource() throws Exception {
+ Properties props = new Properties();
+ props.put("driverClassName", lockProps.getDbDriver());
+ props.put("url", lockProps.getDbUrl());
+ props.put("username", lockProps.getDbUser());
+ props.put("password", lockProps.getDbPwd());
+ props.put("testOnBorrow", "true");
+ props.put("poolPreparedStatements", "true");
+
+ // additional properties are listed in the GenericObjectPool API
+
+ return BasicDataSourceFactory.createDataSource(props);
+ }
+
+ /**
* This method kills the heartbeat thread and calls refreshLockTable which removes
* any records from the db where the current host is the owner.
*/
@@ -142,9 +174,7 @@ public class DistributedLockingFeature implements PolicyEngineFeatureAPI, Policy
*/
private void cleanLockTable() {
- try (Connection conn = DriverManager.getConnection(lockProps.getDbUrl(),
- lockProps.getDbUser(),
- lockProps.getDbPwd());
+ try (Connection conn = dataSource.getConnection();
PreparedStatement statement = conn.prepareStatement("DELETE FROM pooling.locks WHERE host = ? OR expirationTime < ?");
){
@@ -161,8 +191,8 @@ public class DistributedLockingFeature implements PolicyEngineFeatureAPI, Policy
/**
* Initialize the static heartbeat object
*/
- private static void initHeartbeat(DistributedLockingProperties lockProps) {
- heartbeat = new Heartbeat(uuid, lockProps);
+ private void initHeartbeat() {
+ heartbeat = new Heartbeat(uuid, lockProps, dataSource);
}