Top Description Fields Constructors Methods
org.h2.jdbcx

public final Class JdbcConnectionPool

extends Object
implements DataSource, ConnectionEventListener, JdbcConnectionPoolBackwardsCompat
Class Inheritance
All Implemented Interfaces
org.h2.jdbcx.JdbcConnectionPoolBackwardsCompat, javax.sql.ConnectionEventListener, java.util.EventListener, javax.sql.DataSource, java.sql.Wrapper, javax.sql.CommonDataSource
Imports
java.io.PrintWriter, java.sql.Connection, .SQLException, java.util.Queue, java.util.concurrent.ConcurrentLinkedQueue, java.util.concurrent.atomic.AtomicBoolean, .AtomicInteger, java.util.logging.Logger, javax.sql.ConnectionEvent, .ConnectionEventListener, .ConnectionPoolDataSource, .DataSource, .PooledConnection, org.h2.message.DbException

A simple standalone JDBC connection pool. It is based on the MiniConnectionPoolManager written by Christian d'Heureuse (Java 1.5) . It is used as follows:
import java.sql.*;
import org.h2.jdbcx.JdbcConnectionPool;
public class Test {
    public static void main(String... args) throws Exception {
        JdbcConnectionPool cp = JdbcConnectionPool.create(
            "jdbc:h2:~/test", "sa", "sa");
        for (String sql : args) {
            Connection conn = cp.getConnection();
            conn.createStatement().execute(sql);
            conn.close();
        }
        cp.dispose();
    }
}
Authors
Christian d'Heureuse (www.source-code.biz), Thomas Mueller

Field Summary

Modifier and TypeField and Description
private AtomicInteger
private final ConnectionPoolDataSource
private static final int
private static final int
private AtomicBoolean
private PrintWriter
private volatile int
private final Queue<PooledConnection>
private volatile int

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private void
public void
connectionClosed(ConnectionEvent
an event object describing the source of the event
event
)

Implements javax.sql.ConnectionEventListener.connectionClosed.

INTERNAL

public void
connectionErrorOccurred(ConnectionEvent
an event object describing the source of the event and containing the SQLException that the driver is about to throw
event
)

Implements javax.sql.ConnectionEventListener.connectionErrorOccurred.

INTERNAL

public static JdbcConnectionPool

Returns:

the connection pool
create
(ConnectionPoolDataSource
the data source to create connections
dataSource
)

Constructs a new connection pool.

public static JdbcConnectionPool

Returns:

the connection pool
create
(String
the database URL of the H2 connection
url
,
String
the user name
user
,
String
the password
password
)

Constructs a new connection pool for H2 databases.

public void
dispose()

Closes all unused pooled connections.

public int

Returns:

the number of active connections.
getActiveConnections
()

Returns the number of active (open) connections of this pool.

public Connection

Returns:

a new Connection object.
getConnection
()

Implements javax.sql.DataSource.getConnection.

Retrieves a connection from the connection pool.

public Connection
getConnection(String
the database user on whose behalf the connection is being made
user
,
String
the user's password
password
)

Implements javax.sql.DataSource.getConnection.

INTERNAL

private Connection
public int

Returns:

the timeout in seconds
getLoginTimeout
()

Implements javax.sql.DataSource.getLoginTimeout.

Gets the maximum time in seconds to wait for a free connection.

public PrintWriter
public int

Returns:

the max the maximum number of connections
getMaxConnections
()

Gets the maximum number of connections to use.

public java.util.logging.Logger
public boolean

Returns:

whether or not the interface is assignable from this class
isWrapperFor
(Class<?>
the class
iface
)

Implements java.sql.Wrapper.isWrapperFor.

Checks if unwrap can return an object of this class.

private void
recycleConnection(PooledConnection
the pooled connection
pc
)

This method usually puts the connection back into the pool.

public void
setLoginTimeout(int
the timeout, 0 meaning the default
seconds
)

Implements javax.sql.DataSource.setLoginTimeout.

Sets the maximum time in seconds to wait for a free connection.

public void
setLogWriter(PrintWriter
the new log writer; to disable logging, set to null
logWriter
)

Implements javax.sql.DataSource.setLogWriter.

INTERNAL

public void
setMaxConnections(int
the maximum number of connections
max
)

Sets the maximum number of connections to use from now on.

public <T> T

Returns:

this
unwrap
(Class<T>
the class
iface
)

Implements java.sql.Wrapper.unwrap.

Return an object of this class if possible.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

activeConnectionsback to summary
private AtomicInteger activeConnections
dataSourceback to summary
private final ConnectionPoolDataSource dataSource
DEFAULT_MAX_CONNECTIONSback to summary
private static final int DEFAULT_MAX_CONNECTIONS
DEFAULT_TIMEOUTback to summary
private static final int DEFAULT_TIMEOUT
isDisposedback to summary
private AtomicBoolean isDisposed
logWriterback to summary
private PrintWriter logWriter
maxConnectionsback to summary
private volatile int maxConnections
recycledConnectionsback to summary
private final Queue<PooledConnection> recycledConnections
timeoutback to summary
private volatile int timeout

Constructor Detail

JdbcConnectionPoolback to summary
private JdbcConnectionPool(ConnectionPoolDataSource dataSource)

Method Detail

closeConnectionback to summary
private void closeConnection(PooledConnection pc)
connectionClosedback to summary
public void connectionClosed(ConnectionEvent event)

Implements javax.sql.ConnectionEventListener.connectionClosed.

INTERNAL

Parameters
event:ConnectionEvent

Doc from javax.sql.ConnectionEventListener.connectionClosed.

an event object describing the source of the event

Annotations
@Override
connectionErrorOccurredback to summary
public void connectionErrorOccurred(ConnectionEvent event)

Implements javax.sql.ConnectionEventListener.connectionErrorOccurred.

INTERNAL

Parameters
event:ConnectionEvent

Doc from javax.sql.ConnectionEventListener.connectionErrorOccurred.

an event object describing the source of the event and containing the SQLException that the driver is about to throw

Annotations
@Override
createback to summary
public static JdbcConnectionPool create(ConnectionPoolDataSource dataSource)

Constructs a new connection pool.

Parameters
dataSource:ConnectionPoolDataSource

the data source to create connections

Returns:JdbcConnectionPool

the connection pool

createback to summary
public static JdbcConnectionPool create(String url, String user, String password)

Constructs a new connection pool for H2 databases.

Parameters
url:String

the database URL of the H2 connection

user:String

the user name

password:String

the password

Returns:JdbcConnectionPool

the connection pool

disposeback to summary
public void dispose()

Closes all unused pooled connections. Exceptions while closing are written to the log stream (if set).

getActiveConnectionsback to summary
public int getActiveConnections()

Returns the number of active (open) connections of this pool. This is the number of Connection objects that have been issued by getConnection() for which Connection.close() has not yet been called.

Returns:int

the number of active connections.

getConnectionback to summary
public Connection getConnection() throws SQLException

Implements javax.sql.DataSource.getConnection.

Retrieves a connection from the connection pool. If maxConnections connections are already in use, the method waits until a connection becomes available or timeout seconds elapsed. When the application is finished using the connection, it must close it in order to return it to the pool. If no connection becomes available within the given timeout, an exception with SQL state 08001 and vendor code 8001 is thrown.

Returns:Connection

a new Connection object.

Annotations
@Override
Exceptions
SQLException:
when a new connection could not be established, or a timeout occurred
getConnectionback to summary
public Connection getConnection(String user, String password)

Implements javax.sql.DataSource.getConnection.

INTERNAL

Parameters
user:String

Doc from javax.sql.DataSource.getConnection.

the database user on whose behalf the connection is being made

password:String

Doc from javax.sql.DataSource.getConnection.

the user's password

Returns:Connection

Doc from javax.sql.DataSource.getConnection.

a connection to the data source

Annotations
@Override
getConnectionNowback to summary
private Connection getConnectionNow() throws SQLException
getLoginTimeoutback to summary
public int getLoginTimeout()

Implements javax.sql.DataSource.getLoginTimeout.

Gets the maximum time in seconds to wait for a free connection.

Returns:int

the timeout in seconds

Annotations
@Override
getLogWriterback to summary
public PrintWriter getLogWriter()

Implements javax.sql.DataSource.getLogWriter.

INTERNAL

Returns:PrintWriter

Doc from javax.sql.CommonDataSource.getLogWriter.

the log writer for this data source or null if logging is disabled

Annotations
@Override
getMaxConnectionsback to summary
public int getMaxConnections()

Gets the maximum number of connections to use.

Returns:int

the max the maximum number of connections

getParentLoggerback to summary
public java.util.logging.Logger getParentLogger()

Implements javax.sql.CommonDataSource.getParentLogger.

[Not supported]

Returns:java.util.logging.Logger

Doc from javax.sql.CommonDataSource.getParentLogger.

the parent Logger for this data source

Annotations
@Override
isWrapperForback to summary
public boolean isWrapperFor(Class<?> iface) throws SQLException

Implements java.sql.Wrapper.isWrapperFor.

Checks if unwrap can return an object of this class.

Parameters
iface:Class<?>

the class

Returns:boolean

whether or not the interface is assignable from this class

Annotations
@Override
Exceptions
SQLException:

Doc from java.sql.Wrapper.isWrapperFor.

if an error occurs while determining whether this is a wrapper for an object with the given interface.

recycleConnectionback to summary
private void recycleConnection(PooledConnection pc)

This method usually puts the connection back into the pool. There are some exceptions: if the pool is disposed, the connection is disposed as well. If the pool is full, the connection is closed.

Parameters
pc:PooledConnection

the pooled connection

setLoginTimeoutback to summary
public void setLoginTimeout(int seconds)

Implements javax.sql.DataSource.setLoginTimeout.

Sets the maximum time in seconds to wait for a free connection. The default timeout is 30 seconds. Calling this method with the value 0 will set the timeout to the default value.

Parameters
seconds:int

the timeout, 0 meaning the default

Annotations
@Override
setLogWriterback to summary
public void setLogWriter(PrintWriter logWriter)

Implements javax.sql.DataSource.setLogWriter.

INTERNAL

Parameters
logWriter:PrintWriter

Doc from javax.sql.CommonDataSource.setLogWriter.

the new log writer; to disable logging, set to null

Annotations
@Override
setMaxConnectionsback to summary
public void setMaxConnections(int max)

Sets the maximum number of connections to use from now on. The default value is 10 connections.

Parameters
max:int

the maximum number of connections

unwrapback to summary
public <T> T unwrap(Class<T> iface) throws SQLException

Implements java.sql.Wrapper.unwrap.

Return an object of this class if possible.

Parameters
<T>

Doc from java.sql.Wrapper.unwrap. the type of the class modeled by this Class object

iface:Class<T>

the class

Returns:T

this

Annotations
@Override
@SuppressWarnings:unchecked
Exceptions
SQLException:

Doc from java.sql.Wrapper.unwrap.

If no object found that implements the interface