我正在使用以下代码为我的简单 jsp/servlet 登录项目(即单例)连接到数据库。当我第一次登录时,它在我成功注销后工作。现在问题开始了,当我再次尝试登录时出现错误提示“严重:错误消息:连接关闭后不允许操作。” 但是,当我删除代码关闭连接时,它又可以正常工作了。请建议我应该使用它还是避免它。
public class ConnectionMgr {
private static ConnectionMgr instance = null;
private static final String USERNAME = "root";
private static final String PASSWORD = "";
private static final String M_CONN_STRING = "jdbc:mysql://localhost:3306/generator";
private static final String H_CONN_STRING = "jdbc:hsqldb:data/generator";
private DBType dbType = DBType.MYSQL;
private Connection conn = null;
private ConnectionMgr() {
}
public static ConnectionMgr getInstance() {
if (instance == null) {
instance = new ConnectionMgr();
}
return instance;
}
public void setDBType(DBType dbType) {
this.dbType = dbType;
}
private boolean openConnection() {
try {
switch (dbType) {
case MYSQL:
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(M_CONN_STRING, USERNAME, PASSWORD);
return true;
case HSQL:
conn = DriverManager.getConnection(H_CONN_STRING, USERNAME, PASSWORD);
return true;
default:
return false;
}
} catch (SQLException | ClassNotFoundException e) {
System.err.println(e);
DBUtil.processException((SQLException) e);
return false;
}
}
public Connection getConnection() {
if (conn == null) {
if (openConnection()) {
System.out.println("Connection opened");
return conn;
} else {
return null;
}
}
return conn;
}
public void processException(SQLException e) {
System.err.println("Silgleton connection()Error -->");
System.err.println("Erroe message:" + e.getMessage());
System.err.println("Error code:" + e.getErrorCode());
System.err.println("Error State:" + e.getSQLState());
}
public void close() {
System.out.println("Closing connection");
try {
conn.close();
conn = null;
} catch (Exception e) {
}
}
}