I created the following three methods but am confused about what shutDownDB() is really doing. Here are my questions:
- Why do I need an active connection to shutdown a specific database?
- Doesn't it make more sense to close the connection and then shutdown the DB
- Why do I need to close a connection if the specific database is shutdown?
- How come I'm able to reestablish a connection after the specific db has been shut down?
- What is the actual difference between shutting down a specific db and closing the connection?
NOTE: using derby in embedded mode
public static Connection openDB(String dbFolderString) {
Connection conn = null;
try{
File dbFolder = new File(dbFolderString);
String URL = "jdbc:derby:" + dbFolderString + ";create=true";
if(print)System.out.println("\n" + "db exists " + dbFolder.exists());
conn = DriverManager.getConnection(URL);
if(print)System.out.println("Succesfully connected to " + dbFolderString);
}catch(SQLException e){
System.out.println("FATAL ERROR: from getDB " + e);
System.exit(0);
}
return conn;
}
public static boolean shutDownDB(Connection conn) {
//shutsdown a specific database but DOES NOT SHUTDOWN DERBY
try{
String[] tokens;
String url = conn.getMetaData().getURL();
tokens = url.split(":");
DriverManager.getConnection("jdbc:derby:" + tokens[2] +";shutdown=true");
}catch(SQLException e1){
if(e1.getSQLState().equals("08006") && e1.getErrorCode() == 45000){
if(false)System.out.println(e1.getSQLState() + " " + e1.getErrorCode());
if(print)System.out.println("\n" + "Database shutdown successful");
}else{
System.out.println(e1.getSQLState() + " " + e1.getErrorCode());
System.out.println("FATAL ERROR: Database not shutdown " + e1);
System.exit(0);
}
}
return true;
}
public static void closeConnection(Connection conn){
try{
conn.close();
if(print)System.out.println("Connection closed");
}catch(SQLException e){
System.out.println("connection NOT closed " + e);
System.exit(0);
}
}