Reputation: 2579
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:
{
"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:
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. Am I supposed to use something like DataSource
instead of DriverManager
for JOOQ to automatically handle the connection state?
And it would be great if someone could tell me why the connection actually gets dropped.
Upvotes: 0
Views: 429
Reputation: 3604
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
.
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).
One way to do this is using a DataSource
, org.jooq.Configuration#derive(DataSource dataSource)
and DSL#using(Configuration configuration)
as follows:
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.
Upvotes: 1