[#1899] Make some JDBC-related utility methods publicly available in

org.jooq.tools.jdbc.JDBCUtils
This commit is contained in:
Lukas Eder 2012-10-27 19:43:00 +02:00
parent 36c98e4cc4
commit 2b38a75c54
5 changed files with 138 additions and 80 deletions

View File

@ -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<R extends Record> implements Cursor<R> {
@Override
public final void close() {
Util.safeClose(rs);
JDBCUtils.safeClose(rs);
rs = null;
isClosed = true;
}

View File

@ -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();

View File

@ -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 {

View File

@ -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
*/

View File

@ -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
* <p>
* This method will silently ignore if <code>statement</code> is
* <code>null</code>, 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
* <p>
* This method will silently ignore if <code>resultSet</code> is
* <code>null</code>, 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
* <p>
* This method will silently ignore if <code>resultSet</code> or
* <code>statement</code> is <code>null</code>, 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
* <p>
* This method will silently ignore if <code>blob</code> is
* <code>null</code>, 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
* <p>
* This method will silently ignore if <code>clob</code> is
* <code>null</code>, 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() {}
}