org.jcreme.sql
Class WrappedConnection

java.lang.Object
  extended byorg.jcreme.sql.WrappedConnection
All Implemented Interfaces:
java.sql.Connection

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

This class enables to wrap an actual connection to provide a few extra services: access the active statements attached to the connection, handling of some exceptions (rollback, loss of connection). The statements provided by this class are wrapped around the actual statement.

Version:
$Revision: 1.1 $
Author:
$Author: dbregeon $

Field Summary
protected static java.lang.String LOSS_OF_CONNECTION_METHOD_NAME
          Name of the method to use in case of loss of connection.
protected static java.lang.Class[] LOSS_OF_CONNECTION_PARAMETERS
          Types of the parameters to the loss of connection method.
protected static java.lang.String ROLLBACK_METHOD_NAME
          Name of the method to use in case of rollback.
protected static java.lang.Class[] ROLLBACK_PARAMETERS
          Types of the parameters to the rollback method.
 
Fields inherited from interface java.sql.Connection
TRANSACTION_NONE, TRANSACTION_READ_COMMITTED, TRANSACTION_READ_UNCOMMITTED, TRANSACTION_REPEATABLE_READ, TRANSACTION_SERIALIZABLE
 
Constructor Summary
protected WrappedConnection(java.sql.Connection connection)
          Create the wrapping around a database Connection.
 
Method Summary
 void clearWarnings()
           
 void close()
           
 void commit()
           
protected  void connectionExternallyClosed()
          This method enables to actually close the connection when it is lost.
 java.sql.Statement createStatement()
           
 java.sql.Statement createStatement(int i, int j)
           
 java.sql.Statement createStatement(int i, int j, int k)
           
 java.sql.Statement[] getActiveStatements()
          This method gives access to the statements that are currently registered in the Connection.
 boolean getAutoCommit()
           
 java.lang.String getCatalog()
           
 SQLExceptionHandler getExceptionHandler()
          Gives access to the Exception handler that is used by this Connection.
 int getHoldability()
           
static WrappedConnection getInstance(java.sql.Connection connection)
          This method enables to build or retrieve the WrappedConnection created from a given connection.
 CremeAction getLossOfConnectionAction()
          Gives access to the action that enables to close this Connection in case of loss of connection.
static java.lang.reflect.Method getLossOfConnectionMethod()
          Gives access to the method that is used to handle loss of connection in this class.
 java.sql.DatabaseMetaData getMetaData()
           
 ConnectionPool getParentPool()
          Gives access to the eventual ConnectionPool to which this connection is attached.
 CremeAction getRollBackAction()
          Gives access to the action that enables to rollback this Connection.
static java.lang.reflect.Method getRollbackMethod()
          Gives access to the method that is used to rollback in this class.
 int getTransactionIsolation()
           
 java.util.Map getTypeMap()
           
 java.sql.SQLWarning getWarnings()
           
 boolean isClosed()
           
 boolean isReadOnly()
           
protected  int manageException(java.sql.SQLException e)
          This method enables the Connection to handle the exceptions.
protected  int manageException(java.sql.SQLException e, CremeAction redo)
          This method enables the Connection to handle the exceptions.
 java.lang.String nativeSQL(java.lang.String sql)
           
 java.sql.CallableStatement prepareCall(java.lang.String sql)
           
 java.sql.CallableStatement prepareCall(java.lang.String sql, int i, int j)
           
 java.sql.CallableStatement prepareCall(java.lang.String sql, int i, int j, int k)
           
 java.sql.PreparedStatement prepareStatement(java.lang.String sql)
           
 java.sql.PreparedStatement prepareStatement(java.lang.String sql, int i)
           
 java.sql.PreparedStatement prepareStatement(java.lang.String sql, int[] i)
           
 java.sql.PreparedStatement prepareStatement(java.lang.String sql, int i, int j)
           
 java.sql.PreparedStatement prepareStatement(java.lang.String sql, int i, int j, int k)
           
 java.sql.PreparedStatement prepareStatement(java.lang.String sql, java.lang.String[] s)
           
protected  void registerActiveStatement(java.sql.Statement statement)
          This method is called when a Statement is created from this Connection.
 void releaseSavepoint(java.sql.Savepoint savepoint)
           
 void rollback()
           
 void rollback(java.sql.Savepoint savepoint)
           
 void setAutoCommit(boolean flag)
           
 void setCatalog(java.lang.String s)
           
 void setExceptionHandler(SQLExceptionHandler handler)
          Sets the Exception Handler for this connection.
 void setHoldability(int i)
           
 void setParentPool(ConnectionPool pool)
          This method enabes to attach this WrappedConnection to a ConnectionPool.
 void setReadOnly(boolean readOnly)
           
 java.sql.Savepoint setSavepoint()
           
 java.sql.Savepoint setSavepoint(java.lang.String name)
           
 void setTransactionIsolation(int i)
           
 void setTypeMap(java.util.Map map)
           
protected  void unregisterActiveStatement(java.sql.Statement statement)
          This method is called when a Statement created in this Connection is closed.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ROLLBACK_METHOD_NAME

protected static final java.lang.String ROLLBACK_METHOD_NAME
Name of the method to use in case of rollback.

See Also:
Constant Field Values

ROLLBACK_PARAMETERS

protected static final java.lang.Class[] ROLLBACK_PARAMETERS
Types of the parameters to the rollback method.


LOSS_OF_CONNECTION_METHOD_NAME

protected static final java.lang.String LOSS_OF_CONNECTION_METHOD_NAME
Name of the method to use in case of loss of connection.

See Also:
Constant Field Values

LOSS_OF_CONNECTION_PARAMETERS

protected static final java.lang.Class[] LOSS_OF_CONNECTION_PARAMETERS
Types of the parameters to the loss of connection method.

Constructor Detail

WrappedConnection

protected WrappedConnection(java.sql.Connection connection)
                     throws java.sql.SQLException
Create the wrapping around a database Connection.

Parameters:
connection - the connection to wrap.
Throws:
java.sql.SQLException - if connection is null.
Method Detail

getInstance

public static WrappedConnection getInstance(java.sql.Connection connection)
This method enables to build or retrieve the WrappedConnection created from a given connection.

Parameters:
connection - the connection for which we want a wrapper.
Returns:
a WrappedConnection that wraps the given connection. null if connection is null.

getRollbackMethod

public static java.lang.reflect.Method getRollbackMethod()
Gives access to the method that is used to rollback in this class.

Returns:
the rollback method.

getLossOfConnectionMethod

public static java.lang.reflect.Method getLossOfConnectionMethod()
Gives access to the method that is used to handle loss of connection in this class.

Returns:
the loss of connection method.

setParentPool

public void setParentPool(ConnectionPool pool)
This method enabes to attach this WrappedConnection to a ConnectionPool. Being attached to such a pool modifies some of the behaviour of the Connection.

Parameters:
pool - the ConnectionPool to attach.

getParentPool

public ConnectionPool getParentPool()
Gives access to the eventual ConnectionPool to which this connection is attached.

Returns:
the parent pool of the Connection. Null if the connection is not attached to a pool.

setExceptionHandler

public void setExceptionHandler(SQLExceptionHandler handler)
Sets the Exception Handler for this connection. The handler will be called upon anytime a SQLException is thrown in the Connection.

Parameters:
handler - the handler that will be used for all the SQLExceptions.

getExceptionHandler

public SQLExceptionHandler getExceptionHandler()
Gives access to the Exception handler that is used by this Connection.

Returns:
the SQLExceptionHandler used by this connection.

getRollBackAction

public CremeAction getRollBackAction()
Gives access to the action that enables to rollback this Connection.

Returns:
the CremeAction that enables to rollback this Connection.

getLossOfConnectionAction

public CremeAction getLossOfConnectionAction()
Gives access to the action that enables to close this Connection in case of loss of connection.

Returns:
the CremeAction that enables to close this Connection.

connectionExternallyClosed

protected void connectionExternallyClosed()
                                   throws java.sql.SQLException
This method enables to actually close the connection when it is lost. When the Connection is attached to a Pool, the close method merely returns it to the ConnectionPool.

Throws:
java.sql.SQLException - if an error occurs while closing the connection.

manageException

protected int manageException(java.sql.SQLException e)
This method enables the Connection to handle the exceptions. If an SQLExceptionHandler is defined it is called.

Parameters:
e - the SQLException to handle.
Returns:
the result provided by the exception handler (@see SQLExceptionHandler#handleException(SQLException, CremeAction, CremeAction))

manageException

protected int manageException(java.sql.SQLException e,
                              CremeAction redo)
This method enables the Connection to handle the exceptions. If an SQLExceptionHandler is defined it is called.

Parameters:
e - the SQLException to handle.
redo - the action that has provoked the exception. It will be called again if possible.
Returns:
the result provided by the exception handler (@see SQLExceptionHandler#handleException(SQLException, CremeAction, CremeAction, CremeAction))

setCatalog

public void setCatalog(java.lang.String s)
                throws java.sql.SQLException
Specified by:
setCatalog in interface java.sql.Connection
Throws:
java.sql.SQLException
See Also:
Connection.setCatalog(java.lang.String)

close

public void close()
           throws java.sql.SQLException
Specified by:
close in interface java.sql.Connection
Throws:
java.sql.SQLException
See Also:
Connection.close()

clearWarnings

public void clearWarnings()
                   throws java.sql.SQLException
Specified by:
clearWarnings in interface java.sql.Connection
Throws:
java.sql.SQLException
See Also:
Connection.clearWarnings()

rollback

public void rollback()
              throws java.sql.SQLException
Specified by:
rollback in interface java.sql.Connection
Throws:
java.sql.SQLException
See Also:
Connection.rollback()

getTypeMap

public java.util.Map getTypeMap()
                         throws java.sql.SQLException
Specified by:
getTypeMap in interface java.sql.Connection
Throws:
java.sql.SQLException
See Also:
Connection.getTypeMap()

getTransactionIsolation

public int getTransactionIsolation()
                            throws java.sql.SQLException
Specified by:
getTransactionIsolation in interface java.sql.Connection
Throws:
java.sql.SQLException
See Also:
Connection.getTransactionIsolation()

prepareStatement

public java.sql.PreparedStatement prepareStatement(java.lang.String sql)
                                            throws java.sql.SQLException
Specified by:
prepareStatement in interface java.sql.Connection
Throws:
java.sql.SQLException
See Also:
Connection.prepareStatement(java.lang.String)

setTransactionIsolation

public void setTransactionIsolation(int i)
                             throws java.sql.SQLException
Specified by:
setTransactionIsolation in interface java.sql.Connection
Throws:
java.sql.SQLException
See Also:
Connection.setTransactionIsolation(int)

isClosed

public boolean isClosed()
                 throws java.sql.SQLException
Specified by:
isClosed in interface java.sql.Connection
Throws:
java.sql.SQLException
See Also:
Connection.isClosed()

createStatement

public java.sql.Statement createStatement()
                                   throws java.sql.SQLException
Specified by:
createStatement in interface java.sql.Connection
Throws:
java.sql.SQLException
See Also:
Connection.createStatement()

createStatement

public java.sql.Statement createStatement(int i,
                                          int j)
                                   throws java.sql.SQLException
Specified by:
createStatement in interface java.sql.Connection
Throws:
java.sql.SQLException
See Also:
Connection.createStatement(int, int)

setAutoCommit

public void setAutoCommit(boolean flag)
                   throws java.sql.SQLException
Specified by:
setAutoCommit in interface java.sql.Connection
Throws:
java.sql.SQLException
See Also:
Connection.setAutoCommit(boolean)

prepareCall

public java.sql.CallableStatement prepareCall(java.lang.String sql,
                                              int i,
                                              int j)
                                       throws java.sql.SQLException
Specified by:
prepareCall in interface java.sql.Connection
Throws:
java.sql.SQLException
See Also:
Connection.prepareCall(java.lang.String, int, int)

commit

public void commit()
            throws java.sql.SQLException
Specified by:
commit in interface java.sql.Connection
Throws:
java.sql.SQLException
See Also:
Connection.commit()

getCatalog

public java.lang.String getCatalog()
                            throws java.sql.SQLException
Specified by:
getCatalog in interface java.sql.Connection
Throws:
java.sql.SQLException
See Also:
Connection.getCatalog()

prepareStatement

public java.sql.PreparedStatement prepareStatement(java.lang.String sql,
                                                   int i,
                                                   int j)
                                            throws java.sql.SQLException
Specified by:
prepareStatement in interface java.sql.Connection
Throws:
java.sql.SQLException
See Also:
Connection.prepareStatement(java.lang.String, int, int)

getMetaData

public java.sql.DatabaseMetaData getMetaData()
                                      throws java.sql.SQLException
Specified by:
getMetaData in interface java.sql.Connection
Throws:
java.sql.SQLException
See Also:
Connection.getMetaData()

getAutoCommit

public boolean getAutoCommit()
                      throws java.sql.SQLException
Specified by:
getAutoCommit in interface java.sql.Connection
Throws:
java.sql.SQLException
See Also:
Connection.getAutoCommit()

nativeSQL

public java.lang.String nativeSQL(java.lang.String sql)
                           throws java.sql.SQLException
Specified by:
nativeSQL in interface java.sql.Connection
Throws:
java.sql.SQLException
See Also:
Connection.nativeSQL(java.lang.String)

prepareCall

public java.sql.CallableStatement prepareCall(java.lang.String sql)
                                       throws java.sql.SQLException
Specified by:
prepareCall in interface java.sql.Connection
Throws:
java.sql.SQLException
See Also:
Connection.prepareCall(java.lang.String)

setTypeMap

public void setTypeMap(java.util.Map map)
                throws java.sql.SQLException
Specified by:
setTypeMap in interface java.sql.Connection
Throws:
java.sql.SQLException
See Also:
Connection.setTypeMap(java.util.Map)

getWarnings

public java.sql.SQLWarning getWarnings()
                                throws java.sql.SQLException
Specified by:
getWarnings in interface java.sql.Connection
Throws:
java.sql.SQLException
See Also:
Connection.getWarnings()

registerActiveStatement

protected void registerActiveStatement(java.sql.Statement statement)
This method is called when a Statement is created from this Connection. It enables to keep track of the Statements that are attached to this Connection.

Parameters:
statement - the statement to attach to this Connection.

unregisterActiveStatement

protected void unregisterActiveStatement(java.sql.Statement statement)
This method is called when a Statement created in this Connection is closed.

Parameters:
statement - the statement to unregister.

getActiveStatements

public java.sql.Statement[] getActiveStatements()
This method gives access to the statements that are currently registered in the Connection. These statements are currently active.

Returns:
the statements registered in the Connection.

setHoldability

public void setHoldability(int i)
                    throws java.sql.SQLException
Specified by:
setHoldability in interface java.sql.Connection
Throws:
java.sql.SQLException
See Also:
Connection.setHoldability(int)

getHoldability

public int getHoldability()
                   throws java.sql.SQLException
Specified by:
getHoldability in interface java.sql.Connection
Throws:
java.sql.SQLException
See Also:
Connection.getHoldability()

rollback

public void rollback(java.sql.Savepoint savepoint)
              throws java.sql.SQLException
Specified by:
rollback in interface java.sql.Connection
Throws:
java.sql.SQLException
See Also:
Connection.rollback(java.sql.Savepoint)

createStatement

public java.sql.Statement createStatement(int i,
                                          int j,
                                          int k)
                                   throws java.sql.SQLException
Specified by:
createStatement in interface java.sql.Connection
Throws:
java.sql.SQLException
See Also:
Connection.createStatement(int, int, int)

prepareStatement

public java.sql.PreparedStatement prepareStatement(java.lang.String sql,
                                                   int i,
                                                   int j,
                                                   int k)
                                            throws java.sql.SQLException
Specified by:
prepareStatement in interface java.sql.Connection
Throws:
java.sql.SQLException
See Also:
Connection.prepareStatement(java.lang.String,int,int,int)

prepareCall

public java.sql.CallableStatement prepareCall(java.lang.String sql,
                                              int i,
                                              int j,
                                              int k)
                                       throws java.sql.SQLException
Specified by:
prepareCall in interface java.sql.Connection
Throws:
java.sql.SQLException
See Also:
Connection.prepareCall(java.lang.String, int, int, int)

prepareStatement

public java.sql.PreparedStatement prepareStatement(java.lang.String sql,
                                                   int i)
                                            throws java.sql.SQLException
Specified by:
prepareStatement in interface java.sql.Connection
Throws:
java.sql.SQLException
See Also:
Connection.prepareStatement(java.lang.String, int)

prepareStatement

public java.sql.PreparedStatement prepareStatement(java.lang.String sql,
                                                   int[] i)
                                            throws java.sql.SQLException
Specified by:
prepareStatement in interface java.sql.Connection
Throws:
java.sql.SQLException
See Also:
Connection.prepareStatement(java.lang.String, int[])

prepareStatement

public java.sql.PreparedStatement prepareStatement(java.lang.String sql,
                                                   java.lang.String[] s)
                                            throws java.sql.SQLException
Specified by:
prepareStatement in interface java.sql.Connection
Throws:
java.sql.SQLException
See Also:
Connection.prepareStatement(java.lang.String, java.lang.String[])

isReadOnly

public boolean isReadOnly()
                   throws java.sql.SQLException
Specified by:
isReadOnly in interface java.sql.Connection
Throws:
java.sql.SQLException
See Also:
Connection.isReadOnly()

releaseSavepoint

public void releaseSavepoint(java.sql.Savepoint savepoint)
                      throws java.sql.SQLException
Specified by:
releaseSavepoint in interface java.sql.Connection
Throws:
java.sql.SQLException
See Also:
Connection.releaseSavepoint(java.sql.Savepoint)

setReadOnly

public void setReadOnly(boolean readOnly)
                 throws java.sql.SQLException
Specified by:
setReadOnly in interface java.sql.Connection
Throws:
java.sql.SQLException
See Also:
Connection.setReadOnly(boolean)

setSavepoint

public java.sql.Savepoint setSavepoint()
                                throws java.sql.SQLException
Specified by:
setSavepoint in interface java.sql.Connection
Throws:
java.sql.SQLException
See Also:
Connection.setSavepoint()

setSavepoint

public java.sql.Savepoint setSavepoint(java.lang.String name)
                                throws java.sql.SQLException
Specified by:
setSavepoint in interface java.sql.Connection
Throws:
java.sql.SQLException
See Also:
Connection.setSavepoint(java.lang.String)


Copyright 2006 null. All Rights Reserved.