From 96f94ba23f5fd33dce106d380b1a217a3ca3b7c2 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Wed, 14 Jul 2021 11:29:30 +0200 Subject: [PATCH] [jOOQ/jOOQ#12174] Unnecessary finalizer registration in DefaultConnectionProvider --- jOOQ/src/main/java/org/jooq/impl/DSL.java | 6 +- .../DefaultCloseableConnectionProvider.java | 68 +++++++++++++++++++ .../jooq/impl/DefaultCloseableDSLContext.java | 11 ++- .../jooq/impl/DefaultConnectionProvider.java | 15 ---- 4 files changed, 75 insertions(+), 25 deletions(-) create mode 100644 jOOQ/src/main/java/org/jooq/impl/DefaultCloseableConnectionProvider.java diff --git a/jOOQ/src/main/java/org/jooq/impl/DSL.java b/jOOQ/src/main/java/org/jooq/impl/DSL.java index fd1668c581..c4d634c489 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DSL.java +++ b/jOOQ/src/main/java/org/jooq/impl/DSL.java @@ -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); diff --git a/jOOQ/src/main/java/org/jooq/impl/DefaultCloseableConnectionProvider.java b/jOOQ/src/main/java/org/jooq/impl/DefaultCloseableConnectionProvider.java new file mode 100644 index 0000000000..277128340a --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/DefaultCloseableConnectionProvider.java @@ -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}. + *

+ * 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(); + } +} diff --git a/jOOQ/src/main/java/org/jooq/impl/DefaultCloseableDSLContext.java b/jOOQ/src/main/java/org/jooq/impl/DefaultCloseableDSLContext.java index 0dadddf19c..f751f11166 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DefaultCloseableDSLContext.java +++ b/jOOQ/src/main/java/org/jooq/impl/DefaultCloseableDSLContext.java @@ -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) { diff --git a/jOOQ/src/main/java/org/jooq/impl/DefaultConnectionProvider.java b/jOOQ/src/main/java/org/jooq/impl/DefaultConnectionProvider.java index 1cdb45f754..5682767df3 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DefaultConnectionProvider.java +++ b/jOOQ/src/main/java/org/jooq/impl/DefaultConnectionProvider.java @@ -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) // -------------------------------------------------------------------------