db-derby-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Daniel John Debrunner <...@apache.org>
Subject Re: Synchronization on the connection in JDBC calls
Date Thu, 29 Jun 2006 19:28:59 GMT
Kristian Waagan wrote:

> Hello,
> 
> When working on EmbedPreparedStatement.java, I noticed that a few
> methods synchronize on the connection object, for instance addBatch. The
> code typically looks like this:
> 
> synchronized (getConnectionSynchronization()) {
>     // Do stuff
> }
> 
> The getConnectionSynchronization-method returns a reference to the root
> connection. I also saw that EmbedResultSet uses this more.
> The rest of the JDBC methods in EmbedPreparedStatement don't synchronize
> on this object.
> 
> I think a single connection is not expected to be shared between
> multiple threads simultaneously. Maybe the necessary synchronization is
> done at a lower level in the system.
> 
> Would anyone care to share their thoughts/knowledge on this issue?

In addition to what Bryan's correct reply, I'd add that the Derby
embedded engine requires this high level synchronization. The internals
depend on the fact that only a single thread can be active in a context
manager/language connection context at any time. A thread becomes active
in a context manager/language connection context when it enters the JDBC
api and inactive when it leaves the JDBC thread. This is shown in slides
18,19,20 & 33,34 of
http://db.apache.org/derby/binaries/ApacheDerbyInternals_1_1.pdf

There are some unsynchronized calls through the JDBC layer,where one can
demonstrate that:
   - no unxpected exceptions (E.g. NPE, IndexOfOutBounds) will be thrown
due to potential concurrent access of any JDBC objects owned by the same
connection.
   - no incorrect behaviour will occur (e.g. wrong results) due to
potential concurrent access of objects owned by the same connection.

Dan.


Dan.


Mime
View raw message