Noob problem: Tomcat + Cassandra can only serve 1 query at a time

18 views
Skip to first unread message

Mark

unread,
Aug 18, 2016, 2:28:24 AM8/18/16
to DataStax Java Driver for Apache Cassandra User Mailing List
Hi all,

This is a pretty stupid problem but I cannot find a solution. In Tomcat, I cannot perform more than one query at a time to C*. Below you can find the singleton class (CassandraManager) I'm using to connect to C* which in initialized with Tomcat's context. I've already tried to increase the number of connections (poolingOptions)  but it doesn't work. 

With "one query at a time" I mean that if 2 browsers are open, the second request has to wait for the first one to complete! I strongly suspect I made same pretty stupid mistake in my CassandraManager but I cannot find where.

Thanks a lot for your help!
Mark


I'm usind DSC 2.2 with driver 3.0. Everything is running locally on my laptop. The way I query C* is the following:

.....
Statement select = QueryBuilder.select().all().from(keyspace, tableName).where(QueryBuilder.eq("id", id)).and(QueryBuilder.eq("version", version));

ResultSet results = CassandraManager.execute(select);

.....



And this is the CassandraManager class that I'm using:


package com.db.main;

import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ScheduledExecutorService;

import com.datastax.driver.core.BoundStatement;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.PagingState;
import com.datastax.driver.core.PoolingOptions;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.Statement;
import com.datastax.driver.core.querybuilder.QueryBuilder;
import com.plugg.system.RuntimeProperties;
import com.plugg.system.Timer;

public class CassandraManager {
        private static PoolingOptions poolingOptions;
  private static Cluster cluster;
        private static Session session;
        private static boolean FORCE_DEBUG = false;
   
        private static ScheduledExecutorService scheduled;
     
       private synchronized static void Init() throws Exception {      
               poolingOptions = new PoolingOptions();
         
               cluster = Cluster
                      .builder()
                 .addContactPoints(RuntimeProperties.getSystemProperties().cassandraHost)
               .withPoolingOptions(poolingOptions)
            .build();          
               
                session = cluster.connect();
                         
        }
       
        private synchronized static Session getSession() throws Exception {
            if(cluster==null) {
                    CassandraManager.Init();
               }
              return session;
}
     
        public synchronized static BoundStatement getBoundStatement(String query) throws Exception {
           PreparedStatement statement = getSession().prepare(query);
             return new BoundStatement(statement);
  }
             
        public synchronized static ResultSet execute(String query, boolean debug) throws Exception {
           ResultSet res = null;
         
                if(debug || FORCE_DEBUG) {
                     Timer.Start();
                 Session sess = getSession();
                   Timer.Stop();
                  long eOne = Timer.getElapsedMilliseconds();
                   
                        Timer.Stop();
                  res = sess.execute(query);
                     Timer.Stop();
                  long eTwo = Timer.getElapsedMilliseconds();
                   
                        System.out.println("Session in "+eOne+"ms; Executed in: "+eTwo+"ms; "+query);
          } else {
                       res = getSession().execute(query);
             }
             
               
                return res;
    }
             
        public synchronized static ResultSet execute(Statement statement, boolean debug) throws Exception {
            ResultSet res = null;
         
                if(debug || FORCE_DEBUG) {
                     Timer.Start();
                 Session sess = getSession();
                   Timer.Stop();
                  long eOne = Timer.getElapsedMilliseconds();
                   
                        Timer.Stop();
                  res = sess.execute(statement);
                 Timer.Stop();
                  long eTwo = Timer.getElapsedMilliseconds();
                   
                        System.out.println("Session in "+eOne+"ms; Executed in: "+eTwo+"ms; "+statement.toString());
           } else {
                       res = getSession().execute(statement);
         }
              return res;
    }
     
       
        public synchronized static  void Close() {      
               if(scheduled!=null) {
                  scheduled.shutdown();
          }
             
                if(session!=null) {
                    session.close();
               }
              if(cluster!=null) {
                    cluster.close();
               }              
        }
     
        public synchronized static  UUID generateId() {
        return UUID.randomUUID();
      }
             
}


Adil

unread,
Aug 18, 2016, 4:44:25 AM8/18/16
to java-dri...@lists.datastax.com
I think your question is not related to C* but to how u have user the driver... i don't understand the need of using synchronized everywhere, if you initialize the session driver in the tomcat context u can use it for the whole application.

Ad.

--
You received this message because you are subscribed to the Google Groups "DataStax Java Driver for Apache Cassandra User Mailing List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to java-driver-user+unsubscribe@lists.datastax.com.

Ryan Svihla

unread,
Aug 18, 2016, 7:13:07 AM8/18/16
to java-dri...@lists.datastax.com

Session is thread safe so you're doing extra work here for no benefit, ideally you set the call to init() in an initializer and then can avoid all of the other synchronization calls

Anyway your execute call is the big culprit here and literally your telling java to have only one thread access execute() at once which is the behavior you're describing.


To unsubscribe from this group and stop receiving emails from it, send an email to java-driver-us...@lists.datastax.com.

--
You received this message because you are subscribed to the Google Groups "DataStax Java Driver for Apache Cassandra User Mailing List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to java-driver-us...@lists.datastax.com.
--
Regards,

Ryan Svihla

Mark

unread,
Aug 18, 2016, 2:09:21 PM8/18/16
to DataStax Java Driver for Apache Cassandra User Mailing List
I'm sorry I posted such a stupid question! This was definitely not a C* problem, I cannot believe I didn't see all the functions were "synchronized"! removed it...everything is fine. 

Ryan Svihla

unread,
Aug 18, 2016, 3:56:18 PM8/18/16
to java-dri...@lists.datastax.com
We've all done similar before, I wouldn't worry about it. Best of luck.

Regards,

Ryan Svihla

On Aug 18, 2016, at 1:09 PM, Mark <luca.ro...@plugg.co> wrote:

I'm sorry I posted such a stupid question! This was definitely not a C* problem, I cannot believe I didn't see all the functions were "synchronized"! removed it...everything is fine. 

Thanks a lot for your help!  

--
Reply all
Reply to author
Forward
0 new messages