Commit 4c917a75 authored by Miguel Martinez Pedreira's avatar Miguel Martinez Pedreira
Browse files

Improve singleton implementation of Cassandra interface

parent 3907c245
...@@ -19,12 +19,18 @@ import lazyj.ExtProperties; ...@@ -19,12 +19,18 @@ import lazyj.ExtProperties;
* *
*/ */
public class DBCassandra { public class DBCassandra {
private static DBCassandra dbc = new DBCassandra(); private static DBCassandra dbc = null;
private Session session = null; private static Session session = null;
private Cluster cluster = null; private static Cluster cluster = null;
private static transient final Logger logger = ConfigUtils.getLogger(DBCassandra.class.getCanonicalName()); private static transient final Logger logger = ConfigUtils.getLogger(DBCassandra.class.getCanonicalName());
private DBCassandra() { private DBCassandra() {
}
private synchronized static void createInstance() {
if (dbc == null)
dbc = new DBCassandra();
// Create the connection pool // Create the connection pool
final PoolingOptions poolingOptions = new PoolingOptions(); final PoolingOptions poolingOptions = new PoolingOptions();
poolingOptions.setConnectionsPerHost(HostDistance.LOCAL, 56, 56).setConnectionsPerHost(HostDistance.REMOTE, 56, 56); poolingOptions.setConnectionsPerHost(HostDistance.LOCAL, 56, 56).setConnectionsPerHost(HostDistance.REMOTE, 56, 56);
...@@ -57,11 +63,11 @@ public class DBCassandra { ...@@ -57,11 +63,11 @@ public class DBCassandra {
} }
} }
cluster = Cluster.builder().addContactPoints(addresses).withLoadBalancingPolicy(new TokenAwarePolicy(new RoundRobinPolicy())).withPoolingOptions(poolingOptions) DBCassandra.cluster = Cluster.builder().addContactPoints(addresses).withLoadBalancingPolicy(new TokenAwarePolicy(new RoundRobinPolicy())).withPoolingOptions(poolingOptions)
// .withSocketOptions(socketOptions) // .withSocketOptions(socketOptions)
.withCredentials(user, pass).build(); .withCredentials(user, pass).build();
session = cluster.connect(); DBCassandra.session = cluster.connect();
} }
/** /**
...@@ -70,14 +76,22 @@ public class DBCassandra { ...@@ -70,14 +76,22 @@ public class DBCassandra {
* @return the instance * @return the instance
*/ */
public static Session getInstance() { public static Session getInstance() {
return dbc.session; if (dbc == null)
createInstance();
return DBCassandra.session;
} }
/** /**
* *
*/ */
public static void shutdown() { public static void shutdown() {
dbc.session.close(); DBCassandra.session.close();
dbc.cluster.close(); DBCassandra.cluster.close();
}
@Override
public Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException();
} }
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment