cassandra-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sylvain Lebresne <>
Subject Re: Proper Use of PreparedStatements in DataStax driver
Date Fri, 11 Oct 2013 07:21:23 GMT
> Am I posting this to the wrong place?

Actually, kind of. The proper place for that type of questions is probably
java driver mailing (!forum/java-driver-user
But anyway, answers follows.

> 1. My question is should I only instantiate the PreparedStatement once and
> reuse *the same instance* every time I want to execute an INSERT?  Is that
> necessary to get the performance benefits of using PreparedStatements?

Yes, you should. You should only call Session.prepare once.

> If the answer is #1, then that means PreparedStatements are effectively
> singletons and need to be multi-thread-safe and the app is responsible for
> dealing with lifecycle issues presuming that the original Session dies or
> whatever

PreparedStatement is immutable and thus thread-safe. However BoundStatement
not. You should have one PreparedStatement, but you everytime you bind it
execution, you'll created a new BoundStatement.

As for the lifecycle, there is nothing really to deal with. A Session in the
driver is *not* just one server connection. It's a thread-safe object that
encapsulate pools of connections to the nodes in the cluster and that
from the application the issues of handling node failures. A Session never
dies, and as a consequence a PreparedStatement is always valid (unless you
manually shutdown the Session that is).


View raw message