From eda8fce8f67adb6147d777dd21d69f22e0d63cf4 Mon Sep 17 00:00:00 2001 From: Costantino Perciante Date: Wed, 9 Dec 2015 10:51:23 +0000 Subject: [PATCH] Two main updates: Astyanax dependency version updated Keyspace reference is now unique git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/portal/social-networking-library@121704 82a268e6-3cf1-43bd-a215-b396298e98cf --- pom.xml | 1 + .../server/CassandraClusterConnection.java | 53 ++++++++++++------- 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/pom.xml b/pom.xml index 9c75582..35d9b0c 100644 --- a/pom.xml +++ b/pom.xml @@ -45,6 +45,7 @@ com.netflix.astyanax astyanax + 1.56.26 org.gcube.resources.discovery diff --git a/src/main/java/org/gcube/portal/databook/server/CassandraClusterConnection.java b/src/main/java/org/gcube/portal/databook/server/CassandraClusterConnection.java index 245c92d..e82c86f 100644 --- a/src/main/java/org/gcube/portal/databook/server/CassandraClusterConnection.java +++ b/src/main/java/org/gcube/portal/databook/server/CassandraClusterConnection.java @@ -14,6 +14,7 @@ import com.netflix.astyanax.connectionpool.NodeDiscoveryType; import com.netflix.astyanax.connectionpool.OperationResult; import com.netflix.astyanax.connectionpool.exceptions.ConnectionException; import com.netflix.astyanax.connectionpool.impl.ConnectionPoolConfigurationImpl; +import com.netflix.astyanax.connectionpool.impl.ConnectionPoolType; import com.netflix.astyanax.connectionpool.impl.CountingConnectionPoolMonitor; import com.netflix.astyanax.ddl.ColumnDefinition; import com.netflix.astyanax.ddl.ColumnFamilyDefinition; @@ -74,27 +75,43 @@ public class CassandraClusterConnection { } /** - * - * @return + * Get the reference to the current keyspace + * @return keyspace reference */ - public Keyspace getKeyspace() { + public Keyspace getKeyspace() { - AstyanaxContext context = new AstyanaxContext.Builder() - .forCluster(clusterName) - .forKeyspace(keyspaceName) - .withAstyanaxConfiguration(new AstyanaxConfigurationImpl() - .setDiscoveryType(NodeDiscoveryType.NONE) - ) - .withConnectionPoolConfiguration(new ConnectionPoolConfigurationImpl("MyConnectionPool") - .setPort(9160) - .setMaxConnsPerHost(1) - .setSeeds(host) - ) - .withConnectionPoolMonitor(new CountingConnectionPoolMonitor()) - .buildKeyspace(ThriftFamilyFactory.getInstance()); + // The Keyspace instance can be shared among different requests + if(myKeyspace == null){ + synchronized(this){ + if(myKeyspace == null){ // double check lock + AstyanaxContext context = new AstyanaxContext.Builder() + .forCluster(clusterName) + .forKeyspace(keyspaceName) + .withAstyanaxConfiguration( + new AstyanaxConfigurationImpl() + .setDiscoveryType(NodeDiscoveryType.NONE) // use only the host given as seeds (do not discover) + .setConnectionPoolType(ConnectionPoolType.ROUND_ROBIN) // how handle connections of the the connection pool + ) + .withConnectionPoolConfiguration( + new ConnectionPoolConfigurationImpl("MyConnectionPool") + .setMaxConnsPerHost(3) // for each seed(host) + //.setSocketTimeout(1000) -> default: 11 seconds + //.setConnectTimeout(1000) -> default: 2 seconds + .setSeeds(host) + ) + .withConnectionPoolMonitor(new CountingConnectionPoolMonitor()) + .buildKeyspace(ThriftFamilyFactory.getInstance()); + + + context.start(); + + // save keyspace reference + myKeyspace = context.getEntity(); + + } + } + } - context.start(); - myKeyspace = context.getEntity(); return myKeyspace; }