关闭特定数据库并关闭连接的正确方法是什么?
What is the proper way to shutdown a specific db and close the connection?
我创建了以下三种方法,但对 shutDownDB() 的实际作用感到困惑。这是我的问题:
- 为什么我需要活动连接才能关闭特定数据库?
- 关闭连接然后关闭数据库不是更有意义吗
- 如果特定数据库关闭,为什么我需要关闭连接?
- 为什么我能够在特定数据库关闭后重新建立连接?
- 关闭特定数据库与关闭连接之间的实际区别是什么?
注意:在嵌入式模式下使用 derby
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);
}
}
让我们从 Derby documentation 所说的开始:
Shutting down Derby or an individual database
Applications in an embedded environment shut down the Derby system by
specifying the shutdown=true attribute in the connection URL. To shut
down the system, you do not specify a database name, and you do not
ordinarily specify any other attribute.
jdbc:derby:;shutdown=true
A successful shutdown always results in an SQLException to indicate
that Derby has shut down and that there is no other exception.
If you have enabled user authentication at the system level, you will
need to specify credentials (that is, username and password) in order
to shut down a Derby system, and the supplied username and password
must also be defined at the system level.
等等。
您的问题:
1) Why do I need an active connection to shutdown a specific database?
因为文档是这么说的。
因为他们是这样实现的。
因为关闭数据库(一般来说)需要用户
身份验证和连接建立是用户
身份验证发生。
2) Doesn't it make more sense to close the connection and then shutdown the DB?
鉴于最后,没有。
3) Why do I need to close a connection if the specific database is shutdown?
如果您的应用程序能够应对潜在的资源泄漏,您不需要;例如客户端可能未关闭的套接字。
但如果您的应用程序想要在关闭数据库后继续运行,那当然是可取的。
4) How come I'm able to reestablish a connection after the specific db has been shut down?
大概是因为它的设计允许这样做。
5) What is the actual difference between shutting down a specific db and closing the connection?
(我对此不是很确定....)
关闭数据库将使特定数据库的所有连接失效。它不会在客户端关闭它们,因此套接字可能会保持打开状态....直到您的应用程序中使用相应集合的部分尝试使用它们,发现它们是 "dead" 并关闭它们.
相比之下,关闭连接只会关闭服务器端和客户端的 那个 连接。应立即关闭任何套接字。
NOTE: using derby in embedded mode
文档没有区分嵌入式和非嵌入式模式。
我创建了以下三种方法,但对 shutDownDB() 的实际作用感到困惑。这是我的问题:
- 为什么我需要活动连接才能关闭特定数据库?
- 关闭连接然后关闭数据库不是更有意义吗
- 如果特定数据库关闭,为什么我需要关闭连接?
- 为什么我能够在特定数据库关闭后重新建立连接?
- 关闭特定数据库与关闭连接之间的实际区别是什么?
注意:在嵌入式模式下使用 derby
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);
}
}
让我们从 Derby documentation 所说的开始:
Shutting down Derby or an individual database
Applications in an embedded environment shut down the Derby system by specifying the shutdown=true attribute in the connection URL. To shut down the system, you do not specify a database name, and you do not ordinarily specify any other attribute.
jdbc:derby:;shutdown=true
A successful shutdown always results in an SQLException to indicate that Derby has shut down and that there is no other exception.
If you have enabled user authentication at the system level, you will need to specify credentials (that is, username and password) in order to shut down a Derby system, and the supplied username and password must also be defined at the system level.
等等。
您的问题:
1) Why do I need an active connection to shutdown a specific database?
因为文档是这么说的。
因为他们是这样实现的。
因为关闭数据库(一般来说)需要用户 身份验证和连接建立是用户 身份验证发生。
2) Doesn't it make more sense to close the connection and then shutdown the DB?
鉴于最后,没有。
3) Why do I need to close a connection if the specific database is shutdown?
如果您的应用程序能够应对潜在的资源泄漏,您不需要;例如客户端可能未关闭的套接字。
但如果您的应用程序想要在关闭数据库后继续运行,那当然是可取的。
4) How come I'm able to reestablish a connection after the specific db has been shut down?
大概是因为它的设计允许这样做。
5) What is the actual difference between shutting down a specific db and closing the connection?
(我对此不是很确定....)
关闭数据库将使特定数据库的所有连接失效。它不会在客户端关闭它们,因此套接字可能会保持打开状态....直到您的应用程序中使用相应集合的部分尝试使用它们,发现它们是 "dead" 并关闭它们.
相比之下,关闭连接只会关闭服务器端和客户端的 那个 连接。应立即关闭任何套接字。
NOTE: using derby in embedded mode
文档没有区分嵌入式和非嵌入式模式。