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
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <tx:annotation-driven transaction-manager="transactionManager"/> <jdbc:embedded-database id="dataSource" type="HSQL"> <jdbc:script location="classpath:db-init.sql"/> </jdbc:embedded-database> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <bean id="transactionAwareDataSource" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy"> <constructor-arg ref="dataSource"/> </bean> <bean id="factoryProxy" class="org.jooq.impl.Factory"> <constructor-arg name="datasource" ref="transactionAwareDataSource"/> <constructor-arg name="dialect"> <value type="org.jooq.SQLDialect">HSQLDB</value> </constructor-arg> </bean> <context:component-scan base-package="org.jooq.util.spring"/> </beans>
