org.mmbase.module.database
Class MultiConnection

java.lang.Object
  extended byorg.mmbase.module.database.MultiConnection
All Implemented Interfaces:
java.sql.Connection

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

MultiConnection is a replacement class for Connection it provides you a multiplexed and reuseable connections from the connection pool. The main function of this class is to 'log' (keep) the last sql statement passed to it. Another function is to keep state (i.e. notifying that it is busy), and to make itself available again to teh connectionpool once it is finished (closed).

Version:
$Id: MultiConnection.java,v 1.33.2.4 2004/08/26 12:40:14 michiel Exp $
Author:
vpro, Pierre van Rooden
Warning: Contains Database Legacy Code
It would possibly be better to pass the logging of the sql query to the code that calls the conenction, rather than place it in the conenction itself, as it's implementation leads to conflicts between various JDBC versions. This also goes for freeing the connection once it is 'closed'.

Field Summary
static int CON_BUSY
           
static int CON_FAILED
           
static int CON_FINISHED
           
static int CON_UNUSED
           
 int state
           
 
Fields inherited from interface java.sql.Connection
TRANSACTION_NONE, TRANSACTION_READ_COMMITTED, TRANSACTION_READ_UNCOMMITTED, TRANSACTION_REPEATABLE_READ, TRANSACTION_SERIALIZABLE
 
Constructor Summary
protected MultiConnection()
          protected constructor for extending classes, so they can use this with for example only a connection..
  MultiConnection(MultiPool parent, java.sql.Connection con)
           
 
Method Summary
protected  boolean checkAfterException()
          Tries to fix the this connection, if it proves to be broken.
 boolean checkSQLError(java.lang.Exception e)
           
 void claim()
           
 void clearWarnings()
          clear Warnings
 void close()
          Close connections
 void commit()
          Perform commit
 java.sql.Statement createStatement()
          createStatement returns an SQL Statement object
 java.sql.Statement createStatement(int i, int y)
          createStatement returns an SQL Statement object
 java.sql.Statement createStatement(int type, int concurrency, int holdability)
          Creates a Statement object that will generate ResultSet objects with the given type, concurrency, and holdability.
 boolean getAutoCommit()
          get AutoCommit mode
 java.lang.String getCatalog()
          The "catalog" name
 int getHoldability()
          Retrieves the current holdability of ResultSet objects created using this Connection object.
 java.lang.String getLastSQL()
           
 java.sql.DatabaseMetaData getMetaData()
          Advanced features: You can obtain a DatabaseMetaData object to get information about the target database.
 int getStartTime()
          Returns the moment on which the last SQL statement was started in seconds after 1970.
 long getStartTimeMillis()
          Returns the moment on which the last SQL statement was started in milliseconds after 1970.
 java.lang.String getStateString()
           
 int getTransactionIsolation()
           
 java.util.Map getTypeMap()
           
 int getUsage()
           
 java.sql.SQLWarning getWarnings()
          getWarnings will return any warning information related to the current connection.
 boolean isClosed()
          isClosed returns true if the connection is closed, which can occur either due to an explicit call on "close" or due to some fatal error on the connection.
 boolean isReadOnly()
          Is this database readonly ?
 java.lang.String nativeSQL(java.lang.String query)
          Convert the given generic SQL statement to the drivers native SQL.
 java.sql.CallableStatement prepareCall(java.lang.String sql)
          prepareCall create a pre-compiled SQL statement that is a call on a stored procedure.
 java.sql.CallableStatement prepareCall(java.lang.String sql, int i, int y)
          prepareCall create a pre-compiled SQL statement that is a call on a stored procedure.
 java.sql.CallableStatement prepareCall(java.lang.String sql, int type, int concurrency, int holdability)
          Creates a CallableStatement object that will generate ResultSet objects with the given type, concurrency, and holdability.
 java.sql.PreparedStatement prepareStatement(java.lang.String sql)
          prepareStatement creates a pre-compiled SQL PreparedStatement object.
 java.sql.PreparedStatement prepareStatement(java.lang.String sql, int autoGeneratedKeys)
          Creates a default PreparedStatement object that has the capability to retrieve auto-generated keys.
 java.sql.PreparedStatement prepareStatement(java.lang.String sql, int[] columnIndexes)
          Creates a default PreparedStatement object capable of returning the auto-generated keys designated by the given array.
 java.sql.PreparedStatement prepareStatement(java.lang.String sql, int i, int y)
          prepareStatement creates a pre-compiled SQL PreparedStatement object.
 java.sql.PreparedStatement prepareStatement(java.lang.String sql, int type, int concurrency, int holdability)
          Creates a PreparedStatement object that will generate ResultSet objects with the given type, concurrency, and holdability.
 java.sql.PreparedStatement prepareStatement(java.lang.String sql, java.lang.String[] columnNames)
          Creates a default PreparedStatement object capable of returning the auto-generated keys designated by the given array.
 void realclose()
          Close connections
 void release()
           
 void releaseSavepoint(java.sql.Savepoint savepoint)
          Removes the given Savepoint object from the current transaction.
 void rollback()
          Perform rollback
 void rollback(java.sql.Savepoint savepoint)
          Undoes all changes made after the given Savepoint object was set.
 void setAutoCommit(boolean enableAutoCommit)
          If "autoCommit" is true, then all subsequent SQL statements will be executed and committed as individual transactions.
 void setCatalog(java.lang.String catalog)
          The "catalog" selects a sub-space of the target database.
 void setHoldability(int holdability)
          Changes the holdability of ResultSet objects created using this Connection object to the given holdability.
 void setLastSQL(java.lang.String sql)
           
 void setReadOnly(boolean readOnly)
          You can put a connection in read-only mode as a hint to enable database optimizations.
 java.sql.Savepoint setSavepoint()
          Creates an unnamed savepoint in the current transaction and returns the new Savepoint object that represents it.
 java.sql.Savepoint setSavepoint(java.lang.String name)
          Creates a savepoint with the given name in the current transaction and returns the new Savepoint object that represents it.
 void setTransactionIsolation(int level)
          You can call the following method to try to change the transaction isolation level on a newly opened connection, using one of the TRANSACTION_* values.
 void setTypeMap(java.util.Map mp)
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

CON_UNUSED

public static final int CON_UNUSED
See Also:
Constant Field Values

CON_BUSY

public static final int CON_BUSY
See Also:
Constant Field Values

CON_FINISHED

public static final int CON_FINISHED
See Also:
Constant Field Values

CON_FAILED

public static final int CON_FAILED
See Also:
Constant Field Values

state

public int state
Constructor Detail

MultiConnection

protected MultiConnection()
protected constructor for extending classes, so they can use this with for example only a connection..


MultiConnection

public MultiConnection(MultiPool parent,
                       java.sql.Connection con)
Warning: No Java Documentation Available.
Method Detail

getStateString

public java.lang.String getStateString()
Warning: No Java Documentation Available.

setLastSQL

public void setLastSQL(java.lang.String sql)
Warning: No Java Documentation Available.

getLastSQL

public java.lang.String getLastSQL()
Warning: No Java Documentation Available.

createStatement

public java.sql.Statement createStatement()
                                   throws java.sql.SQLException
createStatement returns an SQL Statement object

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
prepareStatement creates a pre-compiled SQL PreparedStatement object.

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
prepareCall create a pre-compiled SQL statement that is a call on a stored procedure.

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

nativeSQL

public java.lang.String nativeSQL(java.lang.String query)
                           throws java.sql.SQLException
Convert the given generic SQL statement to the drivers native SQL.

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

checkAfterException

protected boolean checkAfterException()
                               throws java.sql.SQLException
Tries to fix the this connection, if it proves to be broken. It is supposed to be broken if the query "SELECT 1 FROM " does yield an exception. This method is meant to be called in the catch after trying to use the connection.

Returns:
true if connection was broken and successfully repaired. false if connection was not broken.
Throws:
java.sql.SQLException - If connection is broken and no new one could be obtained.
Since:
MMBase-1.7.1

setAutoCommit

public void setAutoCommit(boolean enableAutoCommit)
                   throws java.sql.SQLException
If "autoCommit" is true, then all subsequent SQL statements will be executed and committed as individual transactions. Otherwise (if "autoCommit" is false) then subsequent SQL statements will all be part of the same transaction , which must be explicitly committed with either a "commit" or "rollback" call. By default new connections are initialized with autoCommit "true".

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

getAutoCommit

public boolean getAutoCommit()
                      throws java.sql.SQLException
get AutoCommit mode

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

commit

public void commit()
            throws java.sql.SQLException
Perform commit

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

rollback

public void rollback()
              throws java.sql.SQLException
Perform rollback

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

close

public void close()
           throws java.sql.SQLException
Close connections

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

realclose

public void realclose()
               throws java.sql.SQLException
Close connections

Throws:
java.sql.SQLException

isClosed

public boolean isClosed()
                 throws java.sql.SQLException
isClosed returns true if the connection is closed, which can occur either due to an explicit call on "close" or due to some fatal error on the connection.

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

getMetaData

public java.sql.DatabaseMetaData getMetaData()
                                      throws java.sql.SQLException
Advanced features: You can obtain a DatabaseMetaData object to get information about the target database.

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

setReadOnly

public void setReadOnly(boolean readOnly)
                 throws java.sql.SQLException
You can put a connection in read-only mode as a hint to enable database optimizations. Note that setReadOnly cannot be called while in the middle of a transaction.

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

isReadOnly

public boolean isReadOnly()
                   throws java.sql.SQLException
Is this database readonly ?

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

setCatalog

public void setCatalog(java.lang.String catalog)
                throws java.sql.SQLException
The "catalog" selects a sub-space of the target database.

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

getCatalog

public java.lang.String getCatalog()
                            throws java.sql.SQLException
The "catalog" name

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

setTransactionIsolation

public void setTransactionIsolation(int level)
                             throws java.sql.SQLException
You can call the following method to try to change the transaction isolation level on a newly opened connection, using one of the TRANSACTION_* values. Use the DatabaseMetaData class to find what isolation levels are supported by the current database. Note that setTransactionIsolation cannot be called while in the middle of a transaction.

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

getTransactionIsolation

public int getTransactionIsolation()
                            throws java.sql.SQLException
Specified by:
getTransactionIsolation in interface java.sql.Connection
Throws:
java.sql.SQLException
Warning: No Java Documentation Available.

getWarnings

public java.sql.SQLWarning getWarnings()
                                throws java.sql.SQLException
getWarnings will return any warning information related to the current connection. Note that SQLWarning may be a chain.

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

clearWarnings

public void clearWarnings()
                   throws java.sql.SQLException
clear Warnings

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

checkSQLError

public boolean checkSQLError(java.lang.Exception e)
Warning: No Java Documentation Available.

claim

public void claim()
Warning: No Java Documentation Available.

release

public void release()
Warning: No Java Documentation Available.

getUsage

public int getUsage()
Warning: No Java Documentation Available.

getStartTime

public int getStartTime()
Returns the moment on which the last SQL statement was started in seconds after 1970.


getStartTimeMillis

public long getStartTimeMillis()
Returns the moment on which the last SQL statement was started in milliseconds after 1970.


toString

public java.lang.String toString()
Warning: No Java Documentation Available.

prepareCall

public java.sql.CallableStatement prepareCall(java.lang.String sql,
                                              int i,
                                              int y)
                                       throws java.sql.SQLException
prepareCall create a pre-compiled SQL statement that is a call on a stored procedure.

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

setTypeMap

public void setTypeMap(java.util.Map mp)
                throws java.sql.SQLException
Specified by:
setTypeMap in interface java.sql.Connection
Throws:
java.sql.SQLException
Warning: No Java Documentation Available.

getTypeMap

public java.util.Map getTypeMap()
                         throws java.sql.SQLException
Specified by:
getTypeMap in interface java.sql.Connection
Throws:
java.sql.SQLException
Warning: No Java Documentation Available.

createStatement

public java.sql.Statement createStatement(int i,
                                          int y)
                                   throws java.sql.SQLException
createStatement returns an SQL Statement object

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

prepareStatement

public java.sql.PreparedStatement prepareStatement(java.lang.String sql,
                                                   int i,
                                                   int y)
                                            throws java.sql.SQLException
prepareStatement creates a pre-compiled SQL PreparedStatement object.

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

setHoldability

public void setHoldability(int holdability)
                    throws java.sql.SQLException
Changes the holdability of ResultSet objects created using this Connection object to the given holdability.

Specified by:
setHoldability in interface java.sql.Connection
Parameters:
holdability - the holdability, one of ResultSet.HOLD_CURSORS_OVER_COMMIT or ResultSet.CLOSE_CURSORS_AT_COMMIT
Throws:
java.sql.SQLException
Since:
MMBase 1.5, JDBC 1.4
Warning: No Java Documentation Available.

getHoldability

public int getHoldability()
                   throws java.sql.SQLException
Retrieves the current holdability of ResultSet objects created using this Connection object.

Specified by:
getHoldability in interface java.sql.Connection
Returns:
the holdability, one of ResultSet.HOLD_CURSORS_OVER_COMMIT or ResultSet.CLOSE_CURSORS_AT_COMMIT
Throws:
java.sql.SQLException
Since:
MMBase 1.5, JDBC 1.4
Warning: No Java Documentation Available.

setSavepoint

public java.sql.Savepoint setSavepoint()
                                throws java.sql.SQLException
Creates an unnamed savepoint in the current transaction and returns the new Savepoint object that represents it.

Specified by:
setSavepoint in interface java.sql.Connection
Returns:
the new Savepoint object
Throws:
java.sql.SQLException
Since:
MMBase 1.5, JDBC 1.4

setSavepoint

public java.sql.Savepoint setSavepoint(java.lang.String name)
                                throws java.sql.SQLException
Creates a savepoint with the given name in the current transaction and returns the new Savepoint object that represents it.

Specified by:
setSavepoint in interface java.sql.Connection
Parameters:
name - a String containing the name of the savepoint
Returns:
the new Savepoint object
Throws:
java.sql.SQLException
Since:
MMBase 1.5, JDBC 1.4

rollback

public void rollback(java.sql.Savepoint savepoint)
              throws java.sql.SQLException
Undoes all changes made after the given Savepoint object was set. This method should be used only when auto-commit has been disabled.

Specified by:
rollback in interface java.sql.Connection
Parameters:
savepoint - the Savepoint object to roll back to
Throws:
java.sql.SQLException
Since:
MMBase 1.5, JDBC 1.4

releaseSavepoint

public void releaseSavepoint(java.sql.Savepoint savepoint)
                      throws java.sql.SQLException
Removes the given Savepoint object from the current transaction. Any reference to the savepoint after it have been removed will cause an SQLException to be thrown

Specified by:
releaseSavepoint in interface java.sql.Connection
Parameters:
savepoint - the Savepoint object to remove
Throws:
java.sql.SQLException
Since:
MMBase 1.5, JDBC 1.4

createStatement

public java.sql.Statement createStatement(int type,
                                          int concurrency,
                                          int holdability)
                                   throws java.sql.SQLException
Creates a Statement object that will generate ResultSet objects with the given type, concurrency, and holdability.

Specified by:
createStatement in interface java.sql.Connection
Parameters:
type - one of the following ResultSet constants: ResultSet.TYPE_FORWARD_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE, or ResultSet.TYPE_SCROLL_SENSITIVE
concurrency - - one of the following ResultSet constants: ResultSet.CONCUR_READ_ONLY or ResultSet.CONCUR_UPDATABLE
holdability - - one of the following ResultSet constants: ResultSet.HOLD_CURSORS_OVER_COMMIT or ResultSet.CLOSE_CURSORS_AT_COMMIT
Returns:
a new Statement object
Throws:
java.sql.SQLException
Since:
MMBase 1.5, JDBC 1.4

prepareStatement

public java.sql.PreparedStatement prepareStatement(java.lang.String sql,
                                                   int type,
                                                   int concurrency,
                                                   int holdability)
                                            throws java.sql.SQLException
Creates a PreparedStatement object that will generate ResultSet objects with the given type, concurrency, and holdability.

Specified by:
prepareStatement in interface java.sql.Connection
Parameters:
sql - a String object that is the SQL statement to be sent to the database
type - one of the following ResultSet constants: ResultSet.TYPE_FORWARD_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE, or ResultSet.TYPE_SCROLL_SENSITIVE
concurrency - - one of the following ResultSet constants: ResultSet.CONCUR_READ_ONLY or ResultSet.CONCUR_UPDATABLE
holdability - - one of the following ResultSet constants: ResultSet.HOLD_CURSORS_OVER_COMMIT or ResultSet.CLOSE_CURSORS_AT_COMMIT
Returns:
a new PreparedStatement object
Throws:
java.sql.SQLException
Since:
MMBase 1.5, JDBC 1.4

prepareCall

public java.sql.CallableStatement prepareCall(java.lang.String sql,
                                              int type,
                                              int concurrency,
                                              int holdability)
                                       throws java.sql.SQLException
Creates a CallableStatement object that will generate ResultSet objects with the given type, concurrency, and holdability.

Specified by:
prepareCall in interface java.sql.Connection
Parameters:
sql - a String object that is the SQL statement to be sent to the database
type - one of the following ResultSet constants: ResultSet.TYPE_FORWARD_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE, or ResultSet.TYPE_SCROLL_SENSITIVE
concurrency - - one of the following ResultSet constants: ResultSet.CONCUR_READ_ONLY or ResultSet.CONCUR_UPDATABLE
holdability - - one of the following ResultSet constants: ResultSet.HOLD_CURSORS_OVER_COMMIT or ResultSet.CLOSE_CURSORS_AT_COMMIT
Returns:
a new CallableStatement object
Throws:
java.sql.SQLException
Since:
MMBase 1.5, JDBC 1.4

prepareStatement

public java.sql.PreparedStatement prepareStatement(java.lang.String sql,
                                                   int autoGeneratedKeys)
                                            throws java.sql.SQLException
Creates a default PreparedStatement object that has the capability to retrieve auto-generated keys. The given constant tells the driver whether it should make auto-generated keys available for retrieval. This array is ignored if the SQL statement is not an INSERT statement.

Specified by:
prepareStatement in interface java.sql.Connection
Parameters:
sql - a String object that is the SQL statement to be sent to the database
autoGeneratedKeys - a flag indicating whether auto-generated keys should be returned;
Returns:
a new PreparedStatement object
Throws:
java.sql.SQLException
Since:
MMBase 1.5, JDBC 1.4

prepareStatement

public java.sql.PreparedStatement prepareStatement(java.lang.String sql,
                                                   int[] columnIndexes)
                                            throws java.sql.SQLException
Creates a default PreparedStatement object capable of returning the auto-generated keys designated by the given array. This array contains the indexes of the columns in the target table that contain the auto-generated keys that should be made available. This array is ignored if the SQL statement is not an INSERT statement.

Specified by:
prepareStatement in interface java.sql.Connection
Parameters:
sql - a String object that is the SQL statement to be sent to the database
columnIndexes - an array of column indexes indicating the columns that should be returned from the inserted row or rows
Returns:
a new PreparedStatement object
Throws:
java.sql.SQLException
Since:
MMBase 1.5, JDBC 1.4

prepareStatement

public java.sql.PreparedStatement prepareStatement(java.lang.String sql,
                                                   java.lang.String[] columnNames)
                                            throws java.sql.SQLException
Creates a default PreparedStatement object capable of returning the auto-generated keys designated by the given array. This array contains the names of the columns in the target table that contain the auto-generated keys that should be returned. This array is ignored if the SQL statement is not an INSERT statement.

Specified by:
prepareStatement in interface java.sql.Connection
Parameters:
sql - a String object that is the SQL statement to be sent to the database
columnNames - an array of column names indicating the columns that should be returned from the inserted row or rows
Returns:
a new PreparedStatement object
Throws:
java.sql.SQLException
Since:
MMBase 1.5, JDBC 1.4


MMBase build 1.7.1.20041002