db-derby-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Knut Anders Hatlen <Knut.Hat...@Sun.COM>
Subject Re: Does using DerbyDB require synchronization?
Date Mon, 08 Sep 2008 20:58:33 GMT
derbyfan195 <huang195@gmail.com> writes:

> Just started using Derby in my Java code and realized some weird
> synchronization issues. I have multiple threads accessing the same Derby
> Database (embedded one) through a single connection. They issue read-only
> queries, i.e., select, each with its own statement, and does some operations
> on the returned ResultSet using ResultSet.next() operation. However, I
> noticed sometimes, one of these next() calls would fail and gave me an error
> of next() is not permitted, make sure that AUTO_COMMIT is turned OFF. I
> don't know what this error means or why I need to turn AUTO_COMMIT off. Do I
> need any synchronization among the statement objects obtained from the same
> connection object? Any help would be appreciated.


You can turn auto-commit off by calling conn.setAutoCommit(false). The
reason why it may help, is that with auto-commit on, the transaction
(Connection) is committed automatically each time a ResultSet is closed,
and when the transaction is committed, all ResultSets in that connection
lose their positions (and, depending on the holdability setting, they
may be closed).

That said, whereas using the same connection concurrently from different
threads should work, I would recommend that you have a separate
connection for each thread. That would give you fewer problems with the
different threads changing the transaction state for each other, and it
could improve the performance since Derby could then execute more
queries in parallel.

Knut Anders

View raw message