org.mentawai.db
Class DBCPConnectionHandler

java.lang.Object
  extended by org.mentawai.db.AbstractConnectionHandler
      extended by org.mentawai.db.DBCPConnectionHandler
All Implemented Interfaces:
org.mentacontainer.Factory, org.mentacontainer.Interceptor<Connection>, ConnectionHandler

public class DBCPConnectionHandler
extends AbstractConnectionHandler

A connection handler that uses the Commons DBCP connection pool. (http://jakarta.apache.org/commons/dbcp/) To use this class you must have the DBCP jars in your /WEB-INF/lib directory. You may access the underlying DBCP's BasicDataSource to configure the pool, before you start calling getConnection().

Author:
Sergio Oliveira

Nested Class Summary
 
Nested classes/interfaces inherited from interface org.mentawai.db.ConnectionHandler
ConnectionHandler.Exec
 
Constructor Summary
DBCPConnectionHandler(String driver, String url, String user, String pass)
          Constructs a DBCPConnectionHandler with DBCP's BasicDataSource.
 
Method Summary
 void destroy()
          Destroy the connection pool, closing and cleaning all connections.
 DataSource getBasicDataSource()
          Gets the underlying DBCP's BasicDataSource.
 Object getBean()
           
 Connection getConnection()
          Returns a Connection to the database.
 String getStatus()
          Print the status of this connection pool if any.
 void release(Connection conn)
          Release this connection.
 
Methods inherited from class org.mentawai.db.AbstractConnectionHandler
exec, getInstance, getType, onCleared, onCreated
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DBCPConnectionHandler

public DBCPConnectionHandler(String driver,
                             String url,
                             String user,
                             String pass)
Constructs a DBCPConnectionHandler with DBCP's BasicDataSource.

Parameters:
driver - The JDBC driver class name to use.
url - The JDBC url to connect to the database.
user - The database username to use.
pass - The database password to use.
Throws:
IllegalStateException - If the Commons DBCP jars are not in the /WEB-INF/lib directory or if the JDBC driver cannot be loaded.
Method Detail

getStatus

public String getStatus()
Description copied from interface: ConnectionHandler
Print the status of this connection pool if any.

Returns:
The status

getBasicDataSource

public DataSource getBasicDataSource()
Gets the underlying DBCP's BasicDataSource. You should cast this data source to DBCP's BasicDataSource.

Returns:
The BasicDataSource as a DataSource.

getConnection

public Connection getConnection()
                         throws SQLException
Description copied from interface: ConnectionHandler
Returns a Connection to the database. Notice that it can never return null. If no connection can be acquired, then a SQLException is thrown.

Returns:
A connection to the database.
Throws:
SQLException - if there were problems trying to acquire the connection.

getBean

public Object getBean()
               throws InstantiationException
Throws:
InstantiationException

release

public void release(Connection conn)
Description copied from interface: ConnectionHandler
Release this connection. If this connection handler is using a database pool, the connection is returned to the pool. If this connection handler is not using a database pool, the connection is closed. If the connection passed as an argument is null, nothing is done.

Parameters:
conn - The connection to release.

destroy

public void destroy()
Description copied from interface: ConnectionHandler
Destroy the connection pool, closing and cleaning all connections. This is important for restarting the web context.



Copyright © 2013. All Rights Reserved.