Prahalad Deshpande
Prahalad Deshpande

Reputation: 4767

Apache Derby Embedded Mode and Multi-Threaded Connection Management

I am currently working on an application (whose logic and code I cannot put out here) that creates an embedded derby database and interacts with it using multiple threads to perform CRUD and SELECT operations.

Let's say the name of the embedded database is bar and the path to this database is c:\foo\bar

Multiple threads open a their own connection to c:\foo\bar and perform their respective operations against their own tables in the database. The connection to the database is abstracted out by a decorator that also maintains the last time the connection was accessed. If the last time the connection was accessed exceeds a particular threshold the database connection is shutdown and reaped. There is a reaper thread that runs at a pre-defined scheduled interval and performs the reaping logic. As a part of the reaping logic it uses the following shutdown URL:

jdbc:derby:c:\foo\bar;shutdown=true

Any thread that attempts to perform a query against this database after the reaper thread has run fails with Derby error 8003 which indicates that there is no current connection.

So is it that in Derby embedded mode even though each thread has opened it's own connection; when the reaper thread runs it shuts down the entire database and any connections that were previously opened against this database across all threads are now in an invalid or closed state?

What are the best practices for using embedded derby within such applications?

Upvotes: 0

Views: 615

Answers (1)

Bryan Pendleton
Bryan Pendleton

Reputation: 16359

The shutdown=true attribute on the Derby JDBC Connection URL doesn't shut down the connection, it shuts down the database.

See: http://db.apache.org/derby/docs/10.13/ref/rrefattrib16471.html

If you just want to shut down a connection, call Connection.close()

Upvotes: 1

Related Questions