org.mmbase.module.database
Class MultiConnection

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

public class MultiConnection
extends ConnectionWrapper

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.44 2006/07/03 11:59:16 johannes 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
           
static long queries
           
 int state
           
 
Fields inherited from class org.mmbase.module.database.ConnectionWrapper
con
 
Fields inherited from interface java.sql.Connection
TRANSACTION_NONE, TRANSACTION_READ_COMMITTED, TRANSACTION_READ_UNCOMMITTED, TRANSACTION_REPEATABLE_READ, TRANSACTION_SERIALIZABLE
 
Constructor Summary
MultiConnection(MultiPool parent, Connection con)
           
 
Method Summary
protected  boolean checkAfterException()
          Tries to fix the this connection, if it proves to be broken.
 boolean checkSQLError(Exception e)
           
 void claim()
           
 void close()
          Close connections
 Statement createStatement()
          createStatement returns an SQL Statement object
 Statement createStatement(int resultSetType, int resultSetConcurrency)
          createStatement returns an SQL Statement object
 Statement createStatement(int type, int concurrency, int holdability)
          
 String getLastSQL()
           
 Connection getRealConnection()
          Return the underlying real connection.
 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.
 String getStateString()
           
 int getUsage()
           
 void realclose()
          Close connections
 void release()
           
 void setAutoCommit(boolean enableAutoCommit)
           If "autoCommit" is true, then all subsequent SQL statements will be executed and committed as individual transactions.
 void setLastSQL(String sql)
          Called just before every prepare statement.
 String toString()
           
 
Methods inherited from class org.mmbase.module.database.ConnectionWrapper
clearWarnings, commit, getAutoCommit, getCatalog, getHoldability, getMetaData, getTransactionIsolation, getTypeMap, getWarnings, isClosed, isReadOnly, nativeSQL, prepareCall, prepareCall, prepareCall, prepareStatement, prepareStatement, prepareStatement, prepareStatement, prepareStatement, prepareStatement, releaseSavepoint, rollback, rollback, setCatalog, setHoldability, setReadOnly, setSavepoint, setSavepoint, setTransactionIsolation, setTypeMap
 
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

queries

public static long queries

state

public int state
Constructor Detail

MultiConnection

public MultiConnection(MultiPool parent,
                       Connection con)
Since:
MMBase-1.7
To Do:
in 1.7 this method was made public,document why?
Warning: No Java Documentation Available.
Method Detail

getStateString

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

setLastSQL

public void setLastSQL(String sql)
Description copied from class: ConnectionWrapper
Called just before every prepare statement. Can be overridden, because this default implementation is empty.

Overrides:
setLastSQL in class ConnectionWrapper
Warning: No Java Documentation Available.

getLastSQL

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

createStatement

public Statement createStatement()
                          throws SQLException
createStatement returns an SQL Statement object

Specified by:
createStatement in interface Connection
Overrides:
createStatement in class ConnectionWrapper
Throws:
SQLException

checkAfterException

protected boolean checkAfterException()
                               throws 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:
SQLException - If connection is broken and no new one could be obtained.
Since:
MMBase-1.7.1

setAutoCommit

public void setAutoCommit(boolean enableAutoCommit)
                   throws 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 Connection
Overrides:
setAutoCommit in class ConnectionWrapper
Throws:
SQLException

close

public void close()
           throws SQLException
Close connections

Specified by:
close in interface Connection
Overrides:
close in class ConnectionWrapper
Throws:
SQLException

realclose

public void realclose()
               throws SQLException
Close connections

Throws:
SQLException

checkSQLError

public boolean checkSQLError(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 String toString()
Warning: No Java Documentation Available.

createStatement

public Statement createStatement(int resultSetType,
                                 int resultSetConcurrency)
                          throws SQLException
createStatement returns an SQL Statement object

Specified by:
createStatement in interface Connection
Overrides:
createStatement in class ConnectionWrapper
Throws:
SQLException

createStatement

public Statement createStatement(int type,
                                 int concurrency,
                                 int holdability)
                          throws SQLException

Specified by:
createStatement in interface Connection
Overrides:
createStatement in class ConnectionWrapper
Throws:
SQLException
Since:
MMBase 1.5, JDBC 1.4

getRealConnection

public Connection getRealConnection()
Return the underlying real connection. NOTE: use with extreme caution! MMBase is supposed to look after it's own connections. This method is public only for the reason that specific database implementations need access to this connection in order to safely clear them before they can be put back in the connection pool.



MMBase build 1.8.1.20060716