org.apache.myfaces.orchestra.connectionManager
Class ConnectionManagerDataSource

java.lang.Object
  extended by org.apache.myfaces.orchestra.connectionManager.ConnectionManagerDataSource
All Implemented Interfaces:
Wrapper, CommonDataSource, DataSource

public class ConnectionManagerDataSource
extends Object
implements DataSource

Manage all borrowed connections and hand out DisconnectableConnection objects so that we can close them again after the HTTP request has been finished.

This datasource can be configured as a "wrapper" for a real datasource. When a connection is requested from this object, a proxy is returned that simply forwards all calls transparently to the real connection. This manager keeps track of all the Connections borrowed by each thread. At some point (eg from a servlet filter) this object can be asked to check for unreturned connections held by the current thread. If any exist then the real connection is returned to the underlying datasource and the proxy's connection reference is set to null. This ensures that a thread cannot leak real database connections.

Of course all code should return its connections; this is only a workaround/hack useful when the real problem cannot be fixed. This is particularly useful for JPA implementations that do not free their connection again after a lazy-init.

See Also:
DisconnectableConnection

Constructor Summary
ConnectionManagerDataSource()
           
 
Method Summary
 Connection getConnection()
           
 Connection getConnection(String username, String password)
           
 DataSource getDataSource()
           
 int getLoginTimeout()
           
 PrintWriter getLogWriter()
           
 boolean isWrapperFor(Class iface)
           
 void onBeforeReleaseConnection(Connection con)
           
static void releaseAllBorrowedConnections()
           
 void setDataSource(DataSource dataSource)
           
 void setJndiName(String jndiName)
           
 void setListeners(ConnectionManagerListener[] listeners)
           
 void setLoginTimeout(int seconds)
           
 void setLogWriter(PrintWriter out)
           
 Object unwrap(Class iface)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ConnectionManagerDataSource

public ConnectionManagerDataSource()
Method Detail

onBeforeReleaseConnection

public void onBeforeReleaseConnection(Connection con)

releaseAllBorrowedConnections

public static void releaseAllBorrowedConnections()

setListeners

public void setListeners(ConnectionManagerListener[] listeners)

setDataSource

public void setDataSource(DataSource dataSource)

getDataSource

public DataSource getDataSource()

setJndiName

public void setJndiName(String jndiName)

getConnection

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

getConnection

public Connection getConnection(String username,
                                String password)
                         throws SQLException
Specified by:
getConnection in interface DataSource
Throws:
SQLException

getLogWriter

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

setLogWriter

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

setLoginTimeout

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

getLoginTimeout

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

unwrap

public Object unwrap(Class iface)
              throws SQLException
Specified by:
unwrap in interface Wrapper
Throws:
SQLException

isWrapperFor

public boolean isWrapperFor(Class iface)
                     throws SQLException
Specified by:
isWrapperFor in interface Wrapper
Throws:
SQLException


Copyright © 2012 The Apache Software Foundation. All Rights Reserved.