Note that there are some explanatory texts on larger screens.

plurals
  1. POThread Safe singleton class
    primarykey
    data
    text
    <p>I wrote a below Singleton class. I am not sure whether this is thread safe singleton class or not?</p> <pre><code>public class CassandraAstyanaxConnection { private static CassandraAstyanaxConnection _instance; private AstyanaxContext&lt;Keyspace&gt; context; private Keyspace keyspace; private ColumnFamily&lt;String, String&gt; emp_cf; public static synchronized CassandraAstyanaxConnection getInstance() { if (_instance == null) { _instance = new CassandraAstyanaxConnection(); } return _instance; } /** * Creating Cassandra connection using Astyanax client * */ private CassandraAstyanaxConnection() { context = new AstyanaxContext.Builder() .forCluster(ModelConstants.CLUSTER) .forKeyspace(ModelConstants.KEYSPACE) .withAstyanaxConfiguration(new AstyanaxConfigurationImpl() .setDiscoveryType(NodeDiscoveryType.RING_DESCRIBE) ) .withConnectionPoolConfiguration(new ConnectionPoolConfigurationImpl("MyConnectionPool") .setPort(9160) .setMaxConnsPerHost(1) .setSeeds("127.0.0.1:9160") ) .withAstyanaxConfiguration(new AstyanaxConfigurationImpl() .setCqlVersion("3.0.0") .setTargetCassandraVersion("1.2")) .withConnectionPoolMonitor(new CountingConnectionPoolMonitor()) .buildKeyspace(ThriftFamilyFactory.getInstance()); context.start(); keyspace = context.getEntity(); emp_cf = ColumnFamily.newColumnFamily( ModelConstants.COLUMN_FAMILY, StringSerializer.get(), StringSerializer.get()); } /** * returns the keyspace * * @return */ public Keyspace getKeyspace() { return keyspace; } public ColumnFamily&lt;String, String&gt; getEmp_cf() { return emp_cf; } } </code></pre> <p>Can anyone help me with this? Any thoughts on my above Singleton class will be of great help.</p> <p><strong>Updated Code:-</strong></p> <p>I am trying to incorporate Bohemian suggestion in my code. Here is the updated code, I got-</p> <pre><code>public class CassandraAstyanaxConnection { private static class ConnectionHolder { static final CassandraAstyanaxConnection connection = new CassandraAstyanaxConnection(); } public static CassandraAstyanaxConnection getInstance() { return ConnectionHolder.connection; } /** * Creating Cassandra connection using Astyanax client * */ private CassandraAstyanaxConnection() { context = new AstyanaxContext.Builder() .forCluster(ModelConstants.CLUSTER) .forKeyspace(ModelConstants.KEYSPACE) .withAstyanaxConfiguration(new AstyanaxConfigurationImpl() .setDiscoveryType(NodeDiscoveryType.RING_DESCRIBE) ) .withConnectionPoolConfiguration(new ConnectionPoolConfigurationImpl("MyConnectionPool") .setPort(9160) .setMaxConnsPerHost(1) .setSeeds("127.0.0.1:9160") ) .withAstyanaxConfiguration(new AstyanaxConfigurationImpl() .setCqlVersion("3.0.0") .setTargetCassandraVersion("1.2")) .withConnectionPoolMonitor(new CountingConnectionPoolMonitor()) .buildKeyspace(ThriftFamilyFactory.getInstance()); context.start(); keyspace = context.getEntity(); emp_cf = ColumnFamily.newColumnFamily( ModelConstants.COLUMN_FAMILY, StringSerializer.get(), StringSerializer.get()); } /** * returns the keyspace * * @return */ public Keyspace getKeyspace() { return keyspace; } public ColumnFamily&lt;String, String&gt; getEmp_cf() { return emp_cf; } } </code></pre> <p>Can anyone take a look and let me know if this time I got it right or not?</p> <p>Thanks for the help.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload