Reputation: 743
Recently started using play 2.1.x. I wonder what is the best way of executing a raw sql query once per request? I use postgres and need to specify what schema - "set search_path to..."-type of query once per request.
I configured postgres with jdbc and it connects fine, found some old comment that play.db.DB is supposed to have an executeQuery method but it doesn't anymore?
To run code for each request I have overridden the "onRequest" method of the GlobalSettings class:
@Override
public Action onRequest(Request request, Method actionMethod) {
//play.api.db.DB.executeQuery(); // -- This does not exist?
return super.onRequest(request, actionMethod);
}
I don't know if this is the best way todo it either, any help would be appreciated. I can't change the underlying architecture, I do need to set the search path to schema =)
Upvotes: 3
Views: 3886
Reputation: 1230
You are mistakenly looking at the scala docs I think. Check these:
http://www.playframework.com/documentation/2.1.3/api/java/index.html
http://www.playframework.com/documentation/2.1.3/JavaDatabase
You probably want to use play.db.DB.getConnection(), which returns a java.sql.Connection. Then you have the standard java api at your disposal.
String sql = "...";
Connection conn = play.db.DB.getConnection();
try {
Statement stmt = conn.createStatement();
try {
stmt.execute(sql)
} finally {
stmt.close();
}
} finally {
conn.close();
}
Upvotes: 5