I have a UserDataDB that I cannot get a connection to in a Java program after I was connected to it in Windows CMD. With java org.apache.derby.tools.ij open in CMD I connect to UserDataDB and eventually disconnect from it. With ij still open the Java program doesn’t connect. Why is it that my Java program doesn’t connect with Ij still running even if I disconnected from the database in Ij? I have to exit ij for the Java program to connect to the database.
try{
Connection con = DriverManager.getConnection("jdbc:derby:C:\Program Files\Java\jdk-22\db\UserDataDB; create=true;");
System.out.println("Good connections. ");
con.close();
}catch (Exception e){
System.out.println(e.getMessage());
}
error:
XJ040.C : [0] C:Program FilesJavajdk-22dbUserDataDB, [1] jdk.internal.loader.ClassLoaders$AppClassLoader@4fca772d
5
When Derby is run in embedded mode, only one program at a time can have the database open. Derby uses a locking mechanism to prevent multiple programs from opening the database at the same time. When you exited your ij session, that unlocked the database, and allowed your program to run.
Disconnecting from the database is not enough, you have to actually shutdown the engine in order to release the lock.
If you want to allow multiple programs to open the same database at the same time, you can use Derby in Client/Server mode.
Learn more about the two modes of running Derby here.