Here is the code of a class which I am writing which uses JooQ 3.7.0 (the irrelevant parts have been stripped); note the uses of the AutoCloseable
feature of DSLContext
:
public final class JooqPerMethodMetricWriter
implements PerMethodMetricWriter
{
private static final Logger LOGGER
= LoggerFactory.getLogger(JooqPerMethodMetricWriter.class);
// [snip]
private final DSLContext jooq;
public JooqPerMethodMetricWriter(final Connection connection,
final Instant instant)
throws IOException
{
// [snip]
jooq = DSL.using(connection);
}
private void writeCsv(final Configuration configuration)
{
// [snip]
try (
final DSLContext context = DSL.using(configuration);
final Reader reader = Files.newBufferedReader(csvPath);
) {
final Loader<PermethodMetricsRecord> loader = context
.loadInto(PERMETHOD_METRICS)
.loadCSV(reader)
.fields(PERMETHOD_METRICS.fields())
.execute();
LOGGER.info("{} lines stored in database", loader.stored());
} catch (IOException e) {
throw new RuntimeException("Cannot open CSV for reading", e);
}
// BREAKPOINT 1
}
@Override
public void close()
throws IOException
{
jooq.transaction(this::writeCsv);
jooq.close();
// BREAKPOINT 2
Files.delete(csvPath);
}
// [snip]
}
If relevant at all, the database used is PostgreSQL (9.4.x).
In the code above, I have two breakpoints. When I debug, I see that:
- at the first breakpoint,
configuration.connectionProvider().acquire().isClosed()
is false... - at the second breakpoint,
jooq.configuration().connectionProvider().acquire().isClosed()
is also false.
I'm confused. What happened to the Connection
which I have received as a constructor parameter? Do I need to .close()
it myself?
Side question, this time with regards to the Loader
: I leave the defaults, therefore .commitNone()
; given that I run the loader within a transacation, would it make a difference at all if I tried and .commit<somethingElse>()
instead, for instance .commitAfter(1000)
?
DSLContext
becameAutoCloseable
with the Java 8 distributions of jOOQ 3.7. TheDSLContext.close()
method's Javadoc explains the semantics of thisclose()
call:Only resources allocated when you constructed the
DSLContext
will be released. Not resources that you passed to theDSLContext
. In your case, you are not allocating any resources in thistry-with-resources
statement, so there is nothing to be released at the end of it:This would be different if you allocated a new
Connection
right there:Regarding your questions:
Nothing. You must govern its lifecycle yourself as jOOQ doesn't know anything about your connection lifecycle strategy.
Yes.