Reputation: 13869
Do I need to obtain a context and DataSource in every method that reads/writes to the DB? (Sample below) Or does Play.db.DB
's getConnection() method handle the pool transparently?
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();
}
}
Upvotes: 3
Views: 2648
Reputation: 284
Play can manage that for you.
But why don't you use Play's JPA persistence? http://www.playframework.com/documentation/1.2.5/jpa
That would be even more transparent than getting connections.
Upvotes: 1