com.boylesoftware.cb2.util.sql
Class ConnectionWrapper

java.lang.Object
  extended bycom.boylesoftware.cb2.util.sql.ConnectionWrapper
All Implemented Interfaces:
java.sql.Connection

public class ConnectionWrapper
extends java.lang.Object
implements java.sql.Connection

Wrapper around a JDBC's Connection interface implementation. You can derive your connection wrapper classes from this one and override some of its methods, while all the others will be calling appropriate methods on the wrapped, "real" connection.

This class also provides some "hook" methods called in certain situations. Note that if your redefine interface methods in your subclass and you use hooks you will have to call the base method from your redefined one in order to make the hooks invoked. Alternatively you call them yourself directly from the redefined interface method.

Version:
$Id: ConnectionWrapper.java,v 1.3 2003/10/01 22:02:36 levahim Exp $
Author:
Lev Himmelfarb

Field Summary
protected  java.sql.Connection con
          The wrapped connection.
 
Fields inherited from interface java.sql.Connection
TRANSACTION_NONE, TRANSACTION_READ_COMMITTED, TRANSACTION_READ_UNCOMMITTED, TRANSACTION_REPEATABLE_READ, TRANSACTION_SERIALIZABLE
 
Constructor Summary
ConnectionWrapper()
          Creates a new wrapper.
ConnectionWrapper(java.sql.Connection con)
          Creates a new wrapper and sets the wrapped connection to the specified one.
 
Method Summary
 void clearWarnings()
          Interface method wrapper.
 void close()
          Interface method wrapper.
 void commit()
          Interface method wrapper.
 java.sql.Statement createStatement()
          Interface method wrapper.
 java.sql.Statement createStatement(int resultSetType, int resultSetConcurrency)
          Interface method wrapper.
 java.sql.Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability)
          Interface method wrapper.
protected  java.sql.Statement createStatementHook(java.sql.Statement stmt, java.lang.String sql)
          This hook method is called from all the methods that create all kinds of database statements, including prepared and callable statements.
protected  void finalize()
          Called by the garbage collector.
 boolean getAutoCommit()
          Interface method wrapper.
 java.lang.String getCatalog()
          Interface method wrapper.
 int getHoldability()
          Interface method wrapper.
 java.sql.DatabaseMetaData getMetaData()
          Interface method wrapper.
 int getTransactionIsolation()
          Interface method wrapper.
 java.util.Map getTypeMap()
          Interface method wrapper.
 java.sql.SQLWarning getWarnings()
          Interface method wrapper.
 java.sql.Connection getWrappedConnection()
          Returns the wrapped connection.
 boolean isClosed()
          Interface method wrapper.
 boolean isReadOnly()
          Interface method wrapper.
 java.lang.String nativeSQL(java.lang.String sql)
          Interface method wrapper.
protected  void postCallHook()
          This hook is invoked after every interface method call.
 java.sql.CallableStatement prepareCall(java.lang.String sql)
          Interface method wrapper.
 java.sql.CallableStatement prepareCall(java.lang.String sql, int resultSetType, int resultSetConcurrency)
          Interface method wrapper.
 java.sql.CallableStatement prepareCall(java.lang.String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)
          Interface method wrapper.
 java.sql.PreparedStatement prepareStatement(java.lang.String sql)
          Interface method wrapper.
 java.sql.PreparedStatement prepareStatement(java.lang.String sql, int autoGeneratedKeys)
          Interface method wrapper.
 java.sql.PreparedStatement prepareStatement(java.lang.String sql, int[] columnIndexes)
          Interface method wrapper.
 java.sql.PreparedStatement prepareStatement(java.lang.String sql, int resultSetType, int resultSetConcurrency)
          Interface method wrapper.
 java.sql.PreparedStatement prepareStatement(java.lang.String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)
          Interface method wrapper.
 java.sql.PreparedStatement prepareStatement(java.lang.String sql, java.lang.String[] columnNames)
          Interface method wrapper.
 void releaseSavepoint(java.sql.Savepoint savepoint)
          Interface method wrapper.
 void rollback()
          Interface method wrapper.
 void rollback(java.sql.Savepoint savepoint)
          Interface method wrapper.
 void setAutoCommit(boolean autoCommit)
          Interface method wrapper.
 void setCatalog(java.lang.String catalog)
          Interface method wrapper.
 void setHoldability(int holdability)
          Interface method wrapper.
 void setReadOnly(boolean readOnly)
          Interface method wrapper.
 java.sql.Savepoint setSavepoint()
          Interface method wrapper.
 java.sql.Savepoint setSavepoint(java.lang.String name)
          Interface method wrapper.
 void setTransactionIsolation(int level)
          Interface method wrapper.
 void setTypeMap(java.util.Map map)
          Interface method wrapper.
 void setWrappedConnection(java.sql.Connection con)
          Sets the wrapped connection.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

con

protected java.sql.Connection con
The wrapped connection.

Constructor Detail

ConnectionWrapper

public ConnectionWrapper()
Creates a new wrapper. The wrapped connection should be set by a setWrappedConnection call before using the instance.


ConnectionWrapper

public ConnectionWrapper(java.sql.Connection con)
Creates a new wrapper and sets the wrapped connection to the specified one.

Parameters:
con - the connection to be wrapped.
Method Detail

setWrappedConnection

public void setWrappedConnection(java.sql.Connection con)
Sets the wrapped connection.

Parameters:
con - connection to wrap.

getWrappedConnection

public java.sql.Connection getWrappedConnection()
Returns the wrapped connection.

Returns:
the wrapped connection.

finalize

protected void finalize()
                 throws java.sql.SQLException
Called by the garbage collector. Tries to close the connection.

Throws:
java.sql.SQLException - if the connection could not be closed.

createStatement

public java.sql.Statement createStatement()
                                   throws java.sql.SQLException
Interface method wrapper.

Also calles createStatementHook hook.

Specified by:
createStatement in interface java.sql.Connection
Throws:
java.sql.SQLException

prepareStatement

public java.sql.PreparedStatement prepareStatement(java.lang.String sql)
                                            throws java.sql.SQLException
Interface method wrapper.

Also calles createStatementHook hook.

Specified by:
prepareStatement in interface java.sql.Connection
Throws:
java.sql.SQLException

prepareCall

public java.sql.CallableStatement prepareCall(java.lang.String sql)
                                       throws java.sql.SQLException
Interface method wrapper.

Also calles createStatementHook hook.

Specified by:
prepareCall in interface java.sql.Connection
Throws:
java.sql.SQLException

nativeSQL

public java.lang.String nativeSQL(java.lang.String sql)
                           throws java.sql.SQLException
Interface method wrapper.

Specified by:
nativeSQL in interface java.sql.Connection
Throws:
java.sql.SQLException

setAutoCommit

public void setAutoCommit(boolean autoCommit)
                   throws java.sql.SQLException
Interface method wrapper.

Specified by:
setAutoCommit in interface java.sql.Connection
Throws:
java.sql.SQLException

getAutoCommit

public boolean getAutoCommit()
                      throws java.sql.SQLException
Interface method wrapper.

Specified by:
getAutoCommit in interface java.sql.Connection
Throws:
java.sql.SQLException

commit

public void commit()
            throws java.sql.SQLException
Interface method wrapper.

Specified by:
commit in interface java.sql.Connection
Throws:
java.sql.SQLException

rollback

public void rollback()
              throws java.sql.SQLException
Interface method wrapper.

Specified by:
rollback in interface java.sql.Connection
Throws:
java.sql.SQLException

close

public void close()
           throws java.sql.SQLException
Interface method wrapper.

Specified by:
close in interface java.sql.Connection
Throws:
java.sql.SQLException

isClosed

public boolean isClosed()
                 throws java.sql.SQLException
Interface method wrapper.

Specified by:
isClosed in interface java.sql.Connection
Throws:
java.sql.SQLException

getMetaData

public java.sql.DatabaseMetaData getMetaData()
                                      throws java.sql.SQLException
Interface method wrapper.

Specified by:
getMetaData in interface java.sql.Connection
Throws:
java.sql.SQLException

setReadOnly

public void setReadOnly(boolean readOnly)
                 throws java.sql.SQLException
Interface method wrapper.

Specified by:
setReadOnly in interface java.sql.Connection
Throws:
java.sql.SQLException

isReadOnly

public boolean isReadOnly()
                   throws java.sql.SQLException
Interface method wrapper.

Specified by:
isReadOnly in interface java.sql.Connection
Throws:
java.sql.SQLException

setCatalog

public void setCatalog(java.lang.String catalog)
                throws java.sql.SQLException
Interface method wrapper.

Specified by:
setCatalog in interface java.sql.Connection
Throws:
java.sql.SQLException

getCatalog

public java.lang.String getCatalog()
                            throws java.sql.SQLException
Interface method wrapper.

Specified by:
getCatalog in interface java.sql.Connection
Throws:
java.sql.SQLException

setTransactionIsolation

public void setTransactionIsolation(int level)
                             throws java.sql.SQLException
Interface method wrapper.

Specified by:
setTransactionIsolation in interface java.sql.Connection
Throws:
java.sql.SQLException

getTransactionIsolation

public int getTransactionIsolation()
                            throws java.sql.SQLException
Interface method wrapper.

Specified by:
getTransactionIsolation in interface java.sql.Connection
Throws:
java.sql.SQLException

getWarnings

public java.sql.SQLWarning getWarnings()
                                throws java.sql.SQLException
Interface method wrapper.

Specified by:
getWarnings in interface java.sql.Connection
Throws:
java.sql.SQLException

clearWarnings

public void clearWarnings()
                   throws java.sql.SQLException
Interface method wrapper.

Specified by:
clearWarnings in interface java.sql.Connection
Throws:
java.sql.SQLException

createStatement

public java.sql.Statement createStatement(int resultSetType,
                                          int resultSetConcurrency)
                                   throws java.sql.SQLException
Interface method wrapper.

Also calles createStatementHook hook.

Specified by:
createStatement in interface java.sql.Connection
Throws:
java.sql.SQLException

prepareStatement

public java.sql.PreparedStatement prepareStatement(java.lang.String sql,
                                                   int resultSetType,
                                                   int resultSetConcurrency)
                                            throws java.sql.SQLException
Interface method wrapper.

Also calles createStatementHook hook.

Specified by:
prepareStatement in interface java.sql.Connection
Throws:
java.sql.SQLException

prepareCall

public java.sql.CallableStatement prepareCall(java.lang.String sql,
                                              int resultSetType,
                                              int resultSetConcurrency)
                                       throws java.sql.SQLException
Interface method wrapper.

Also calles createStatementHook hook.

Specified by:
prepareCall in interface java.sql.Connection
Throws:
java.sql.SQLException

getTypeMap

public java.util.Map getTypeMap()
                         throws java.sql.SQLException
Interface method wrapper.

Specified by:
getTypeMap in interface java.sql.Connection
Throws:
java.sql.SQLException

setTypeMap

public void setTypeMap(java.util.Map map)
                throws java.sql.SQLException
Interface method wrapper.

Specified by:
setTypeMap in interface java.sql.Connection
Throws:
java.sql.SQLException

setHoldability

public void setHoldability(int holdability)
                    throws java.sql.SQLException
Interface method wrapper.

Specified by:
setHoldability in interface java.sql.Connection
Throws:
java.sql.SQLException

getHoldability

public int getHoldability()
                   throws java.sql.SQLException
Interface method wrapper.

Specified by:
getHoldability in interface java.sql.Connection
Throws:
java.sql.SQLException

setSavepoint

public java.sql.Savepoint setSavepoint()
                                throws java.sql.SQLException
Interface method wrapper.

Specified by:
setSavepoint in interface java.sql.Connection
Throws:
java.sql.SQLException

setSavepoint

public java.sql.Savepoint setSavepoint(java.lang.String name)
                                throws java.sql.SQLException
Interface method wrapper.

Specified by:
setSavepoint in interface java.sql.Connection
Throws:
java.sql.SQLException

rollback

public void rollback(java.sql.Savepoint savepoint)
              throws java.sql.SQLException
Interface method wrapper.

Specified by:
rollback in interface java.sql.Connection
Throws:
java.sql.SQLException

releaseSavepoint

public void releaseSavepoint(java.sql.Savepoint savepoint)
                      throws java.sql.SQLException
Interface method wrapper.

Specified by:
releaseSavepoint in interface java.sql.Connection
Throws:
java.sql.SQLException

createStatement

public java.sql.Statement createStatement(int resultSetType,
                                          int resultSetConcurrency,
                                          int resultSetHoldability)
                                   throws java.sql.SQLException
Interface method wrapper.

Also calles createStatementHook hook.

Specified by:
createStatement in interface java.sql.Connection
Throws:
java.sql.SQLException

prepareStatement

public java.sql.PreparedStatement prepareStatement(java.lang.String sql,
                                                   int resultSetType,
                                                   int resultSetConcurrency,
                                                   int resultSetHoldability)
                                            throws java.sql.SQLException
Interface method wrapper.

Also calles createStatementHook hook.

Specified by:
prepareStatement in interface java.sql.Connection
Throws:
java.sql.SQLException

prepareCall

public java.sql.CallableStatement prepareCall(java.lang.String sql,
                                              int resultSetType,
                                              int resultSetConcurrency,
                                              int resultSetHoldability)
                                       throws java.sql.SQLException
Interface method wrapper.

Also calles createStatementHook hook.

Specified by:
prepareCall in interface java.sql.Connection
Throws:
java.sql.SQLException

prepareStatement

public java.sql.PreparedStatement prepareStatement(java.lang.String sql,
                                                   int autoGeneratedKeys)
                                            throws java.sql.SQLException
Interface method wrapper.

Also calles createStatementHook hook.

Specified by:
prepareStatement in interface java.sql.Connection
Throws:
java.sql.SQLException

prepareStatement

public java.sql.PreparedStatement prepareStatement(java.lang.String sql,
                                                   int[] columnIndexes)
                                            throws java.sql.SQLException
Interface method wrapper.

Also calles createStatementHook hook.

Specified by:
prepareStatement in interface java.sql.Connection
Throws:
java.sql.SQLException

prepareStatement

public java.sql.PreparedStatement prepareStatement(java.lang.String sql,
                                                   java.lang.String[] columnNames)
                                            throws java.sql.SQLException
Interface method wrapper.

Also calles createStatementHook hook.

Specified by:
prepareStatement in interface java.sql.Connection
Throws:
java.sql.SQLException

postCallHook

protected void postCallHook()
                     throws java.sql.SQLException
This hook is invoked after every interface method call. It is not called if the underlying implementation method has thrown an exception. The default implementation does nothing.

Throws:
java.sql.SQLException - to indicate an error in the method.

createStatementHook

protected java.sql.Statement createStatementHook(java.sql.Statement stmt,
                                                 java.lang.String sql)
                                          throws java.sql.SQLException
This hook method is called from all the methods that create all kinds of database statements, including prepared and callable statements. The default implementation just returns the passed stmt parameter back, but you can override this method in a subclass.

Parameters:
stmt - statement returned by the underlying wrapped connection.
sql - SQL text passed to a prepareStatement or prepareCall method, null if it was createStatement call.
Returns:
object that should be returned to the caller.
Throws:
java.sql.SQLException - if an error happened.


Copyright © 2002,2003,2004 - Boyle Software, Inc.