[jOOQ/jOOQ#10512] Add a CloseableDSLContext and stop subtyping AutoCloseable from DSLContext directly

This commit is contained in:
Lukas Eder 2020-08-21 11:31:41 +02:00
parent afa0ce5748
commit 441f7ccc25
5 changed files with 165 additions and 50 deletions

View File

@ -0,0 +1,75 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq;
import java.sql.Connection;
import java.util.Properties;
import org.jooq.exception.DataAccessException;
import org.jooq.impl.DSL;
/**
* A resourceful {@link DSLContext} that should be closed in a
* try-with-resources statement.
*/
public interface CloseableDSLContext extends DSLContext , AutoCloseable {
// -------------------------------------------------------------------------
// 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.
*/
@Override
void close() throws DataAccessException;
}

View File

@ -81,7 +81,6 @@ import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;
import java.util.function.BiFunction;
@ -143,30 +142,7 @@ import org.jetbrains.annotations.Nullable;
* @see Configuration
* @author Lukas Eder
*/
public interface DSLContext extends Scope , AutoCloseable {
// -------------------------------------------------------------------------
// 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.
*/
@Override
void close() throws DataAccessException;
public interface DSLContext extends Scope {
// -------------------------------------------------------------------------
// XXX Configuration API

View File

@ -152,6 +152,7 @@ import org.jooq.CaseConditionStep;
import org.jooq.CaseValueStep;
import org.jooq.Catalog;
import org.jooq.CharacterSet;
import org.jooq.CloseableDSLContext;
import org.jooq.Collation;
import org.jooq.Comment;
import org.jooq.CommentOnIsStep;
@ -520,10 +521,10 @@ public class DSL {
* @see JDBCUtils#dialect(String)
*/
@NotNull
public static DSLContext using(String url) {
public static CloseableDSLContext using(String url) {
try {
Connection connection = DriverManager.getConnection(url);
return using(new DefaultConnectionProvider(connection, true), JDBCUtils.dialect(connection));
return new DefaultCloseableDSLContext(new DefaultConnectionProvider(connection, true), JDBCUtils.dialect(connection));
}
catch (SQLException e) {
throw Tools.translate("Error when initialising Connection", e);
@ -552,10 +553,10 @@ public class DSL {
* @see JDBCUtils#dialect(String)
*/
@NotNull
public static DSLContext using(String url, String username, String password) {
public static CloseableDSLContext using(String url, String username, String password) {
try {
Connection connection = DriverManager.getConnection(url, username, password);
return using(new DefaultConnectionProvider(connection, true), JDBCUtils.dialect(connection));
return new DefaultCloseableDSLContext(new DefaultConnectionProvider(connection, true), JDBCUtils.dialect(connection));
}
catch (SQLException e) {
throw Tools.translate("Error when initialising Connection", e);
@ -583,10 +584,10 @@ public class DSL {
* @see JDBCUtils#dialect(String)
*/
@NotNull
public static DSLContext using(String url, Properties properties) {
public static CloseableDSLContext using(String url, Properties properties) {
try {
Connection connection = DriverManager.getConnection(url, properties);
return using(new DefaultConnectionProvider(connection, true), JDBCUtils.dialect(connection));
return new DefaultCloseableDSLContext(new DefaultConnectionProvider(connection, true), JDBCUtils.dialect(connection));
}
catch (SQLException e) {
throw Tools.translate("Error when initialising Connection", e);

View File

@ -0,0 +1,82 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq.impl;
import org.jooq.CloseableDSLContext;
import org.jooq.ConnectionProvider;
import org.jooq.SQLDialect;
import org.jooq.conf.Settings;
import org.jooq.tools.jdbc.JDBCUtils;
/**
* An extension of {@link DefaultDSLContext} that implements also the
* {@link CloseableDSLContext} contract.
*/
public class DefaultCloseableDSLContext extends DefaultDSLContext implements CloseableDSLContext {
/**
* Generated UID
*/
private static final long serialVersionUID = -6315025485115195218L;
public DefaultCloseableDSLContext(ConnectionProvider connectionProvider, SQLDialect dialect, Settings settings) {
super(connectionProvider, dialect, settings);
}
public DefaultCloseableDSLContext(ConnectionProvider connectionProvider, SQLDialect dialect) {
super(connectionProvider, dialect);
}
// -------------------------------------------------------------------------
// 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;
}
}
}
}

View File

@ -279,7 +279,6 @@ import org.jooq.exception.InvalidResultException;
import org.jooq.exception.SQLDialectNotSupportedException;
import org.jooq.impl.BatchCRUD.Action;
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;
@ -343,24 +342,6 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri
super(configuration, configuration == null ? null : configuration.data());
}
// -------------------------------------------------------------------------
// 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
// -------------------------------------------------------------------------