org.genericdao
Class ConnectionPool

java.lang.Object
  extended by org.genericdao.ConnectionPool

public class ConnectionPool
extends Object


Field Summary
static long DEFAULT_MAX_IDLE_TIME
          Default max idle time for connections.
 
Constructor Summary
ConnectionPool(String jdbcDriverName, String jdbcURL)
           
ConnectionPool(String jdbcDriverName, String jdbcURL, String user, String password)
           
 
Method Summary
 Connection getConnection()
           
 PrintWriter getDebugWriter()
           
 String getDriverName()
          Gets the JDBC Driver Name that this connection pool uses.
 boolean getLowerCaseColumnNames()
           
 long getMaxIdleTime()
          Get the maximum time that after which idle database connections are closed.
 Connection getTransactionConnection()
          Get the database connection used by the thread's current transaction.
 String getURL()
          Gets the JDBC URL that this connection pool uses.
 String getUserName()
          Gets the user name that this connection pool uses to log into the database.
 void releaseConnection(Connection c)
          Returns a connection to the connection pool.
 void setDebugOutput(OutputStream out)
          Sets up an output stream to which debugging output can be printed.
 void setMaxIdleTime(long millis)
          Changes the time after which idle database connections are closed.
 String toString()
          Returns a description of this connection pool, include the JDBC Driver Name, the JDBC URL, and the user name used to log into the database.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_MAX_IDLE_TIME

public static final long DEFAULT_MAX_IDLE_TIME
Default max idle time for connections. Value is 5 seconds.

See Also:
Constant Field Values
Constructor Detail

ConnectionPool

public ConnectionPool(String jdbcDriverName,
                      String jdbcURL)

ConnectionPool

public ConnectionPool(String jdbcDriverName,
                      String jdbcURL,
                      String user,
                      String password)
Method Detail

getConnection

public Connection getConnection()
                         throws SQLException
Throws:
SQLException

getDebugWriter

public PrintWriter getDebugWriter()

getDriverName

public String getDriverName()
Gets the JDBC Driver Name that this connection pool uses.

Returns:
the JDBC Driver Name

getLowerCaseColumnNames

public boolean getLowerCaseColumnNames()

getMaxIdleTime

public long getMaxIdleTime()
Get the maximum time that after which idle database connections are closed.

Returns:
maximum idle time in milliseconds

getTransactionConnection

public Connection getTransactionConnection()
                                    throws RollbackException
Get the database connection used by the thread's current transaction. The connection returned is the same one that GenericDAOs using this connection pool would also be using. This permits you to execute SQL statements in the same transaction as GenericDAO calls.

The connection is returned to the connection pool (for you) when the transaction is committed or rolled back.

Note that current only one connection pool can be used in any transaction (because the ACID properties cannot be guaranteed without a lot of work and complication, e.g., using the two-phase commit protocol).

Returns:
a JDBC SQL Connection
Throws:
RollbackException - if there is no active transaction, if there is an underlying SQL exception, or if there is already another connection pool using the currently active transaction. If this exception is thrown, the currently running transaction is rolled back.

getURL

public String getURL()
Gets the JDBC URL that this connection pool uses.

Returns:
the JDBC URL

getUserName

public String getUserName()
Gets the user name that this connection pool uses to log into the database.

Returns:
the user name used to log into the database. Null is return if not user name was provided.

releaseConnection

public void releaseConnection(Connection c)
Returns a connection to the connection pool. The connection should have been obtained using getConnection().

Parameters:
c -

setDebugOutput

public void setDebugOutput(OutputStream out)
Sets up an output stream to which debugging output can be printed.

Parameters:
out -

setMaxIdleTime

public void setMaxIdleTime(long millis)
Changes the time after which idle database connections are closed. The default time is 20 minutes (20*60*1000 milliseconds).

Parameters:
millis - time a connection can be idle before it will be closed

toString

public String toString()
Returns a description of this connection pool, include the JDBC Driver Name, the JDBC URL, and the user name used to log into the database.

Overrides:
toString in class Object
Returns:
a description of this connection pool.


Copyright © 2012 Jeffrey L. Eppinger. All rights reserved. Permission granted for educational use only.