tags:

views:

759

answers:

2

Hi guys i can connect Derby via Eclipse Database Development but i cannot connect Derby with same url with Eclipse Database Development's through Hibernate. Error :"Caused by: java.sql.SQLException: Another instance of Derby may have already booted the database "

+2  A: 

It seems as if you are running Derby as an embedded DB and not a DB Server.

The problem is both are running in different JVMs and a given database can only be accessed from one JVM.

Eclipse will start another JVM, when you start testing your program and not allow to conenect to the embedded DB in eclipse. I think the how to use Derby as a network DB Server can help you.

Daff
An alternative would be to disconnect from the DB in the tooling prior to running the code - via the `Data Source Explorer` view.
McDowell
+1  A: 

Just finished a project that did this, using Derby with Hibernate, a couple of days ago. (With Derby running in the same JVM.)

As I understand it when you use the Embedded driver it by default starts the database instance as part of the driver and as long as you hang onto the connection the database is running. But for Hibernate it likes to have a DataSource given to it which should really be a pooling data source.

The above answer is correct, it is really a good idea to start Derby as a Network DB Server, even if your in the same JVM. You can still use the embedded JDBC driver which seems to know when the database your connecting to is in Network mode and adjusts accordingly. (This also allows using a third party tool to connect to the database and view and edit the data and schema while it is running, very handy for debugging.)

System.setProperty("derby.system.home", applicationHome);

NetworkServerControl serverControl = new NetworkServerControl(InetAddress.getByName(m_address),port);

serverControl.start(new PrintWriter(System.out, true));

Once the database is running you then stick a DataSource instance in a JNDI registry. Then Hibernate can access this data source, given it's name, from the JNDI registry.

EmbeddedConnectionPoolDataSource40 dataSource = new EmbeddedConnectionPoolDataSource40();
dataSource.setDatabaseName(databaseName);
dataSource.setUser(username);
dataSource.setPassword(password);

The EmbeddedConnectionPoolDataSource40 is the DataSource implementation to use with a pooling DataSource wrapper so connections can be reused where possible. I used Apache Commons DBCP, and modified one of the examples to create my own pooling DataSource using EmbeddedConnectionPoolDataSource40.

Nathan Johns