[#4479] DSLContext should implement AutoCloseable, in case it was constructed via DSL.using(String)
This commit is contained in:
parent
a90a3bc459
commit
010befc2c9
@ -71,6 +71,7 @@ import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Properties;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import javax.annotation.Generated;
|
||||
@ -121,7 +122,30 @@ import org.jooq.tools.jdbc.MockRunnable;
|
||||
* @see Configuration
|
||||
* @author Lukas Eder
|
||||
*/
|
||||
public interface DSLContext extends Scope {
|
||||
public interface DSLContext extends Scope /* [java-8] */, AutoCloseable /* [/java-8] */ {
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// XXX AutoCloseable API
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Close the underlying resources, if any resources have been allocated when
|
||||
* constructing this <code>DSLContext</code>.
|
||||
* <p>
|
||||
* Some {@link DSLContext} constructors, such as {@link DSL#using(String)},
|
||||
* {@link DSL#using(String, Properties)}, or
|
||||
* {@link DSL#using(String, String, String)} allocate a {@link Connection}
|
||||
* resource, which is inaccessible to the outside of the {@link DSLContext}
|
||||
* implementation. Proper resource management must thus be done via this
|
||||
* {@link #close()} method.
|
||||
*
|
||||
* @throws DataAccessException When something went wrong closing the
|
||||
* underlying resources.
|
||||
*/
|
||||
/* [java-8] */
|
||||
@Override
|
||||
/* [/java-8] */
|
||||
void close() throws DataAccessException;
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// XXX Configuration API
|
||||
|
||||
@ -65,7 +65,7 @@ public class DefaultConnectionProvider implements ConnectionProvider {
|
||||
|
||||
private static final JooqLogger log = JooqLogger.getLogger(DefaultConnectionProvider.class);
|
||||
Connection connection;
|
||||
private boolean finalize;
|
||||
final boolean finalize;
|
||||
|
||||
public DefaultConnectionProvider(Connection connection) {
|
||||
this(connection, false);
|
||||
|
||||
@ -212,6 +212,7 @@ import org.jooq.exception.SQLDialectNotSupportedException;
|
||||
import org.jooq.impl.BatchCRUD.Action;
|
||||
import org.jooq.tools.JooqLogger;
|
||||
import org.jooq.tools.csv.CSVReader;
|
||||
import org.jooq.tools.jdbc.JDBCUtils;
|
||||
import org.jooq.tools.jdbc.MockCallable;
|
||||
import org.jooq.tools.jdbc.MockConfiguration;
|
||||
import org.jooq.tools.jdbc.MockDataProvider;
|
||||
@ -279,6 +280,24 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri
|
||||
super(configuration);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// XXX AutoCloseable
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
ConnectionProvider cp = configuration().connectionProvider();
|
||||
|
||||
if (cp instanceof DefaultConnectionProvider) {
|
||||
DefaultConnectionProvider dcp = (DefaultConnectionProvider) cp;
|
||||
|
||||
if (dcp.finalize) {
|
||||
JDBCUtils.safeClose(dcp.connection);
|
||||
dcp.connection = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// XXX Configuration API
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
Loading…
Reference in New Issue
Block a user