简体   繁体   English

JDBC连接意外关闭

[英]JDBC connection closes unexpectedly

I'm getting the following error from time to time and I can't figure out why: 我不时收到以下错误,我不知道为什么:

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

Here is the most important part of the stacktrace: 这是stacktrace中最重要的部分:

      {
        "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
      }

I'm using JOOQ (3.11.11) with Postgres (42.2.5.jre7) and I create my connection and DSLContext like this: 我将JOOQ(3.11.11)与Postgres(42.2.5.jre7)结合使用, DSLContext如下方式创建连接和DSLContext

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

I thought that JOOQ would manage the connections for me and that I don't have to reopen/open/close them manually. 我以为JOOQ会为我管理连接,而我不必手动重新打开/打开/关闭它们。 Am I supposed to use something like DataSource instead of DriverManager for JOOQ to automatically handle the connection state? 我是否应该使用DataSource代替DriverManager for JOOQ来自动处理连接状态? And it would be great if someone could tell me why the connection actually gets dropped. 如果有人能告诉我为什么实际上断开连接,那就太好了。

While JOOQ can somewhat manage your connections as well (using DSL#using(String url, String username, String password) , it usually delegates to the underlying ConnectionProvider or DataSource . 尽管JOOQ也可以某种程度上管理您的连接(使用DSL#using(String url, String username, String password) ,但它通常委托给基础ConnectionProviderDataSource

IMO, the best approach is to completely decouple JOOQ (as the executor using the connections) and your connection manager (as the manager that oversees connection opening and closing). IMO,最好的方法是将JOOQ (作为使用连接的执行者 )和您的连接管理器(作为监督连接打开和关闭的管理器 )完全分离。

One way to do this is using a DataSource , org.jooq.Configuration#derive(DataSource dataSource) and DSL#using(Configuration configuration) as follows: 一种方法是使用DataSourceorg.jooq.Configuration#derive(DataSource dataSource)DSL#using(Configuration configuration) ,如下所示:

javax.sql.DataSource ds = ...; // initialize DataSource
org.jooq.Configuration configuration = Configuration.derive(ds);
DSLContext dslContext = DSL.using(configuration);

Record r = dslContext.selectFrom(...).where.(...).fetchOne();

See also the DSL javadocs as well as the Configuration javadocs as well as this answer. 另请参见DSL javadocsConfiguration javadocs答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM