From 2b38a75c545461cd21ba42d6b757f64c43df5dec Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Sat, 27 Oct 2012 19:43:00 +0200 Subject: [PATCH] [#1899] Make some JDBC-related utility methods publicly available in org.jooq.tools.jdbc.JDBCUtils --- .../main/java/org/jooq/impl/CursorImpl.java | 3 +- .../org/jooq/impl/DefaultExecuteContext.java | 5 +- .../java/org/jooq/impl/FieldTypeHelper.java | 3 +- jOOQ/src/main/java/org/jooq/impl/Util.java | 79 +---------- .../java/org/jooq/tools/jdbc/JDBCUtils.java | 128 ++++++++++++++++++ 5 files changed, 138 insertions(+), 80 deletions(-) create mode 100644 jOOQ/src/main/java/org/jooq/tools/jdbc/JDBCUtils.java diff --git a/jOOQ/src/main/java/org/jooq/impl/CursorImpl.java b/jOOQ/src/main/java/org/jooq/impl/CursorImpl.java index 830bb232d9..0b8cd5df0a 100644 --- a/jOOQ/src/main/java/org/jooq/impl/CursorImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/CursorImpl.java @@ -70,6 +70,7 @@ import org.jooq.RecordMapper; import org.jooq.Result; import org.jooq.Table; import org.jooq.tools.jdbc.JDBC41ResultSet; +import org.jooq.tools.jdbc.JDBCUtils; /** * @author Lukas Eder @@ -223,7 +224,7 @@ class CursorImpl implements Cursor { @Override public final void close() { - Util.safeClose(rs); + JDBCUtils.safeClose(rs); rs = null; isClosed = true; } diff --git a/jOOQ/src/main/java/org/jooq/impl/DefaultExecuteContext.java b/jOOQ/src/main/java/org/jooq/impl/DefaultExecuteContext.java index e9f088a6f4..ad3092e256 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DefaultExecuteContext.java +++ b/jOOQ/src/main/java/org/jooq/impl/DefaultExecuteContext.java @@ -58,6 +58,7 @@ import org.jooq.ResultQuery; import org.jooq.Routine; import org.jooq.Truncate; import org.jooq.Update; +import org.jooq.tools.jdbc.JDBCUtils; /** * A default iplementation for the {@link ExecuteContext} @@ -133,7 +134,7 @@ class DefaultExecuteContext extends AbstractConfiguration implements ExecuteCont if (blobs != null) { for (Blob blob : blobs) { - Util.safeFree(blob); + JDBCUtils.safeFree(blob); } BLOBS.remove(); @@ -141,7 +142,7 @@ class DefaultExecuteContext extends AbstractConfiguration implements ExecuteCont if (clobs != null) { for (Clob clob : clobs) { - Util.safeFree(clob); + JDBCUtils.safeFree(clob); } CLOBS.remove(); diff --git a/jOOQ/src/main/java/org/jooq/impl/FieldTypeHelper.java b/jOOQ/src/main/java/org/jooq/impl/FieldTypeHelper.java index 426bce24c0..3aed92246c 100644 --- a/jOOQ/src/main/java/org/jooq/impl/FieldTypeHelper.java +++ b/jOOQ/src/main/java/org/jooq/impl/FieldTypeHelper.java @@ -77,6 +77,7 @@ import org.jooq.UDTRecord; import org.jooq.exception.SQLDialectNotSupportedException; import org.jooq.tools.Convert; import org.jooq.tools.JooqLogger; +import org.jooq.tools.jdbc.JDBCUtils; import org.jooq.types.DayToSecond; import org.jooq.types.UByte; import org.jooq.types.UInteger; @@ -147,7 +148,7 @@ public final class FieldTypeHelper { return (T) (blob == null ? null : blob.getBytes(1, (int) blob.length())); } finally { - Util.safeFree(blob); + JDBCUtils.safeFree(blob); } } else { diff --git a/jOOQ/src/main/java/org/jooq/impl/Util.java b/jOOQ/src/main/java/org/jooq/impl/Util.java index 159e83125a..f6c064e764 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Util.java +++ b/jOOQ/src/main/java/org/jooq/impl/Util.java @@ -46,13 +46,8 @@ import static org.jooq.tools.reflect.Reflect.accessible; import java.lang.reflect.Method; import java.lang.reflect.Modifier; -import java.sql.Blob; -import java.sql.Clob; import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; import java.sql.SQLException; -import java.sql.Statement; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -66,7 +61,6 @@ import javax.persistence.Entity; import org.jooq.ArrayRecord; import org.jooq.BindContext; import org.jooq.Configuration; -import org.jooq.Cursor; import org.jooq.DataType; import org.jooq.ExecuteContext; import org.jooq.ExecuteListener; @@ -86,6 +80,7 @@ import org.jooq.tools.Convert; import org.jooq.tools.LoggerListener; import org.jooq.tools.StopWatchListener; import org.jooq.tools.StringUtils; +import org.jooq.tools.jdbc.JDBCUtils; import org.jooq.tools.reflect.Reflect; /** @@ -660,9 +655,9 @@ final class Util { * Safely close a statement */ static final void safeClose(ExecuteListener listener, ExecuteContext ctx, boolean keepStatement) { - safeClose(ctx.resultSet()); + JDBCUtils.safeClose(ctx.resultSet()); if (!keepStatement) - safeClose(ctx.statement()); + JDBCUtils.safeClose(ctx.statement()); // [#1868] TODO: This needs to be called in fetchLazy(), too listener.end(ctx); @@ -671,74 +666,6 @@ final class Util { DefaultExecuteContext.clean(); } - /** - * Safely close a statement - */ - static final void safeClose(Statement statement) { - if (statement != null) { - try { - statement.close(); - } - catch (Exception ignore) {} - } - } - - /** - * Safely close a result set - */ - static final void safeClose(ResultSet resultSet) { - if (resultSet != null) { - try { - resultSet.close(); - } - catch (Exception ignore) {} - } - } - - /** - * Safely close a cursor - */ - static final void safeClose(Cursor cursor) { - if (cursor != null) { - try { - cursor.close(); - } - catch (Exception ignore) {} - } - } - - /** - * Safely close a result set and / or a statement - */ - static final void safeClose(ResultSet resultSet, PreparedStatement statement) { - safeClose(resultSet); - safeClose(statement); - } - - /** - * Safely free a blob - */ - static final void safeFree(Blob blob) { - if (blob != null) { - try { - blob.free(); - } - catch (Exception ignore) {} - } - } - - /** - * Safely free a clob - */ - static final void safeFree(Clob clob) { - if (clob != null) { - try { - clob.free(); - } - catch (Exception ignore) {} - } - } - /** * Extract an underlying connection */ diff --git a/jOOQ/src/main/java/org/jooq/tools/jdbc/JDBCUtils.java b/jOOQ/src/main/java/org/jooq/tools/jdbc/JDBCUtils.java new file mode 100644 index 0000000000..348111c0b8 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/tools/jdbc/JDBCUtils.java @@ -0,0 +1,128 @@ +/** + * Copyright (c) 2009-2012, Lukas Eder, lukas.eder@gmail.com + * All rights reserved. + * + * This software is licensed to you under the Apache License, Version 2.0 + * (the "License"); You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * . Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * . Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * . Neither the name "jOOQ" nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package org.jooq.tools.jdbc; + +import java.sql.Blob; +import java.sql.Clob; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.Statement; + +/** + * JDBC-related utility methods + * + * @author Lukas Eder + */ +public class JDBCUtils { + + /** + * Safely close a statement + *

+ * This method will silently ignore if statement is + * null, or if {@link Statement#close()} throws an exception. + */ + public static final void safeClose(Statement statement) { + if (statement != null) { + try { + statement.close(); + } + catch (Exception ignore) {} + } + } + + /** + * Safely close a result set + *

+ * This method will silently ignore if resultSet is + * null, or if {@link ResultSet#close()} throws an exception. + */ + public static final void safeClose(ResultSet resultSet) { + if (resultSet != null) { + try { + resultSet.close(); + } + catch (Exception ignore) {} + } + } + + /** + * Safely close a result set and / or a statement + *

+ * This method will silently ignore if resultSet or + * statement is null, or if + * {@link ResultSet#close()} or {@link Statement#close()} throws an + * exception. + */ + public static final void safeClose(ResultSet resultSet, PreparedStatement statement) { + safeClose(resultSet); + safeClose(statement); + } + + /** + * Safely free a blob + *

+ * This method will silently ignore if blob is + * null, or if {@link Blob#free()} throws an exception. + */ + public static final void safeFree(Blob blob) { + if (blob != null) { + try { + blob.free(); + } + catch (Exception ignore) {} + } + } + + /** + * Safely free a clob + *

+ * This method will silently ignore if clob is + * null, or if {@link Clob#free()} throws an exception. + */ + public static final void safeFree(Clob clob) { + if (clob != null) { + try { + clob.free(); + } + catch (Exception ignore) {} + } + } + + /** + * No instances + */ + private JDBCUtils() {} +}