public class DBPoolImpl extends java.lang.Object implements AlarmListener, EnvironmentListener
Like JDBC 2.0 pooling, DBPool returns a wrapped Connection.
Applications can use that connection just like an unpooled connection.
It is more important than ever to close()
the connection,
because the close returns the connection to the connection pool.
Context env = (Context) new InitialContext().lookup("java:comp/env");
DataSource pool = (DataSource) env.lookup("jdbc/test");
Connection conn = pool.getConnection();
try {
... // normal connection stuff
} finally {
conn.close();
}
<database name='jdbc/test'>
<init>
<driver>postgresql.Driver</driver>
<url>jdbc:postgresql://localhost/test</url>
<user>ferg</user>
<password>foobar</password>
</init>
</database>
getMaxConnection
connections are already active,
getPooledConnection
will block waiting for an available
connection. The wait is timed. If connection-wait-time passes
and there is still no connection, getPooledConnection
create a new connection anyway.
Connections will only stay in the pool for about 5 seconds. After that they will be removed and closed. This reduces the load on the DB and also protects against the database dropping old connections.
Modifier and Type | Field and Description |
---|---|
protected static java.util.logging.Logger |
log |
static java.lang.String |
PROPERTY_PASSWORD
The key used to look into the properties passed to the
connect method to find the password.
|
static java.lang.String |
PROPERTY_USER
The key used to look into the properties passed to the
connect method to find the username.
|
Constructor and Description |
---|
DBPoolImpl()
Null constructor for the Driver interface; called by the JNDI
configuration.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Close the pool, closing the connections.
|
void |
closeIdleConnections()
Closes the idle connections in the pool.
|
DriverConfig |
createBackupDriver()
Returns the driver config.
|
ConnectionConfig |
createConnection()
Creates the connection config.
|
DriverConfig |
createDriver()
Returns the driver config.
|
void |
environmentBind(EnvironmentClassLoader loader)
Callback when the environment binds.
|
void |
environmentConfigure(EnvironmentClassLoader loader)
Callback when the environment configures.
|
void |
environmentStart(EnvironmentClassLoader loader)
Callback when the environment starts.
|
void |
environmentStop(EnvironmentClassLoader loader)
Callback when the class loader dies.
|
void |
forceClose()
Close all the connections in the pool.
|
ConnectionConfig |
getConnectionConfig()
Returns the connection config.
|
long |
getConnectionWaitTime()
Gets the time to wait for a connection when all are used.
|
JdbcDriverMXBean[] |
getDriverAdmin()
Returns the driver admin
|
int |
getLoginTimeout()
Gets the timeout for a database login.
|
java.io.PrintWriter |
getLogWriter()
Sets the debugging log for the connection.
|
long |
getMaxActiveTime()
Get the time in milliseconds a connection can remain active.
|
int |
getMaxCloseStatements()
Gets the max statement.
|
int |
getMaxConnections()
Get the maximum number of pooled connections.
|
long |
getMaxIdleTime()
Get the time in milliseconds a connection will remain in the pool before
being closed.
|
int |
getMaxOverflowConnections()
The number of connections to overflow if the connection pool fills
and there's a timeout.
|
long |
getMaxPoolTime()
Get the time in milliseconds a connection will remain in the pool before
being closed.
|
java.lang.String |
getName()
Returns the Pool's name
|
java.lang.String |
getPassword()
Returns the connection's password
|
long |
getPingInterval()
Gets how often the ping for ping-on-idle
|
boolean |
getPingOnIdle()
If true, the pool will ping in the idle pool.
|
boolean |
getPingOnReuse()
If true, the pool will ping when attempting to reuse a connection.
|
java.lang.String |
getPingQuery()
Returns the ping query.
|
java.lang.String |
getPingTable()
Get the table to 'ping' to see if the connection is still live.
|
int |
getPreparedStatementCacheSize()
Returns the prepared statement cache size.
|
SpyDataSource |
getSpyDataSource()
Returns the next spy id.
|
int |
getTotalConnections()
Get the total number of connections
|
long |
getTransactionTimeout()
Gets the transaction timeout.
|
java.lang.String |
getURL() |
java.lang.String |
getUser()
Returns the connection's user.
|
void |
handleAlarm(Alarm alarm)
At the alarm, close all connections which have been sitting in
the pool for too long.
|
void |
init()
Initialize the pool.
|
boolean |
isClosed()
Returns true if the pool is closed.
|
boolean |
isCommitOnTimeout() |
boolean |
isLocalTransaction()
Returns true if there is a valid local transactino associated
with the database.
|
boolean |
isPing()
Returns true if pinging is enabled.
|
boolean |
isSpy()
Return true for a spy.
|
boolean |
isTransactional()
Returns true if the pool supports transactions.
|
boolean |
isWrapStatements()
Sets true if statements should be wrapped.
|
boolean |
isXA()
Returns true if this is transactional.
|
boolean |
isXAForbidSameRM()
Returns true if transactions should force isSameRM to be false.
|
boolean |
isXATransaction()
Returns true if there is a valid XAResource associated
with the database.
|
java.lang.String |
newSpyId(DriverConfig driver)
Returns the next spy id.
|
void |
setCommitOnTimeout(boolean commitOnTimeout) |
void |
setConnectionWaitTime(Period waitTime)
Sets the time to wait for a connection when all are used.
|
void |
setInitParam(InitParam init)
Sets a driver parameter.
|
void |
setJDBCDriver(java.sql.Driver jdbcDriver)
Sets the jdbc-driver config.
|
void |
setLoginTimeout(int seconds)
Sets the timeout for a database login.
|
void |
setLogWriter(java.io.PrintWriter out)
Sets the debugging log for the connection.
|
void |
setMaxActiveTime(Period maxActiveTime)
Set the time in milliseconds a connection can remain active.
|
void |
setMaxCloseStatements(int max)
Sets the max statement.
|
void |
setMaxConnections(int maxConnections)
Sets the maximum number of pooled connections.
|
void |
setMaxIdleTime(Period idleTime)
Set the time in milliseconds a connection will remain in the pool before
being closed.
|
void |
setMaxOverflowConnections(int maxOverflowConnections)
The number of connections to overflow if the connection pool fills
and there's a timeout.
|
void |
setMaxPoolTime(Period maxPoolTime)
Set the time in milliseconds a connection will remain in the pool before
being closed.
|
void |
setName(java.lang.String name)
Sets the Pool's name.
|
void |
setPassword(java.lang.String password)
Sets the connection's password
|
void |
setPing(boolean ping)
Set true if pinging is enabled.
|
void |
setPingInterval(Period interval)
Sets the time to ping for ping-on-idle
|
void |
setPingOnIdle(boolean pingOnIdle)
Set the table to 'ping' to see if the connection is still live.
|
void |
setPingOnReuse(boolean pingOnReuse)
Set the table to 'ping' to see if the connection is still live.
|
void |
setPingQuery(java.lang.String pingQuery)
Sets the ping query.
|
void |
setPingTable(java.lang.String pingTable)
Set the table to 'ping' to see if the connection is still live.
|
void |
setPoolDataSource(javax.sql.ConnectionPoolDataSource poolDataSource)
Sets the jdbc-driver config.
|
void |
setPreparedStatementCacheSize(int size)
Sets the prepared statement cache size.
|
void |
setSpy(boolean isSpy)
Set the output for spying.
|
void |
setTransactionManager(TransactionManager tm)
Set the transaction manager for this pool.
|
void |
setTransactionTimeout(Period period)
Sets the transaction timeout.
|
void |
setURL(java.lang.String url)
Sets the jdbc-driver config.
|
void |
setUser(java.lang.String user)
Sets the connection's user.
|
void |
setWrapStatements(boolean isWrap)
Sets true if statements should be wrapped.
|
void |
setXA(boolean isTransactional)
Returns true if this is transactional.
|
void |
setXADataSource(javax.sql.XADataSource xaDataSource)
Sets the jdbc-driver config.
|
void |
setXAForbidSameRM(boolean isXAForbidSameRM)
Returns true if transactions should force isSameRM to be false.
|
java.lang.String |
toString()
Returns a string description of the pool.
|
protected static final java.util.logging.Logger log
public static final java.lang.String PROPERTY_USER
public static final java.lang.String PROPERTY_PASSWORD
public DBPoolImpl()
public java.lang.String getName()
public void setName(java.lang.String name)
public java.lang.String getURL()
public DriverConfig createDriver()
public DriverConfig createBackupDriver()
public void setInitParam(InitParam init)
public void setJDBCDriver(java.sql.Driver jdbcDriver) throws java.sql.SQLException
java.sql.SQLException
public JdbcDriverMXBean[] getDriverAdmin()
public ConnectionConfig createConnection()
public ConnectionConfig getConnectionConfig()
public void setPoolDataSource(javax.sql.ConnectionPoolDataSource poolDataSource) throws java.sql.SQLException
java.sql.SQLException
public void setXADataSource(javax.sql.XADataSource xaDataSource) throws java.sql.SQLException
java.sql.SQLException
public void setURL(java.lang.String url) throws ConfigException
ConfigException
public java.lang.String getUser()
public void setUser(java.lang.String user)
public java.lang.String getPassword()
public void setPassword(java.lang.String password)
public int getMaxConnections()
public void setMaxConnections(int maxConnections)
public int getTotalConnections()
public void setConnectionWaitTime(Period waitTime)
public long getConnectionWaitTime()
public void setMaxOverflowConnections(int maxOverflowConnections)
public int getMaxOverflowConnections()
public void setTransactionTimeout(Period period)
public long getTransactionTimeout()
public void setMaxCloseStatements(int max)
public int getMaxCloseStatements()
public void setWrapStatements(boolean isWrap)
public boolean isWrapStatements()
public int getPreparedStatementCacheSize()
public void setPreparedStatementCacheSize(int size)
public long getMaxIdleTime()
public void setMaxIdleTime(Period idleTime)
public long getMaxPoolTime()
public void setMaxPoolTime(Period maxPoolTime)
public long getMaxActiveTime()
public void setCommitOnTimeout(boolean commitOnTimeout)
public boolean isCommitOnTimeout()
public void setMaxActiveTime(Period maxActiveTime)
public java.lang.String getPingTable()
public void setPingTable(java.lang.String pingTable)
pingTable
- name of the SQL table to ping.public java.lang.String getPingQuery()
public void setPingQuery(java.lang.String pingQuery)
public boolean getPingOnReuse()
public void setPingOnReuse(boolean pingOnReuse)
public boolean getPingOnIdle()
public void setPingOnIdle(boolean pingOnIdle)
public void setPing(boolean ping)
public boolean isPing()
public void setPingInterval(Period interval)
public long getPingInterval()
public void setTransactionManager(TransactionManager tm)
public boolean isXA()
public void setXA(boolean isTransactional)
public boolean isXAForbidSameRM()
public void setXAForbidSameRM(boolean isXAForbidSameRM)
public void setSpy(boolean isSpy)
public boolean isSpy()
public SpyDataSource getSpyDataSource()
public java.lang.String newSpyId(DriverConfig driver)
public boolean isTransactional()
public boolean isXATransaction()
public boolean isLocalTransaction()
public void setLoginTimeout(int seconds) throws java.sql.SQLException
java.sql.SQLException
public int getLoginTimeout() throws java.sql.SQLException
java.sql.SQLException
public void setLogWriter(java.io.PrintWriter out) throws java.sql.SQLException
java.sql.SQLException
public java.io.PrintWriter getLogWriter() throws java.sql.SQLException
java.sql.SQLException
public void init() throws java.lang.Exception
java.lang.Exception
public void closeIdleConnections()
public void handleAlarm(Alarm alarm)
handleAlarm
in interface AlarmListener
alarm
- the alarm event.public void environmentConfigure(EnvironmentClassLoader loader)
environmentConfigure
in interface EnvironmentListener
public void environmentBind(EnvironmentClassLoader loader)
environmentBind
in interface EnvironmentListener
public void environmentStart(EnvironmentClassLoader loader)
environmentStart
in interface EnvironmentListener
public void environmentStop(EnvironmentClassLoader loader)
environmentStop
in interface EnvironmentListener
public boolean isClosed()
public void close()
public void forceClose()
public java.lang.String toString()
toString
in class java.lang.Object