我是否需要在每个读取/写入数据库的方法中获取上下文和DataSource? (下面的示例)或Play.db.DB
的getConnection()方法是否透明地处理池?
public void saveResponse() {
try {
InitialContext ctx = new InitialContext();
DataSource ds = (DataSource) ctx
.lookup("java:comp/env/jdbc/MySQLDB");
conn = ds.getConnection();
stmt = conn.createStatement();
stmt.execute("SOME SQL QUERY");
stmt.close();
stmt = null;
conn.close();
conn = null;
} catch (NamingException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
答案 0 :(得分:1)