org.ow2.jonas.dbm.internal.cm
Class ConnectionManager

java.lang.Object
  extended by org.ow2.jonas.dbm.internal.cm.ConnectionManager
All Implemented Interfaces:
EventListener, Referenceable, ConnectionEventListener, DataSource, XADataSource, Pool

public class ConnectionManager
extends Object
implements DataSource, XADataSource, Referenceable, Pool, ConnectionEventListener

DataSource implementation. Manage a pool of connections.

Author:
durieuxp

Constructor Summary
ConnectionManager()
          Deprecated.  
ConnectionManager(boolean isClient)
          Always called with true
ConnectionManager(TransactionService txs)
           
 
Method Summary
 void adjust()
          Adjust the pool size, according to poolMax and poolMin values.
 String checkConnection(String testStatement)
          Check on a connection the test statement
 void closeAllConnection()
          Close all connections in the pool, when server is shut down.
 void connectionClosed(ConnectionEvent event)
           
 void connectionErrorOccurred(ConnectionEvent event)
           
 void forceCloseConnection(int connectionId)
           
 void freeConnections(Transaction tx)
          The transaction has committed (or rolled back).
 int getAdjustPeriod()
           
 int getBusyMaxRecent()
           
 int getBusyMinRecent()
           
 int getCheckLevel()
           
 String getClassName()
           
 Connection getConnection()
           
 Connection getConnection(String username, String password)
          Attempts to establish a connection with the data source that this DataSource object represents.
 Map getConnectionDetails(int connectionId)
           
 int getConnectionFailures()
           
 int getConnectionLeaks()
           
static ConnectionManager getConnectionManager(String dsname)
          get the ConnectionManager matching the DataSource name
 int getCurrentBusy()
           
 int getCurrentInTx()
           
 int getCurrentOpened()
           
 int getCurrentWaiters()
           
 String getDataSourceDescription()
           
 String getDatasourceName()
           
 String getDSName()
           
 int getLoginTimeout()
           
 PrintWriter getLogWriter()
           
 String getMapperName()
           
 int getMaxAge()
           
 long getMaxAgeMilli()
           
 int getMaxOpenTime()
           
 long getMaxOpenTimeMilli()
           
 int getMaxWaiters()
           
 int getMaxWaitTime()
           
 int[] getOpenedConnections()
           
 int[] getOpenedConnections(int usedTimeSec)
           
 int getOpenedCount()
           
 String getPassword()
           
 Pool getPool()
           
 int getPoolMax()
           
 int getPoolMin()
           
 int getPstmtMax()
           
 Reference getReference()
          Retrieves the Reference of this object.
 int getRejectedFull()
           
 int getRejectedOpen()
           
 int getRejectedOther()
           
 int getRejectedTimeout()
           
 int getSamplingPeriod()
           
 int getServedOpen()
           
 String getTestStatement()
           
 String getTransactionIsolation()
           
 String getUrl()
           
 String getUserName()
           
 int getWaiterCount()
           
 int getWaitersHigh()
           
 int getWaitersHighRecent()
           
 long getWaitingHigh()
           
 long getWaitingHighRecent()
           
 long getWaitingTime()
           
 XAConnection getXAConnection()
           
 XAConnection getXAConnection(String user, String passwd)
          Attempts to establish a physical database connection, using the given user name and password.
 boolean isClientCase()
          This manager is in the client case or not ?
 boolean isObservable()
           
 JManagedConnection openConnection(String user, Transaction tx)
          lookup connection in the pool for this user/tx
 void poolConfigure(String connchecklevel, String connmaxage, String maxopentime, String connteststmt, String pstmtmax, String minconpool, String maxconpool, String maxwaittime, String maxwaiters, String samplingperiod, String adjustperiod)
          Configure the Connection pool.
 void recomputeBusy()
          compute current min/max busyConnections
 void sampling()
          make samples with some monitoring values
 void setAdjustPeriod(int sec)
           
 void setCheckLevel(int level)
           
 void setClassName(String s)
           
 void setDataSourceDescription(String dsDesc)
           
 void setDatasourceName(String s)
           
 void setDSName(String s)
           
 void setLoginTimeout(int seconds)
           
 void setLogWriter(PrintWriter out)
           
 void setMapperName(String mappername)
           
 void setMaxAge(int mn)
           
 void setMaxOpenTime(int mn)
           
 void setMaxWaiters(int nb)
           
 void setMaxWaitTime(int sec)
           
 void setObservable(boolean observable)
           
 void setPassword(String s)
           
 void setPoolMax(int max)
           
 void setPoolMin(int min)
           
 void setPstmtMax(int nb)
           
 void setSamplingPeriod(int sec)
           
 void setTestStatement(String s)
           
 void setTransactionIsolation(String level)
           
 void setUrl(String s)
           
 void setUserName(String s)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ConnectionManager

@Deprecated
public ConnectionManager()
                  throws Exception
Deprecated. 

Constructor for Factory

Throws:
Exception

ConnectionManager

public ConnectionManager(boolean isClient)
                  throws Exception
Always called with true

Throws:
Exception

ConnectionManager

public ConnectionManager(TransactionService txs)
                  throws Exception
Throws:
Exception
Method Detail

isClientCase

public boolean isClientCase()
This manager is in the client case or not ?

Returns:
boolean true if this is the client case TODO : must be removed. A client can lookup a datasource

getPool

public Pool getPool()
Returns:
The pool associated to this datasource

getConnectionManager

public static ConnectionManager getConnectionManager(String dsname)
get the ConnectionManager matching the DataSource name


getDSName

public String getDSName()
Returns:
Jndi name of the datasource

setDSName

public void setDSName(String s)
Parameters:
s - Jndi name for the datasource

getDatasourceName

public String getDatasourceName()

setDatasourceName

public void setDatasourceName(String s)

getUrl

public String getUrl()

setUrl

public void setUrl(String s)

getClassName

public String getClassName()

setClassName

public void setClassName(String s)
                  throws ClassNotFoundException
Throws:
ClassNotFoundException

getUserName

public String getUserName()

setUserName

public void setUserName(String s)

getPassword

public String getPassword()

setPassword

public void setPassword(String s)

setTransactionIsolation

public void setTransactionIsolation(String level)

getTransactionIsolation

public String getTransactionIsolation()

setMapperName

public void setMapperName(String mappername)

getMapperName

public String getMapperName()

getDataSourceDescription

public String getDataSourceDescription()
Returns:
the desrciption of this datasource

setDataSourceDescription

public void setDataSourceDescription(String dsDesc)
Parameters:
dsDesc - the desrciption of this datasource

getPoolMin

public int getPoolMin()
Specified by:
getPoolMin in interface Pool
Returns:
min pool size

setPoolMin

public void setPoolMin(int min)
Specified by:
setPoolMin in interface Pool
Parameters:
min - minimum connection pool size to be set.

getPoolMax

public int getPoolMax()
Specified by:
getPoolMax in interface Pool
Returns:
actual max pool size

setPoolMax

public void setPoolMax(int max)
Specified by:
setPoolMax in interface Pool
Parameters:
max - max pool size. -1 means "no limit".

getMaxAge

public int getMaxAge()
Specified by:
getMaxAge in interface Pool
Returns:
max age for connections (in mm)

getMaxAgeMilli

public long getMaxAgeMilli()
Specified by:
getMaxAgeMilli in interface Pool
Returns:
max age for connections (in millisec)

setMaxAge

public void setMaxAge(int mn)
Specified by:
setMaxAge in interface Pool
Parameters:
mn - max age of connection in minutes

getMaxOpenTime

public int getMaxOpenTime()
Specified by:
getMaxOpenTime in interface Pool
Returns:
max age for connections (in mns)

getMaxOpenTimeMilli

public long getMaxOpenTimeMilli()
Specified by:
getMaxOpenTimeMilli in interface Pool
Returns:
max age for connections (in millisecs)

setMaxOpenTime

public void setMaxOpenTime(int mn)
Specified by:
setMaxOpenTime in interface Pool
Parameters:
mn - max time of open connection in minutes

getMaxWaitTime

public int getMaxWaitTime()
Specified by:
getMaxWaitTime in interface Pool
Returns:
waiter timeout in seconds

setMaxWaitTime

public void setMaxWaitTime(int sec)
Specified by:
setMaxWaitTime in interface Pool
Parameters:
sec - max time to wait for a connection, in seconds

getMaxWaiters

public int getMaxWaiters()
Specified by:
getMaxWaiters in interface Pool
Returns:
max nb of waiters

setMaxWaiters

public void setMaxWaiters(int nb)
Specified by:
setMaxWaiters in interface Pool
Parameters:
nb - max nb of waiters

getSamplingPeriod

public int getSamplingPeriod()
Specified by:
getSamplingPeriod in interface Pool
Returns:
sampling period in sec.

setSamplingPeriod

public void setSamplingPeriod(int sec)
Specified by:
setSamplingPeriod in interface Pool
Parameters:
sec - sampling period in sec.

getAdjustPeriod

public int getAdjustPeriod()
Specified by:
getAdjustPeriod in interface Pool
Returns:
adjust period in sec.

setAdjustPeriod

public void setAdjustPeriod(int sec)
Specified by:
setAdjustPeriod in interface Pool
Parameters:
sec - adjust period in sec.

getCheckLevel

public int getCheckLevel()
Specified by:
getCheckLevel in interface Pool
Returns:
connection checking level

setCheckLevel

public void setCheckLevel(int level)
Specified by:
setCheckLevel in interface Pool
Parameters:
level - jdbc connection checking level (0, 1, or 2)

getPstmtMax

public int getPstmtMax()
Returns:
PreparedStatement cache size

setPstmtMax

public void setPstmtMax(int nb)
Parameters:
nb - PreparedStatement cache size

getTestStatement

public String getTestStatement()
Specified by:
getTestStatement in interface Pool
Returns:
test statement used when checkLevel = 2.

setTestStatement

public void setTestStatement(String s)
Specified by:
setTestStatement in interface Pool
Parameters:
s - test statement

isObservable

public boolean isObservable()
Returns:
the observable

setObservable

public void setObservable(boolean observable)
Parameters:
observable - the observable to set

poolConfigure

public void poolConfigure(String connchecklevel,
                          String connmaxage,
                          String maxopentime,
                          String connteststmt,
                          String pstmtmax,
                          String minconpool,
                          String maxconpool,
                          String maxwaittime,
                          String maxwaiters,
                          String samplingperiod,
                          String adjustperiod)
Configure the Connection pool. Called by the Container at init. Configuration can be set in datasource.properties files.

Parameters:
connchecklevel - JDBC connection checking level
connmaxage - JDBC connection maximum age
maxopentime - JDBC connection maximum open time
connteststmt - SQL query for test statement
pstmtmax - prepare statement pool size per managed connection
minconpool - Min size for the connection pool
maxconpool - Max size for the connection pool
maxwaittime - Max time to wait for a connection (in seconds)
maxwaiters - Max nb of waiters for a connection
samplingperiod - sampling period in sec.
adjustperiod - pool adjust period in sec.

getBusyMaxRecent

public int getBusyMaxRecent()
Specified by:
getBusyMaxRecent in interface Pool
Returns:
maximum nb of busy connections in last sampling period

getBusyMinRecent

public int getBusyMinRecent()
Specified by:
getBusyMinRecent in interface Pool
Returns:
minimum nb of busy connections in last sampling period

getCurrentWaiters

public int getCurrentWaiters()
Specified by:
getCurrentWaiters in interface Pool
Returns:
current number of connection waiters

getOpenedCount

public int getOpenedCount()
Specified by:
getOpenedCount in interface Pool
Returns:
int number of physical jdbc connection opened

getConnectionFailures

public int getConnectionFailures()
Specified by:
getConnectionFailures in interface Pool
Returns:
int number of xa connection failures on open

getConnectionLeaks

public int getConnectionLeaks()
Specified by:
getConnectionLeaks in interface Pool
Returns:
int number of connection leaks

getServedOpen

public int getServedOpen()
Specified by:
getServedOpen in interface Pool
Returns:
int number of xa connection served

getRejectedFull

public int getRejectedFull()
Specified by:
getRejectedFull in interface Pool
Returns:
int number of open calls that were rejected due to waiter overflow

getRejectedTimeout

public int getRejectedTimeout()
Specified by:
getRejectedTimeout in interface Pool
Returns:
int number of open calls that were rejected by timeout

getRejectedOther

public int getRejectedOther()
Specified by:
getRejectedOther in interface Pool
Returns:
int number of open calls that were rejected

getRejectedOpen

public int getRejectedOpen()
Specified by:
getRejectedOpen in interface Pool
Returns:
int number of open calls that were rejected

getWaitersHigh

public int getWaitersHigh()
Specified by:
getWaitersHigh in interface Pool
Returns:
maximum nb of waiters since the datasource creation

getWaitersHighRecent

public int getWaitersHighRecent()
Specified by:
getWaitersHighRecent in interface Pool
Returns:
maximum nb of waiters in last sampling period

getWaiterCount

public int getWaiterCount()
Specified by:
getWaiterCount in interface Pool
Returns:
total nb of waiters since the datasource creation

getWaitingTime

public long getWaitingTime()
Specified by:
getWaitingTime in interface Pool
Returns:
total waiting time since the datasource creation

getWaitingHigh

public long getWaitingHigh()
Specified by:
getWaitingHigh in interface Pool
Returns:
max waiting time since the datasource creation

getWaitingHighRecent

public long getWaitingHighRecent()
Specified by:
getWaitingHighRecent in interface Pool
Returns:
max waiting time in last sampling period

getLoginTimeout

public int getLoginTimeout()
                    throws SQLException
Specified by:
getLoginTimeout in interface DataSource
Specified by:
getLoginTimeout in interface XADataSource
Throws:
SQLException

setLoginTimeout

public void setLoginTimeout(int seconds)
                     throws SQLException
Specified by:
setLoginTimeout in interface DataSource
Specified by:
setLoginTimeout in interface XADataSource
Throws:
SQLException

getLogWriter

public PrintWriter getLogWriter()
                         throws SQLException
Specified by:
getLogWriter in interface DataSource
Specified by:
getLogWriter in interface XADataSource
Throws:
SQLException

setLogWriter

public void setLogWriter(PrintWriter out)
                  throws SQLException
Specified by:
setLogWriter in interface DataSource
Specified by:
setLogWriter in interface XADataSource
Throws:
SQLException

getConnection

public Connection getConnection()
                         throws SQLException
Specified by:
getConnection in interface DataSource
Throws:
SQLException

getConnection

public Connection getConnection(String username,
                                String password)
                         throws SQLException
Attempts to establish a connection with the data source that this DataSource object represents. - comes from the javax.sql.DataSource interface

Specified by:
getConnection in interface DataSource
Parameters:
username - - the database user on whose behalf the connection is being made
password - - the user's password
Returns:
a connection to the data source
Throws:
SQLException - - if a database access error occurs

getXAConnection

public XAConnection getXAConnection()
                             throws SQLException
Specified by:
getXAConnection in interface XADataSource
Throws:
SQLException

getXAConnection

public XAConnection getXAConnection(String user,
                                    String passwd)
                             throws SQLException
Attempts to establish a physical database connection, using the given user name and password. The connection that is returned is one that can be used in a distributed transaction - comes from the javax.sql.XADataSource interface

Specified by:
getXAConnection in interface XADataSource
Parameters:
user - - the database user on whose behalf the connection is being made
passwd - - the user's password
Returns:
an XAConnection object, which represents a physical connection to a data source, that can be used in a distributed transaction
Throws:
SQLException - - if a database access error occurs

getReference

public Reference getReference()
                       throws NamingException
Retrieves the Reference of this object. Used at binding time by JNDI to build a reference on this object.

Specified by:
getReference in interface Referenceable
Returns:
The non-null Reference of this object.
Throws:
NamingException - If a naming exception was encountered while retrieving the reference.

getOpenedConnections

public int[] getOpenedConnections(int usedTimeSec)

forceCloseConnection

public void forceCloseConnection(int connectionId)

getOpenedConnections

public int[] getOpenedConnections()

getConnectionDetails

public Map getConnectionDetails(int connectionId)

connectionClosed

public void connectionClosed(ConnectionEvent event)
Specified by:
connectionClosed in interface ConnectionEventListener

connectionErrorOccurred

public void connectionErrorOccurred(ConnectionEvent event)
Specified by:
connectionErrorOccurred in interface ConnectionEventListener

getCurrentOpened

public int getCurrentOpened()
Specified by:
getCurrentOpened in interface Pool
Returns:
int number of xa connection

getCurrentBusy

public int getCurrentBusy()
Specified by:
getCurrentBusy in interface Pool
Returns:
int number of busy xa connection

recomputeBusy

public void recomputeBusy()
compute current min/max busyConnections


getCurrentInTx

public int getCurrentInTx()
Specified by:
getCurrentInTx in interface Pool
Returns:
int number of xa connection reserved for tx

sampling

public void sampling()
make samples with some monitoring values


adjust

public void adjust()
Adjust the pool size, according to poolMax and poolMin values. Also remove old connections in the freeList.


openConnection

public JManagedConnection openConnection(String user,
                                         Transaction tx)
                                  throws SQLException
lookup connection in the pool for this user/tx

Parameters:
user - user name
tx - Transaction the connection is involved
Returns:
a free JManagedConnection (never null)
Throws:
SQLException - Cannot open a connection because the pool's max size is reached

freeConnections

public void freeConnections(Transaction tx)
The transaction has committed (or rolled back). We can return its connections to the pool of available connections.

Parameters:
tx - the non null transaction

closeAllConnection

public void closeAllConnection()
Close all connections in the pool, when server is shut down.


checkConnection

public String checkConnection(String testStatement)
                       throws SQLException
Check on a connection the test statement

Specified by:
checkConnection in interface Pool
Parameters:
testStatement - the statement to use for test
Returns:
the test statement if the test succeeded, an error message otherwise
Throws:
SQLException - If an error occured when trying to test (not due to the test itself, but to other preliminary or post operation).


Copyright © 2010 OW2 Consortium. All Rights Reserved.