|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object edu.jhu.tmaj.database.conn.PooledConnectionManager
final class PooledConnectionManager
A connection factory that supports and reuses multiple connections to the database in a
thread-safe manner. This class insists that connections be returned with returnConnection()
after they are for SQL statements, otherwise they are lost and will not be used again. We refer
to it as a Connection leak if a client of this class fails to return a connection, and an
OutOfConnections Exception is likely to be thrown.
The default maximum number of simulatenous connections is declared in the
DEFAULT_MAX_CONNECTIONS variable. You may specify the maximum in the constructor. This class
refuses to open more connections than what is specified as the maximum number of connections. If
this class has to wait longer than MAX_TIME_TO_WAIT_FOR_FREE_CONNECTION to get a connection from
the connection pool, an OutOfConnections SQLException will be thrown.
How a Tomcat Server might use this class:
The tomcat server runs an instance of a webapp (like TMAJ), and each time a client connects to
Tomcat, tomcat generates a new thread. (Tomcat does this automatically without any special
settings.) The only piece of data that these threads share are the database connections. Thus
access to the database connections must be done in a thread-safe manner. Database connections
must be shared must generating a new connection for each Request to tomcat would be inefficient
since creating a new connection takes a lot of time.
Constructor Summary | |
---|---|
PooledConnectionManager(ConnectionFactory connectionFactory,
java.lang.String testSqlStatement)
Constructs this object. |
Method Summary | |
---|---|
java.sql.Connection |
getConnection()
Returns a shared connection from the connection pool. |
void |
returnConnection(java.sql.Connection connection)
returns the connection allowing other threads to use it. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public PooledConnectionManager(ConnectionFactory connectionFactory, java.lang.String testSqlStatement)
maxConnections
- the maximum number of connections that this class will hold. Many databases only
allow a user to have so many connections open at once.testSqlStatement
- A SQL statement that will be run each time a new connection is returned for the
purposes of testing the connection. This should be a statement that runs very
fast as it will be run often, and a statement that will not fail. An example
statement might be "SELECT ID FROM Users WHERE 1 = 0". This example statement
would return no rows, but would definitely fail if something were wrong with the
connection.Method Detail |
---|
public java.sql.Connection getConnection() throws java.sql.SQLException
getConnection
in interface ConnectionManager
java.sql.SQLException
- if there is an error making a connection or the class is out of connectionspublic void returnConnection(java.sql.Connection connection)
returnConnection
in interface ConnectionManager
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |