English 中文(简体)
How to shutdown Derby in-memory database Properly
原标题:

I m using derby as an embedded database. Furthermore, I m using it s in-memory database option for my unit tests.

What I can t figure out is how to properly shut down (A quick look at the code) the Derby database. I beleive I have it working for a standard database but I m getting different exceptions when attempt similar code on a in-memory database.

I m going to omit details, I ll add them if other feel are needed.

Basically, I m trying to shut down my database in these two fashions where my in-memory database is consistently called "eh":

DriverManager.getConnection("jdbc:derby:memory:eh;shutdown=true");

then:

DriverManager.getConnection("jdbc:derby:eh;shutdown=true");

The former results in an exception but not the one expected. The details are:

java.sql.SQLNonTransientConnectionException: Database memory:eh shutdown.

The latter results in

java.sql.SQLException: Database eh not found.

Based on what I ve been able to figure out, we want a SQLException but not the one we receive. On the other hand, the SQLNonTransientConnectionException error seems more appropriate but isn t the right type (though it is derived from SQLException) nor does it have the right state code. The state code end up being: 08006.

The example code I have illustrates that a SQLException with a SQL state of "XJ015".

Note: The example I m referencing is: WwdEmbedded Program (Java Code).

最佳回答

XJ015 (with SQLCODE 50000) is the expected (successful) SQLSTATE for complete system shutdown. 08006 (with SQLCODE 45000), on the other hand, is the expected SQLSTATE for shutdown of only an individual database.

DriverManager.getConnection("jdbc:derby:;shutdown=true");

Shuts down the entire system and should result in XJ015.

问题回答

The URL "jdbc:derby:memory:eh;shutdown=true" results in the expected 08006 error code, but doesn t actually remove the DB from memory. If later on, you try to create a new database with "jdbc:derby:memory:eh;create=true", you ll get an error saying that the database already exists.

Fortunately, as of Derby 10.6.1.0 (released on May 17, 2010), it is possible to actually drop an in-memory database using a URL of the form "jdbc:derby:memory:eh;drop=true". See the release notes and the page Using in-memory databases.

I believe that your first code example is fine. The SQL State difference that you are seeing, I believe, is because you are running Derby embedded, but the example code that you saw (with SQL state XJ015) was running in a client-server configuration.

As you noted, the SQLNonTransientConnectionException is a subclass of SQLException, so I am confused as to why you think you are not getting the right type of exception.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签