【问题标题】:Play framework execute raw sql at start of request?播放框架在请求开始时执行原始 sql?
【发布时间】:2013-08-31 08:37:52
【问题描述】:

最近开始使用 play 2.1.x。我想知道每个请求执行一次原始 sql 查询的最佳方法是什么?我使用 postgres 并且需要指定什么模式 - “set search_path to...” - 每个请求一次的查询类型。

我用 jdbc 配置了 postgres,它连接正常,发现一些旧评论 play.db.DB 应该有一个 executeQuery 方法,但现在没有了?

为了为每个请求运行代码,我重写了 GlobalSettings 类的“onRequest”方法:

    @Override
public Action onRequest(Request request, Method actionMethod) {
   //play.api.db.DB.executeQuery(); // -- This does not exist?
   return super.onRequest(request, actionMethod);
}

我也不知道这是否是最好的方法,任何帮助将不胜感激。我无法更改底层架构,我确实需要将搜索路径设置为 schema =)

【问题讨论】:

    标签: java sql postgresql playframework playframework-2.0


    【解决方案1】:

    您错误地查看了我认为的 scala 文档。检查这些:

    http://www.playframework.com/documentation/2.1.3/api/java/index.html

    http://www.playframework.com/documentation/2.1.3/JavaDatabase

    您可能想要使用 play.db.DB.getConnection(),它返回一个 java.sql.Connection。然后你就可以使用标准的 java api了。

    String sql = "...";
    
    Connection conn = play.db.DB.getConnection();
    try {
        Statement stmt = conn.createStatement();
        try {
            stmt.execute(sql)
        } finally {
            stmt.close();
        }
    } finally {
        conn.close();
    }
    

    【讨论】:

      猜你喜欢
      • 2012-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-16
      • 1970-01-01
      • 1970-01-01
      • 2017-06-29
      • 1970-01-01
      相关资源
      最近更新 更多