这就是我实现我想要的每个jooq查询的方法.
UtilClass{
//one per table more or less
static void methodA(){
//my method
Connection con = MySQLConnection.getConexion(); //open
DSLContext create = DSL.using(con, SQLDialect.MYSQL); //open
/* my logic and jooq querys */ //The code !!!!!!!
try {
if ( con != null )
con.close(); //close
} catch (SQLException e) {
} //close
con=null; //close
create=null; //close
}
}
Run Code Online (Sandbox Code Playgroud)
我在这里过度工作吗?/保持上下文和连接打开是否安全?
如果保持打开是安全的,我宁愿使用1个静态字段DSLContext per UtilClass(并且只有评论的部分将在我的方法上).我将为每个UtilClass打开一个连接,因为我封装了每个表的方法(或多或少).
Luk*_*der 19
DSLContext 通常不是资源,所以你可以让它"开放",即你可以让垃圾收集器为你收集它.
Connection但是,JDBC 是一种资源,作为所有资源,您应该始终显式关闭它.在Java 7+中关闭资源的正确方法是使用try-with-resources语句:
static void methodA() {
try (Connection con = MySQLConnection.getConexion()) {
DSLContext ctx = DSL.using(con, SQLDialect.MYSQL); //open
/* my logic and jooq queries */
// "ctx" goes out of scope here, and can be garbage-collected
} // "con" will be closed here by the try-with-resources statement
}
Run Code Online (Sandbox Code Playgroud)
有关try-with-resources语句的更多信息,请参见此处.另请注意,在使用独立JDBC连接时,jOOQ教程使用try-with-resources语句.
DSLContext资源?上述情况的一个例外是当您让DSLContext实例管理Connection自身时,例如通过传递连接URL,如下所示:
try (DSLContext ctx = DSL.using("jdbc:url:something", "username", "password")) {
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,你需要close()在DSLContext如上图所示
| 归档时间: |
|
| 查看次数: |
5397 次 |
| 最近记录: |