Question about thread-safety of PreparedStatements

458 views
Skip to first unread message

Donald Smith

unread,
Mar 19, 2014, 1:28:04 PM3/19/14
to java-dri...@lists.datastax.com
Using DataStaxDriver 2.0.X, in code like

    public Set<Integer> loadUserIds(String userid) {

        ResultSet result = executeReadQueryWithRetry( m_loadUserIdssStatement.bind(userid));

        String string= result.one().getString("ids");

        return convertStringToSetOfIntegers(string);

    }



my coworker was concerned that there might be a race condition between the blue and the green code (during the binding of the userid).  But bind returns a BoundStatment and I think that exists in the local thread only, so am I correct that this code is thread safe?

 Thanks, Don

Alex Popescu

unread,
Mar 19, 2014, 1:34:27 PM3/19/14
to java-dri...@lists.datastax.com
While I'm not very sure what you mean by "the blue and the green code", what PreparedStatement.bind does is just creating a new BoundStatement. Bottom line it's thread safe.


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



--

:- a)


Alex Popescu
Sen. Product Manager @ DataStax
@al3xandru

Donald Smith

unread,
Mar 19, 2014, 1:50:42 PM3/19/14
to java-dri...@lists.datastax.com
Oh, maybe your email client doesn't show the colors.  executeReadQueryWithRetry( m_loadUserIdssStatement.bind(userid)).   executeReadQueryWithRetry is blue.  m_loadUserIdssStatement.bind(userid) is green.

Anyway, the method "bind"  SOUNDS like it modifies (mutates) the preparedStatement.  If what you're saying is true, then it does not in fact modify it; instead, it returns a new object in the current thread.  The name is confusing. The documentation should make it clear that it's thread-safe.

 Don

Alex Popescu

unread,
Mar 19, 2014, 1:57:33 PM3/19/14
to java-dri...@lists.datastax.com


On Wednesday, March 19, 2014, Donald Smith <thinke...@gmail.com> wrote:
Oh, maybe your email client doesn't show the colors.  executeReadQueryWithRetry( m_loadUserIdssStatement.bind(userid)).   executeReadQueryWithRetry is blue.  m_loadUserIdssStatement.bind(userid) is green.

Anyway, the method "bind"  SOUNDS like it modifies (mutates) the preparedStatement.  If what you're saying is true, then it does not in fact modify it; instead, it returns a new object in the current thread.  The name is confusing. The documentation should make it clear that it's thread-safe.


 Don


The javadoc for bind methods is pretty clear imo: "Creates a new BoundStatement object..."

How would you suggest to make this even more clear?

Donald Smith

unread,
Mar 19, 2014, 2:07:19 PM3/19/14
to java-dri...@lists.datastax.com
"Creates a new BoundStatement object without modifying the the PreparedStatement; bind is thread safe."

Donald Smith

unread,
Mar 20, 2014, 12:41:22 PM3/20/14
to java-dri...@lists.datastax.com
OK, here's a further question. I know that BoundStatements are thread safe, but are they concurrent or do they synchronize behind the scenes and get executed sequentially? I presume they're truly concurrent: multiple threads binding the same PreparedStatement will not interfere.

 Don

Sylvain Lebresne

unread,
Mar 20, 2014, 12:58:15 PM3/20/14
to java-dri...@lists.datastax.com
On Thu, Mar 20, 2014 at 5:41 PM, Donald Smith <thinke...@gmail.com> wrote:
OK, here's a further question. I know that BoundStatements are thread safe,

They are not! PreparedStatement is thread safe, because it's basically an immutable object (to be very precise, it's not exactly true, as the property of the PrepareStatment (ConsitencyLevel, ...) can be updated and this is not thread-safe, you have to synchronize in your app, though it's not really a problem in practice since it make no sense to update those concurrently, or more than once really).

But every time you want to execute a PreparedStatement, you should create a new BoundStatement, which is really some values for execution. BoundStatement itself is not thread safe, but it's not meant to be use concurrently. You can reuse BoundStatement if you really want (though you don't have to, it's fine creating a new one for each execution), but it's your job to synchronize if you do so concurrently (and truly, I wouldn't advise it, you can reuse BoundStatement, but only do it within a given thread so you don't have to synchronize).
 
but are they concurrent or do they synchronize behind the scenes and get executed sequentially? I presume they're truly concurrent: multiple threads binding the same PreparedStatement will not interfere.

Does what's above answer your question?

--
Sylvain

Donald Smith

unread,
Mar 20, 2014, 7:54:01 PM3/20/14
to java-dri...@lists.datastax.com
OK, I misspoke when I said that BoundStatements are thread safe. What I meant was: if multiple threads create different BoundStatement objects from a single PreparedStatement, those objects can safely be used in the respective threads. 

My further question is:  will the multiple BoundStatement objects created from the single PreparedStatement serialize their activity behind the scenes? I presume not. I presume they're truly concurrent and don't synchronize.

 Thanks, Don

Sylvain Lebresne

unread,
Mar 21, 2014, 6:04:21 AM3/21/14
to java-dri...@lists.datastax.com
On Fri, Mar 21, 2014 at 12:54 AM, Donald Smith <thinke...@gmail.com> wrote:
OK, I misspoke when I said that BoundStatements are thread safe. What I meant was: if multiple threads create different BoundStatement objects from a single PreparedStatement, those objects can safely be used in the respective threads.

Right.
 

My further question is:  will the multiple BoundStatement objects created from the single PreparedStatement serialize their activity behind the scenes? I presume not. I presume they're truly concurrent and don't synchronize.

There is no behind-the-scene synchronization no. But if you are interested by what's behind the scene, I can suggest having a quick look at the code: PrepareStatement and BoundStatement are pretty trivial classes, and hopefully that could answer all your questions.

--
Sylvain
Reply all
Reply to author
Forward
0 new messages