public class DBPool extends java.lang.Object implements javax.sql.DataSource, java.io.Serializable, HandleAware
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 |
Constructor and Description |
---|
DBPool()
Null constructor for the Driver interface; called by the JNDI
configuration.
|
Modifier and Type | Method and Description |
---|---|
void |
add(javax.sql.ConnectionPoolDataSource dataSource)
Adds a preconfigured driver using Java Injection syntax.
|
void |
add(javax.sql.DataSource dataSource)
Adds a preconfigured driver using Java Injection syntax.
|
void |
add(javax.sql.XADataSource dataSource)
Adds a preconfigured driver using Java Injection syntax.
|
void |
addAnnotation(java.lang.annotation.Annotation ann)
Adds an annotation
|
void |
close() |
void |
closeIdleConnections()
Clears the pool.
|
DriverConfig |
createBackupDriver()
Sets a custom driver (or data source)
|
ConnectionConfig |
createConnection()
Configure the initial connection
|
DriverConfig |
createDriver()
Sets a custom driver (or data source)
|
int |
getActiveConnections()
Get the total number of connections in use by the program.
|
java.sql.Connection |
getConnection()
Returns a new or pooled connection.
|
java.sql.Connection |
getConnection(java.lang.String user,
java.lang.String password)
Return a connection.
|
long |
getConnectionWaitTime()
Gets the time to wait for a connection when all are used.
|
JdbcDriverMXBean[] |
getDriverAdmin()
Returns driver admin
|
java.lang.String |
getJndiName()
Returns the Pool's name
|
int |
getLoginTimeout()
Returns the login timeout
|
java.io.PrintWriter |
getLogWriter()
Gets the log writer
|
long |
getMaxActiveTime()
Get the time in milliseconds a connection can remain active.
|
int |
getMaxConnections()
Get the maximum number of pooled connections.
|
int |
getMaxIdleCount()
Get the maximum number of connections in the idle pool.
|
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.
|
int |
getMinIdleCount()
Get the minimum number of connections in the idle pool.
|
java.lang.String |
getName()
Returns the Pool's name
|
java.util.logging.Logger |
getParentLogger() |
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 |
getPingTable()
Get the table to 'ping' to see if the connection is still live.
|
int |
getPreparedStatementCacheSize()
Returns the prepared statement cache size.
|
int |
getTotalConnections()
Get the total number of connections
|
java.lang.String |
getURL()
Return the url
|
java.lang.String |
getUser()
Returns the connection's user (compat).
|
void |
init()
Initialize the pool.
|
boolean |
isSpy()
Returns true for spy
|
boolean |
isWrapperFor(java.lang.Class<?> iface) |
boolean |
isXA()
Returns true if this is transactional.
|
void |
markForPoolRemoval(ManagedConnectionImpl mConn) |
void |
postConstruct()
Initialize the pool.
|
void |
setCloseDanglingConnections(boolean isClose)
Set true if dangling connections should be closed.
|
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 (compat).
|
void |
setJDBCDriver(java.sql.Driver jdbcDriver)
Sets the jdbc-driver config.
|
void |
setJndiName(java.lang.String name)
Sets the Pool's JNDI name.
|
void |
setLoginTimeout(int timeout)
Sets the login timeout
|
void |
setLogWriter(java.io.PrintWriter log)
Sets the log writer
|
void |
setMaxActiveTime(Period maxActiveTime)
Set the time in milliseconds a connection can remain active.
|
void |
setMaxConnections(int maxConnections)
Sets the maximum number of pooled connections.
|
void |
setMaxCreateConnections(int maxCreateConnections)
The number of connections to create at any one time.
|
void |
setMaxIdleCount(int count)
Set the maximum number of connections in the idle pool.
|
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 |
setMinIdleCount(int count)
Set the minimum number of connections in the idle pool.
|
void |
setName(java.lang.String name)
Sets the Pool's WebBeans name.
|
void |
setPassword(java.lang.String password)
Sets the connection's password
|
void |
setPing(boolean ping)
Set the table to 'ping' to see if the connection is still live.
|
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)
Set the query to 'ping' to see if the connection is still live.
|
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 |
setSaveAllocationStackTrace(boolean save)
Set true if the stack trace should be saved on allocation.
|
void |
setSerializationHandle(java.lang.Object handle)
HandleAware callback to set the webbeans handle for serialization
|
void |
setSpy(boolean isSpy)
Set the output for spying.
|
void |
setTransactionManager(TransactionManagerImpl 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 URL
|
void |
setUser(java.lang.String user)
Sets the connection's user.
|
void |
setVar(java.lang.String var)
Sets the Pool's var.
|
void |
setWrapStatements(boolean isWrap)
Set true if statement 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 this is transactional.
|
java.lang.String |
toString()
Returns a string description of the pool.
|
<T> T |
unwrap(java.lang.Class<T> iface) |
public DBPool()
public java.lang.String getJndiName()
public void setJndiName(java.lang.String name)
public void setName(java.lang.String name)
public void setVar(java.lang.String var)
public java.lang.String getName()
public void addAnnotation(java.lang.annotation.Annotation ann)
public DriverConfig createDriver() throws ConfigException
ConfigException
public DriverConfig createBackupDriver() throws ConfigException
ConfigException
public void add(javax.sql.DataSource dataSource)
public void add(javax.sql.ConnectionPoolDataSource dataSource)
public void add(javax.sql.XADataSource dataSource)
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() throws ConfigException
ConfigException
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 getURL()
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) throws ConfigException
ConfigException
public int getTotalConnections()
public void setConnectionWaitTime(Period waitTime)
public long getConnectionWaitTime()
public void setMaxOverflowConnections(int maxOverflowConnections)
public void setMaxCreateConnections(int maxCreateConnections) throws ConfigException
ConfigException
public void setSaveAllocationStackTrace(boolean save)
public void setCloseDanglingConnections(boolean isClose)
public int getMaxOverflowConnections()
public int getActiveConnections()
public int getMaxIdleCount()
public void setMaxIdleCount(int count)
public int getMinIdleCount()
public void setMinIdleCount(int count)
public long getMaxIdleTime()
public void setMaxIdleTime(Period idleTime)
public long getMaxPoolTime()
public void setMaxPoolTime(Period maxPoolTime)
public long getMaxActiveTime()
public void setMaxActiveTime(Period maxActiveTime)
public void setCommitOnTimeout(boolean commitOnTimeout)
public java.lang.String getPingTable()
public void setPingTable(java.lang.String pingTable)
pingTable
- name of the SQL table to ping.public void setPingQuery(java.lang.String pingQuery)
pingQuery
- SQL to use for ping.public boolean getPingOnReuse()
public void setPingOnReuse(boolean pingOnReuse)
public boolean getPingOnIdle()
public void setPingOnIdle(boolean pingOnIdle)
public void setPing(boolean ping)
public void setPingInterval(Period interval)
public long getPingInterval()
public int getPreparedStatementCacheSize()
public void setPreparedStatementCacheSize(int size)
public void setTransactionManager(TransactionManagerImpl tm)
public void setWrapStatements(boolean isWrap)
public void setTransactionTimeout(Period period)
public boolean isXA()
public void setXA(boolean isTransactional)
public void setXAForbidSameRM(boolean isXAForbidSameRM)
public void setSpy(boolean isSpy) throws java.io.IOException
java.io.IOException
public boolean isSpy()
public void setSerializationHandle(java.lang.Object handle)
setSerializationHandle
in interface HandleAware
@PostConstruct public void postConstruct() throws java.lang.Exception
java.lang.Exception
public void init() throws java.lang.Exception
java.lang.Exception
public java.sql.Connection getConnection() throws java.sql.SQLException
getConnection
in interface javax.sql.DataSource
java.sql.SQLException
public java.sql.Connection getConnection(java.lang.String user, java.lang.String password) throws java.sql.SQLException
getConnection
in interface javax.sql.DataSource
user
- database userpassword
- database passwordjava.sql.SQLException
public void closeIdleConnections()
public void markForPoolRemoval(ManagedConnectionImpl mConn)
public int getLoginTimeout() throws java.sql.SQLException
getLoginTimeout
in interface javax.sql.CommonDataSource
java.sql.SQLException
public void setLoginTimeout(int timeout) throws java.sql.SQLException
setLoginTimeout
in interface javax.sql.CommonDataSource
java.sql.SQLException
public java.io.PrintWriter getLogWriter() throws java.sql.SQLException
getLogWriter
in interface javax.sql.CommonDataSource
java.sql.SQLException
public void setLogWriter(java.io.PrintWriter log) throws java.sql.SQLException
setLogWriter
in interface javax.sql.CommonDataSource
java.sql.SQLException
public <T> T unwrap(java.lang.Class<T> iface) throws java.sql.SQLException
unwrap
in interface java.sql.Wrapper
java.sql.SQLException
public boolean isWrapperFor(java.lang.Class<?> iface) throws java.sql.SQLException
isWrapperFor
in interface java.sql.Wrapper
java.sql.SQLException
public java.util.logging.Logger getParentLogger()
getParentLogger
in interface javax.sql.CommonDataSource
public void close()
public java.lang.String toString()
toString
in class java.lang.Object