连接池中InitialContext的问题
public class dbconf {
private static Connection conn;
public static Connection getconnection() throws SQLException {
Context ctx = null;
try {
//判断数据库连接是否关闭,如果没有关闭返回数据库连接对象 conn,否则创建数据库连接对象
if (conn != null && !conn.isClosed()) {
return conn;
}
ctx = new InitialContext();
if (ctx == null) {
throw new Exception("Boom - No Context");
}
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/TestDB");
if (ds != null) {
conn = ds.getConnection();
}
} catch (Exception e) {
e.printStackTrace();
// log.error("获取连接失败");
} finally {
if (ctx != null) {
try {
ctx.close();
} catch (NamingException ex) {
ex.printStackTrace();
}
}
}
return conn;
}
InitialContext是否需要关闭?
为什么在程序中看到数据库连接数为0?