HSQLDB 从不关闭数据库连接
HSQLDB never closes DB connection
我就是在玩这种数据库,用完之后试过关闭HSQLDB连接,但是最后还是打开了。
代码:
//----This methods are in a specific connection class file
public static Connection conn = null;
public static Connection getConnection(){
try {
input = new FileInputStream("PathToMyPropertiesFile");
prop.load(input);
//The properties constants are correctly checked
Class.forName(prop.getProperty("DRIVER_HSQLDB"));
conn = DriverManager.getConnection(prop.getProperty("CONN_HSQLDB"));
}
catch(ClassNotFoundException | SQLException e) {
LOG.log(null,"Error: "+e);
}
catch (IOException ex) {
LOG.log(null,"FILE ERROR: "+ex);
}
finally {
if (input != null) {
try {
input.close();
} catch (Exception e) {
LOG.log(null,"CLOSE ERROR: "+e);
}
}
}
return conn;
}
public static boolean stopConn() {
try {
if(conn != null) {
conn.close();
System.err.println("\nCLOSE CONN\n"+conn);
return true;
}
}
catch (SQLException e) {
e.printStackTrace();
return false;
}
return false;
}
//========= the other class file with the methods to use the conneciton
public static boolean insertUser(String uName, String uEmail){
Connection con;
con = ConnectionDB.getConnection();
PreparedStatement ps = null;
try {
String consulta = "insert into USERS (\"NICK\",\"EMAIL\") VALUES(?,?);";
ps = con.prepareStatement(consulta);
System.err.println(ps);
ps.setString(1,uName);
ps.setString(2,uEmail);
System.err.println("\nASSIGNATION\n"+ps);
if(ps.executeUpdate() == 1) {
System.err.println("\nTRUE\n");
return true;
}
}
catch(SQLException e) {
e.printStackTrace();
}
finally {
try {
System.err.println("\nFINALLY\n"+ps);
if(ps != null) {
ps.close();
System.err.println("\nCLOSE PS\n"+ps);
}
if(con != null) {
con.close();
System.err.println("\nCLOSE CON\n"+con);
if(ConnectionDB.stopConn()) {
System.err.println("\nALL IS OK\n"+ConnectionDB.conn);
}
else {
System.err.println("\nMEEEEKKKK!!!\n"+ConnectionDB.conn);
}
}
}
}
return false;
}
控制台给我这个结果,我不知道为什么连接没有关闭,因为我尝试关闭它两次。如果有人有想法请告诉我。
org.hsqldb.jdbc.JDBCPreparedStatement@4501280b[sql=[insert into USERS ("NICK","EMAIL") VALUES(?,?);], parameters=[[null], [null]]]
ASSIGNATION
org.hsqThis is my cldb.jdbc.JDBCPreparedStatement@4501280b[sql=[insert into USERS ("NICK","EMAIL") VALUES(?,?);], parameters=[[extra], [extra@mail.com]]]
TRUE
FINALLY
org.hsqldb.jdbc.JDBCPreparedStatement@4501280b[sql=[insert into USERS ("NICK","EMAIL") VALUES(?,?);], parameters=[[extra], [extra@mail.com]]]
CLOSE PS
org.hsqldb.jdbc.JDBCPreparedStatement@4501280b[closed]
CLOSE CON
org.hsqldb.jdbc.JDBCConnection@3e5b87f5
CLOSE CONN
org.hsqldb.jdbc.JDBCConnection@3e5b87f5
ALL IS OK
org.hsqldb.jdbc.JDBCConnection@3e5b87f5
关闭 JDBC 连接不会关闭进程中的数据库。这允许您在应用程序运行期间打开和关闭不同的连接。
您需要执行 JDBC 语句来关闭数据库。要执行的 SQL 语句是 "SHUTDOWN".
可以添加一个连接属性 "shutdown=true"到JDBC连接URL以在最后一个连接到进程内数据库时强制快速关闭已关闭。但这主要对只读或测试数据库有用。完全关闭允许数据库在下次建立连接时快速打开。
查看指南http://hsqldb.org/doc/2.0/guide/running-chapt.html#rgc_inprocess
我就是在玩这种数据库,用完之后试过关闭HSQLDB连接,但是最后还是打开了。
代码:
//----This methods are in a specific connection class file
public static Connection conn = null;
public static Connection getConnection(){
try {
input = new FileInputStream("PathToMyPropertiesFile");
prop.load(input);
//The properties constants are correctly checked
Class.forName(prop.getProperty("DRIVER_HSQLDB"));
conn = DriverManager.getConnection(prop.getProperty("CONN_HSQLDB"));
}
catch(ClassNotFoundException | SQLException e) {
LOG.log(null,"Error: "+e);
}
catch (IOException ex) {
LOG.log(null,"FILE ERROR: "+ex);
}
finally {
if (input != null) {
try {
input.close();
} catch (Exception e) {
LOG.log(null,"CLOSE ERROR: "+e);
}
}
}
return conn;
}
public static boolean stopConn() {
try {
if(conn != null) {
conn.close();
System.err.println("\nCLOSE CONN\n"+conn);
return true;
}
}
catch (SQLException e) {
e.printStackTrace();
return false;
}
return false;
}
//========= the other class file with the methods to use the conneciton
public static boolean insertUser(String uName, String uEmail){
Connection con;
con = ConnectionDB.getConnection();
PreparedStatement ps = null;
try {
String consulta = "insert into USERS (\"NICK\",\"EMAIL\") VALUES(?,?);";
ps = con.prepareStatement(consulta);
System.err.println(ps);
ps.setString(1,uName);
ps.setString(2,uEmail);
System.err.println("\nASSIGNATION\n"+ps);
if(ps.executeUpdate() == 1) {
System.err.println("\nTRUE\n");
return true;
}
}
catch(SQLException e) {
e.printStackTrace();
}
finally {
try {
System.err.println("\nFINALLY\n"+ps);
if(ps != null) {
ps.close();
System.err.println("\nCLOSE PS\n"+ps);
}
if(con != null) {
con.close();
System.err.println("\nCLOSE CON\n"+con);
if(ConnectionDB.stopConn()) {
System.err.println("\nALL IS OK\n"+ConnectionDB.conn);
}
else {
System.err.println("\nMEEEEKKKK!!!\n"+ConnectionDB.conn);
}
}
}
}
return false;
}
控制台给我这个结果,我不知道为什么连接没有关闭,因为我尝试关闭它两次。如果有人有想法请告诉我。
org.hsqldb.jdbc.JDBCPreparedStatement@4501280b[sql=[insert into USERS ("NICK","EMAIL") VALUES(?,?);], parameters=[[null], [null]]]
ASSIGNATION org.hsqThis is my cldb.jdbc.JDBCPreparedStatement@4501280b[sql=[insert into USERS ("NICK","EMAIL") VALUES(?,?);], parameters=[[extra], [extra@mail.com]]]
TRUE
FINALLY org.hsqldb.jdbc.JDBCPreparedStatement@4501280b[sql=[insert into USERS ("NICK","EMAIL") VALUES(?,?);], parameters=[[extra], [extra@mail.com]]]
CLOSE PS org.hsqldb.jdbc.JDBCPreparedStatement@4501280b[closed]
CLOSE CON org.hsqldb.jdbc.JDBCConnection@3e5b87f5
CLOSE CONN org.hsqldb.jdbc.JDBCConnection@3e5b87f5
ALL IS OK org.hsqldb.jdbc.JDBCConnection@3e5b87f5
关闭 JDBC 连接不会关闭进程中的数据库。这允许您在应用程序运行期间打开和关闭不同的连接。
您需要执行 JDBC 语句来关闭数据库。要执行的 SQL 语句是 "SHUTDOWN".
可以添加一个连接属性 "shutdown=true"到JDBC连接URL以在最后一个连接到进程内数据库时强制快速关闭已关闭。但这主要对只读或测试数据库有用。完全关闭允许数据库在下次建立连接时快速打开。
查看指南http://hsqldb.org/doc/2.0/guide/running-chapt.html#rgc_inprocess