8

What I want to do: My application has a full connection to a Derby DB, and I want to poke around in the DB (read-only) in parallel (using a different tool).

I'm not sure how Derby actually works internally, but I understand that I can have only 1 active connection to a Derby DB. However, since the DB is only consisting of files on my HDD, shouldn't I be able to open additional connections to it, in read-only mode?

Are there any tools to do just that?

BennyInc
  • 193
  • 1
  • 5

4 Answers4

10

There are two possibilities how to run Apache Derby DB.

  1. Embedded: You run DB within your application → only one connection possible
  2. Client: You start DB as server in separate process → classic DB with many connections

You can recognize the type upon driver size. If the driver has more then 2MB that you use embedded version.

Update

When you startup the derby engine (server or embedded) it gets exclusive access to database files.

If you need to access a single database from more than one Java Virtual Machine (JVM), you will need to put a server solution in place. You can allow applications from multiple JVMs that need to access that database to connect to the server.

For details see Double-booting system behavior.

Community
  • 1
  • 1
amra
  • 16,125
  • 7
  • 50
  • 47
  • It's embedded. Still, I hoped there would be a way to open the DB (read-only) in a separate JVM again. :-/ – BennyInc Sep 09 '10 at 17:14
  • With Derby (and most other databases) it's technically not possible to get a consistent read-only 'view' of the database while another process is writing, because the writing process can overwrite data ('update in place'). – Thomas Mueller Sep 11 '10 at 10:45
6

I realize this is an old question, but I thought I might add a little more detail on a solution since links in the currently accepted answer are broken.

It is possible to run the Derby Network Server within a JVM that is using the embedded database already. The code that is using the embedded Derby database doesn't need to change anything and can keep using the DB as is, but with the Derby Network Server started, other programs can connect to derby and access the database.

All you need to do is ensure that derbynet.jar is on the classpath

And then you can do one of the following

  • Include the following line in the derby.properties file: derby.drda.startNetworkServer=true

  • Specify the property as a system property at java start java -Dderby.drda.startNetworkServer=true

  • You can use the NetworkServerControl API to start the Network Server from a separate thread within a Java application: NetworkServerControl server = new NetworkServerControl(); server.start (new PrintWriter(System.out));

More details here: http://db.apache.org/derby/docs/10.9/adminguide/tadminconfig814963.html

Keep in mind that doing this does not enable any security on this connection, so it is not a good idea to do this on a production system. It is possible to add security though and that is documented here: http://db.apache.org/derby/docs/10.9/adminguide/cadminnetservsecurity.html

Bert Lamb
  • 2,317
  • 2
  • 20
  • 26
  • So you are proposing having an embedded connection and a server connection at the same time. I'm not sure that is valid and could lead to data corruption, from https://db.apache.org/derby/docs/10.0/manuals/develop/develop79.html - with an embedded connection "Only one application can access a database at a time, and only one application can access a specific system at a time. When using a pre-1.4 JVM, Derby might not prevent multiple applications from concurrently accessing the same Derby system, but do not allow this because such access can corrupt the databases involved." – IanB Oct 10 '17 at 16:22
1

Two other ideas:

  1. In your application, shut down the database and close the connection when the database is not actively in use. Then your application won't interfere with another tool which is trying to open the database.
  2. Make a copy of your database, by taking a backup (you can do this while the database is open by your application), then restore that backup to a separate place on your disk. Then you can use another tool to access the copied database at your ease.
Bryan Pendleton
  • 16,128
  • 3
  • 32
  • 56
1

If you can afford the memory and do not need up-to-date data, then you can access read-only databases from multiple JVMs by creating in-memory copies:

ij> connect 'jdbc:derby:memory:memdb;restoreFrom=mydb';
mvanle
  • 1,847
  • 23
  • 19