JDBC连接意外关闭

时间:2019-05-15 09:06:27

标签: java postgresql jdbc jooq

我不时收到以下错误,我不知道为什么:

org.jooq.exception.DataAccessException: SQL [select "public"."RefreshTokens"."userId" from "public"."RefreshTokens" where "public"."RefreshTokens"."token" = ?]; This connection has been closed.

这是堆栈跟踪中最重要的部分:

      {
        "class" : "org.postgresql.jdbc.PgConnection",
        "method" : "checkClosed",
        "file" : "PgConnection.java",
        "line" : 783
      }, {
        "class" : "org.postgresql.jdbc.PgConnection",
        "method" : "prepareStatement",
        "file" : "PgConnection.java",
        "line" : 1680
      }, {
        "class" : "org.postgresql.jdbc.PgConnection",
        "method" : "prepareStatement",
        "file" : "PgConnection.java",
        "line" : 371
      }, {
        "class" : "org.jooq.impl.ProviderEnabledConnection",
        "method" : "prepareStatement",
        "file" : "ProviderEnabledConnection.java",
        "line" : 109
      }, {
        "class" : "org.jooq.impl.SettingsEnabledConnection",
        "method" : "prepareStatement",
        "file" : "SettingsEnabledConnection.java",
        "line" : 73
      }, {
        "class" : "org.jooq.impl.AbstractResultQuery",
        "method" : "prepare",
        "file" : "AbstractResultQuery.java",
        "line" : 239
      }, {
        "class" : "org.jooq.impl.AbstractQuery",
        "method" : "execute",
        "file" : "AbstractQuery.java",
        "line" : 322
      }, {
        "class" : "org.jooq.impl.AbstractResultQuery",
        "method" : "fetchLazy",
        "file" : "AbstractResultQuery.java",
        "line" : 393
      }, {
        "class" : "org.jooq.impl.AbstractResultQuery",
        "method" : "fetchLazy",
        "file" : "AbstractResultQuery.java",
        "line" : 380
      }, {
        "class" : "org.jooq.impl.AbstractResultQuery",
        "method" : "fetchOne",
        "file" : "AbstractResultQuery.java",
        "line" : 545
      }, {
        "class" : "org.jooq.impl.SelectImpl",
        "method" : "fetchOne",
        "file" : "SelectImpl.java",
        "line" : 2879
      }

我将JOOQ(3.11.11)与Postgres(42.2.5.jre7)结合使用,然后创建连接和DSLContext,如下所示:

val conn = DriverManager.getConnection(
            config.databaseUrl, config.databaseUser, config.databasePassword)
DSL.using(conn, SQLDialect.POSTGRES_10)

我认为JOOQ会为我管理连接,而我不必手动重新打开/打开/关闭它们。我是否应该使用DataSource而不是DriverManager之类的东西让JOOQ自动处理连接状态? 如果有人能告诉我为什么实际上断开连接,那就太好了。

1 个答案:

答案 0 :(得分:1)

虽然a也可以在某种程度上管理您的连接(使用JOOQ,但通常将其委托给基础的DSL#using(String url, String username, String password)ConnectionProvider

IMO,最好的方法是将DataSource(作为使用连接器的 executor )和您的连接管理器(作为监视连接的 manager )完全分离打开和关闭)。

一种方法是使用JOOQDataSourceorg.jooq.Configuration#derive(DataSource dataSource),如下所示:

DSL#using(Configuration configuration)

另请参阅DSL javadocsConfiguration javadocsthis答案。