[jOOQ/jOOQ#12174] Unnecessary finalizer registration in

DefaultConnectionProvider
This commit is contained in:
Lukas Eder 2021-07-14 11:29:30 +02:00
parent 6f7c733014
commit 96f94ba23f
4 changed files with 75 additions and 25 deletions

View File

@ -558,7 +558,7 @@ public class DSL {
else {
try {
Connection connection = DriverManager.getConnection(url);
return new DefaultCloseableDSLContext(new DefaultConnectionProvider(connection, true), JDBCUtils.dialect(connection));
return new DefaultCloseableDSLContext(new DefaultCloseableConnectionProvider(connection), JDBCUtils.dialect(connection));
}
catch (SQLException e) {
throw Tools.translate("Error when initialising Connection", e);
@ -599,7 +599,7 @@ public class DSL {
else {
try {
Connection connection = DriverManager.getConnection(url, username, password);
return new DefaultCloseableDSLContext(new DefaultConnectionProvider(connection, true), JDBCUtils.dialect(connection));
return new DefaultCloseableDSLContext(new DefaultCloseableConnectionProvider(connection), JDBCUtils.dialect(connection));
}
catch (SQLException e) {
throw Tools.translate("Error when initialising Connection", e);
@ -639,7 +639,7 @@ public class DSL {
else {
try {
Connection connection = DriverManager.getConnection(url, properties);
return new DefaultCloseableDSLContext(new DefaultConnectionProvider(connection, true), JDBCUtils.dialect(connection));
return new DefaultCloseableDSLContext(new DefaultCloseableConnectionProvider(connection), JDBCUtils.dialect(connection));
}
catch (SQLException e) {
throw Tools.translate("Error when initialising Connection", e);

View File

@ -0,0 +1,68 @@
/*
* 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 java.sql.Connection;
import org.jooq.tools.jdbc.JDBCUtils;
/**
* An extension of the {@link DefaultConnectionProvider}.
* <p>
* This implementation is used by {@link DefaultCloseableDSLContext} to
* implement the {@link AutoCloseable} semantics, which includes closing
* underlying connections on {@link Object#finalize()}.
*
* @author Lukas Eder
*/
class DefaultCloseableConnectionProvider extends DefaultConnectionProvider {
DefaultCloseableConnectionProvider(Connection connection) {
super(connection);
}
// -------------------------------------------------------------------------
// XXX: ConnectionProvider API
// -------------------------------------------------------------------------
@Override
protected void finalize() throws Throwable {
JDBCUtils.safeClose(connection);
super.finalize();
}
}

View File

@ -76,13 +76,10 @@ public class DefaultCloseableDSLContext extends DefaultDSLContext implements Clo
ConnectionProvider cp = configuration().connectionProvider();
ConnectionFactory cf = configuration().connectionFactory();
if (cp instanceof DefaultConnectionProvider) {
DefaultConnectionProvider dcp = (DefaultConnectionProvider) cp;
if (dcp.finalize) {
JDBCUtils.safeClose(dcp.connection);
dcp.connection = null;
}
if (cp instanceof DefaultCloseableConnectionProvider) {
DefaultConnectionProvider dcp = (DefaultCloseableConnectionProvider) cp;
JDBCUtils.safeClose(dcp.connection);
dcp.connection = null;
}
if (cf instanceof DefaultConnectionFactory) {

View File

@ -44,7 +44,6 @@ import java.sql.Savepoint;
import org.jooq.ConnectionProvider;
import org.jooq.exception.DataAccessException;
import org.jooq.tools.JooqLogger;
import org.jooq.tools.jdbc.JDBCUtils;
import org.jetbrains.annotations.NotNull;
@ -64,15 +63,9 @@ public class DefaultConnectionProvider implements ConnectionProvider {
private static final JooqLogger log = JooqLogger.getLogger(DefaultConnectionProvider.class);
Connection connection;
final boolean finalize;
public DefaultConnectionProvider(Connection connection) {
this(connection, false);
}
DefaultConnectionProvider(Connection connection, boolean finalize) {
this.connection = connection;
this.finalize = finalize;
}
// -------------------------------------------------------------------------
@ -88,14 +81,6 @@ public class DefaultConnectionProvider implements ConnectionProvider {
@Override
public final void release(Connection released) {}
@Override
protected void finalize() throws Throwable {
if (finalize)
JDBCUtils.safeClose(connection);
super.finalize();
}
// -------------------------------------------------------------------------
// XXX: Original DSLContext/Factory API (JDBC utility methods)
// -------------------------------------------------------------------------