Hello community,
Following up on the various discussions on this user group, I have now
added org.jooq.impl.Factory constructors taking a DataSource instead
of a Connection as an argument. This will be included in jOOQ 2.4.0:
https://sourceforge.net/apps/trac/jooq/ticket/1424
Internally, jOOQ now wraps all relevant JDBC objects, such that this
distinction (DataSource-mode, Connection-mode) can be abstracted to
all of jOOQ's internals, providing full backwards-compatibility.
Essentially, this was done:
- A DataSourceConnection (DSC) was created to wrap both DataSource and
Connection.
- If the wrapped Connection in DSC is null, the wrapped DataSource is
used to create one
- DSC.createStatement(), DSC.prepareStatement() and DSC.prepareCall()
methods return DataSourceStatement (DSS), DataSourcePreparedStatement
(DSPS), and DataSourcePreparedCall (DSPC)
- When DSS, DSPS, and DSPC are closed, they also close the underlying
DSC, if DSC contains a Connection created from a DataSource
The above design allows jOOQ to lazy-fetch a Connection from a
DataSource when needed AND to return (close) that Connection again, as
soon as any Statement is closed. Since jOOQ always closes Statements
after execution, this implementation will take care of correctly
handling a DataSource-driven Connection's lifecycle, while leaving
standalone Connections untouched.
This solution works in integration tests as well as in a
Spring-enabled test setup that Sergey provided me with, some months
ago (sample Spring configuration file is attached). It is available
from GitHub and as 2.4.0-SNAPSHOT here:
https://oss.sonatype.org/content/repositories/snapshots/org/jooq/
Early end-user tests are very welcome!
Cheers
Lukas