> Am I posting this to the wrong place?

Actually, kind of. The proper place for that type of questions is probably
the
java driver mailing (
https://groups.google.com/a/lists.datastax.com/forum/#!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
are
not. You should have one PreparedStatement, but you everytime you bind it
for
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
abstract
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).

--
Sylvain

Reply via email to