public class ConnectionPool extends Object
Rather than allocating a new connection every time we access the database, we ask the connection pool for a connection and return it to the connection pool when we're finished. The connection pool will save the connection for for subsequent reuse. If there are no open connections to hand out, the connection pool opens another one. With some JDBC implementations, idle connections eventually fail. So, this connection pool closes idle connections. (See implementation for the current settings to determine how long idle connections remain open.)
This connection pool coordinates with the Transaction class. The getTransactionConnection() method can be used to get the connection that is currently being used in the current transaction. This enables you to execute your own SQL statements in the same transaction and other GenericDAO calls.
Modifier and Type | Field and Description |
---|---|
static long |
DEFAULT_MAX_IDLE_TIME
Default max idle time for connections.
|
Constructor and Description |
---|
ConnectionPool(String jdbcDriverName,
String jdbcURL) |
ConnectionPool(String jdbcDriverName,
String jdbcURL,
String user,
String password) |
Modifier and Type | Method and Description |
---|---|
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.
|
public static final long DEFAULT_MAX_IDLE_TIME
public Connection getConnection() throws SQLException
SQLException
public PrintWriter getDebugWriter()
public String getDriverName()
public boolean getLowerCaseColumnNames()
public long getMaxIdleTime()
public Connection getTransactionConnection() throws RollbackException
The connection is returned to the connection pool when the
transaction is committed or rolled back. So do not call
releaseConnection()
for connections in a transaction and
do not close the connection yourself.
You must use Transaction.commit()
(or rollback) rather than
connection.commit()
to end the transaction.
Note that current only one connection pool can be used in any transaction (because the ACID properties cannot be guaranteed across SQL connections without a lot of work and complication, e.g., using a two-phase commit protocol).
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.public String getURL()
public String getUserName()
public void releaseConnection(Connection c)
c
- connection to return to the connection pool.public void setDebugOutput(OutputStream out)
Transaction.commit()
)
to print out messages showing the SQL they are generating.out
- an output stream to which debugging info will be written.
(Usually System.out
is passed as the parameter.)public void setMaxIdleTime(long millis)
millis
- time a connection can be idle before it will be closedCopyright © 2012-2016 Jeffrey L. Eppinger. All rights reserved. Permission granted for educational use only.