Class JdbcConnectionPool
java.lang.Object
org.h2.jdbcx.JdbcConnectionPool
- All Implemented Interfaces:
Wrapper, EventListener, CommonDataSource, ConnectionEventListener, DataSource, JdbcConnectionPoolBackwardsCompat
public final class JdbcConnectionPool
extends Object
implements DataSource, ConnectionEventListener, JdbcConnectionPoolBackwardsCompat
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();
}
}
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate AtomicIntegerprivate final ConnectionPoolDataSourceprivate static final intprivate static final intprivate AtomicBooleanprivate PrintWriterprivate intprivate final Queue<PooledConnection> private int -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate voidvoidconnectionClosed(ConnectionEvent event) INTERNALvoidINTERNALstatic JdbcConnectionPoolConstructs a new connection pool for H2 databases.static JdbcConnectionPoolcreate(ConnectionPoolDataSource dataSource) Constructs a new connection pool.voiddispose()Closes all unused pooled connections.intReturns the number of active (open) connections of this pool.Retrieves a connection from the connection pool.getConnection(String user, String password) INTERNALprivate ConnectionintGets the maximum time in seconds to wait for a free connection.INTERNALintGets the maximum number of connections to use.[Not supported]booleanisWrapperFor(Class<?> iface) Checks if unwrap can return an object of this class.private voidThis method usually puts the connection back into the pool.voidsetLoginTimeout(int seconds) Sets the maximum time in seconds to wait for a free connection.voidsetLogWriter(PrintWriter logWriter) INTERNALvoidsetMaxConnections(int max) Sets the maximum number of connections to use from now on.<T> TReturn an object of this class if possible.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface CommonDataSource
createShardingKeyBuilderMethods inherited from interface DataSource
createConnectionBuilder
-
Field Details
-
DEFAULT_TIMEOUT
private static final int DEFAULT_TIMEOUT- See Also:
-
DEFAULT_MAX_CONNECTIONS
private static final int DEFAULT_MAX_CONNECTIONS- See Also:
-
dataSource
-
recycledConnections
-
logWriter
-
maxConnections
private volatile int maxConnections -
timeout
private volatile int timeout -
activeConnections
-
isDisposed
-
-
Constructor Details
-
JdbcConnectionPool
-
-
Method Details
-
create
Constructs a new connection pool.- Parameters:
dataSource- the data source to create connections- Returns:
- the connection pool
-
create
Constructs a new connection pool for H2 databases.- Parameters:
url- the database URL of the H2 connectionuser- the user namepassword- the password- Returns:
- the connection pool
-
setMaxConnections
public void setMaxConnections(int max) Sets the maximum number of connections to use from now on. The default value is 10 connections.- Parameters:
max- the maximum number of connections
-
getMaxConnections
public int getMaxConnections()Gets the maximum number of connections to use.- Returns:
- the max the maximum number of connections
-
getLoginTimeout
public int getLoginTimeout()Gets the maximum time in seconds to wait for a free connection.- Specified by:
getLoginTimeoutin interfaceCommonDataSource- Specified by:
getLoginTimeoutin interfaceDataSource- Returns:
- the timeout in seconds
-
setLoginTimeout
public void setLoginTimeout(int seconds) 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.- Specified by:
setLoginTimeoutin interfaceCommonDataSource- Specified by:
setLoginTimeoutin interfaceDataSource- Parameters:
seconds- the timeout, 0 meaning the default
-
dispose
public void dispose()Closes all unused pooled connections. Exceptions while closing are written to the log stream (if set). -
getConnection
Retrieves a connection from the connection pool. IfmaxConnectionsconnections are already in use, the method waits until a connection becomes available ortimeoutseconds 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.- Specified by:
getConnectionin interfaceDataSource- Returns:
- a new Connection object.
- Throws:
SQLException- when a new connection could not be established, or a timeout occurred
-
getConnection
INTERNAL- Specified by:
getConnectionin interfaceDataSource
-
getConnectionNow
- Throws:
SQLException
-
recycleConnection
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- the pooled connection
-
closeConnection
-
connectionClosed
INTERNAL- Specified by:
connectionClosedin interfaceConnectionEventListener
-
connectionErrorOccurred
INTERNAL- Specified by:
connectionErrorOccurredin interfaceConnectionEventListener
-
getActiveConnections
public int getActiveConnections()Returns the number of active (open) connections of this pool. This is the number ofConnectionobjects that have been issued by getConnection() for whichConnection.close()has not yet been called.- Returns:
- the number of active connections.
-
getLogWriter
INTERNAL- Specified by:
getLogWriterin interfaceCommonDataSource- Specified by:
getLogWriterin interfaceDataSource
-
setLogWriter
INTERNAL- Specified by:
setLogWriterin interfaceCommonDataSource- Specified by:
setLogWriterin interfaceDataSource
-
unwrap
Return an object of this class if possible.- Specified by:
unwrapin interfaceWrapper- Parameters:
iface- the class- Returns:
- this
- Throws:
SQLException
-
isWrapperFor
Checks if unwrap can return an object of this class.- Specified by:
isWrapperForin interfaceWrapper- Parameters:
iface- the class- Returns:
- whether or not the interface is assignable from this class
- Throws:
SQLException
-
getParentLogger
[Not supported]- Specified by:
getParentLoggerin interfaceCommonDataSource
-