title: Efficient way to connect to Cassandra database using Pelops client
body: <p>I am working on a project in which I need to use <code>Cassandra Database</code>. I have a sample program that will populate data into <code>Cassandra database</code>. I am using <code>Pelops client</code> for that.</p>
  
  <p>So now I am thinking of making a <code>Singleton class</code> for <code>Cassandra database</code> that will make a connection to <code>Cassandra database</code> and then I be using that instance from <code>Singelton class</code> into my <code>CassandraDAO</code> to insert into Cassandra database and retrieve the data from Cassandra database as well.</p>
  
  <p>Below is my Singleton class that I have built so far which will make a connection to Cassandra database-</p>
  
  <pre><code>public class CassandraConnection {
  
      private static CassandraConnection _instance;
      private String keyspace;
      private String[] seeds;
      private int port;
      private String poolName;
  
      public static synchronized CassandraConnection getInstance() {
          if (_instance == null) {
              _instance = new CassandraConnection();
          }
          return _instance;
      }
  
      private CassandraConnection() {
          setKeyspace(ICassandraDo.KEYSPACE_NAME);
          setSeeds(ICassandraDo.NODES).split(",");
          setPort(ICassandraDo.CASSANDRA_PORT);
          setPoolName(ICassandraDo.THRIFT_CONNECTION_POOL);
  
          createPool();
      }
  
      //This is the right way to `addPool` in pelops?
      private void createPool() {
          Pelops.addPool(getPoolName(), getSeeds(), getPort(),
                  false, getKeyspace(), new Policy());
  
      }
  
      private String setSeeds(String nodes) {
  
      // I am not sure what I am supposed to do here? 
      // Any guidance will be of great help
  
      }
  
      private void setPoolName(String thriftConnectionPool) {
          this.poolName = thriftConnectionPool;
      }
  
      private void setPort(int cassandraPort) {
          this.port = cassandraPort;
      }
  
      private void setKeyspace(String keyspaceName) {
          this.keyspace = keyspaceName;
  
      }
  
      public void setSeeds(String[] seeds) {
          this.seeds = seeds;
      }
  
      public String[] getSeeds() {
          return seeds;
      }
  
      public int getPort() {
          return port;
      }
  
      public String getKeyspace() {
          return keyspace;
      }
  
      public String getPoolName() {
          return poolName;
      }
  }
  </code></pre>
  
  <p><strong>Problem Statement:-</strong></p>
  
  <p>I have few doubts in my above code.</p>
  
  <ol>
  <li>Firstly, what I am supposed to do in <code>setSeeds</code> method in my above class? Any pointers or example will be of great help.</li>
  <li>Secondly, I am not sure whether this is the right way to do this as I am creating a Singleton class?  I am wondering what's the best approach is for managing a cluster connection with pelops client.</li>
  <li>And also, what's the best way of using <code>addPool</code> method in my above code? I guess, I messed up something over there as well? As I keep on seeing different <code>addPool</code> methods in <code>Pelops class</code>? So which method I should be using keeping in mind as I will be running this in Production environment.</li>
  </ol>
  
  <p>And after the above Singleton class is ready, I am planning to use the above class in my <code>DAO</code> code, something like this-</p>
  
  <p><code>Mutator mutator = Pelops.createMutator(CassandraConnection.getInstance().getPoolName());</code>
  <code>mutator.writeColumns(other data inside)</code>;</p>
  
  <p>And then do the selector as well for retrieving the data.</p>
  
  <p>Just FYI, I am working with <code>Cassandra 1.2.3</code> and <code>Scale 7 pelops client</code>.</p>
  
  <p>Any help will be appreciated. Thanks in advance.</p>
  
  <p><strong>Updated Code:-</strong></p>
  
  <p>Below is my updated code.</p>
  
  <pre><code>public class CassandraConnection {
  
      private static CassandraConnection _instance;
      private String keyspace;
      private String[] nodes;
      private int port;
      private String poolName;
  
  
      public static synchronized CassandraConnection getInstance() {
          if (_instance == null) {
              _instance = new CassandraConnection();
          }
          return _instance;
      }
  
      private CassandraConnection() {
          setKeyspace(ICassandraDo.KEYSPACE_NAME);
          setNodes(ICassandraDo.NODES);
          setPort(ICassandraDo.CASSANDRA_PORT);
          setPoolName(ICassandraDo.THRIFT_CONNECTION_POOL);
  
          createPool();
      }
  
  
      private void createPool() {
          Pelops.addPool(getPoolName(), getCluster(), getKeyspace());
  
      }
  
      private Cluster getCluster() {
  
          Config casconf = new Config(ICassandraDo.CASSANDRA_PORT, true, 0); 
  
          Cluster cluster= new Cluster(nodes, casconf, ICassandraDo.NODE_DISCOVERY);
  
          return cluster; 
      }
  
  
      private void setPoolName(String thriftConnectionPool) {
          this.poolName = thriftConnectionPool;
      }
  
      private void setPort(int cassandraPort) {
          this.port = cassandraPort;
      }
  
      private void setKeyspace(String keyspaceName) {
          this.keyspace = keyspaceName;
  
      }
  
      private void setNodes(String nodes) {
          this.nodes = nodes.split(",");
      }
  
      public int getPort() {
          return port;
      }
  
      public String getKeyspace() {
          return keyspace;
      }
  
      public String getPoolName() {
          return poolName;
      }
  }
  </code></pre>
  
  <p><strong>Just FYI, In my case, I am going to have two clusters each with 12 nodes.</strong></p>
  
  <p>Can anyone take a look and let me know I got everything correctly? Thanks for the help.</p>