top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

I am opening a single JDBC connection in my init() method. ......or the Statement object?

+1 vote
245 views

I am opening a single JDBC connection in my init() method. Do I need to synchronize on the Connection or the Statement object?

posted Oct 27, 2015 by Dominic

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

You shouldn’t have to. If your JDBC driver supports multiple connections, then the various createStatement methods will give you a thread-safe, reentrant, independent Statement that should work OK, even if other requests/threads are also accessing other Statements on the same Connection. Of course, crossing your fingers never hurts… Many early JDBC drivers were not re-entrant. The modern versions of JDBC drivers should work OK, but there are never any guarantees.
Using connection pooling will avoid the whole issue, plus will lead to improved performance.

answer Oct 29, 2015 by Karthick.c
...