public final class JdbcConnectionPool extends Object implements DataSource, ConnectionEventListener, JdbcConnectionPoolBackwardsCompat
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();
}
}
| 限定符和类型 | 方法和说明 |
|---|---|
void |
connectionClosed(ConnectionEvent event)
INTERNAL
|
void |
connectionErrorOccurred(ConnectionEvent event)
INTERNAL
|
static JdbcConnectionPool |
create(ConnectionPoolDataSource dataSource)
Constructs a new connection pool.
|
static JdbcConnectionPool |
create(String url,
String user,
String password)
Constructs a new connection pool for H2 databases.
|
void |
dispose()
Closes all unused pooled connections.
|
int |
getActiveConnections()
Returns the number of active (open) connections of this pool.
|
Connection |
getConnection()
Retrieves a connection from the connection pool.
|
Connection |
getConnection(String user,
String password)
INTERNAL
|
int |
getLoginTimeout()
Gets the maximum time in seconds to wait for a free connection.
|
PrintWriter |
getLogWriter()
INTERNAL
|
int |
getMaxConnections()
Gets the maximum number of connections to use.
|
Logger |
getParentLogger()
[Not supported]
|
boolean |
isWrapperFor(Class<?> iface)
Checks if unwrap can return an object of this class.
|
void |
setLoginTimeout(int seconds)
Sets the maximum time in seconds to wait for a free connection.
|
void |
setLogWriter(PrintWriter logWriter)
INTERNAL
|
void |
setMaxConnections(int max)
Sets the maximum number of connections to use from now on.
|
<T> T |
unwrap(Class<T> iface)
Return an object of this class if possible.
|
public static JdbcConnectionPool create(ConnectionPoolDataSource dataSource)
dataSource - the data source to create connectionspublic static JdbcConnectionPool create(String url, String user, String password)
url - the database URL of the H2 connectionuser - the user namepassword - the passwordpublic void setMaxConnections(int max)
max - the maximum number of connectionspublic int getMaxConnections()
public int getLoginTimeout()
getLoginTimeout 在接口中 CommonDataSourcepublic void setLoginTimeout(int seconds)
setLoginTimeout 在接口中 CommonDataSourceseconds - the timeout, 0 meaning the defaultpublic void dispose()
public Connection getConnection() throws SQLException
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.getConnection 在接口中 DataSourceSQLException - when a new connection could not be established,
or a timeout occurredpublic Connection getConnection(String user, String password)
getConnection 在接口中 DataSourcepublic void connectionClosed(ConnectionEvent event)
connectionClosed 在接口中 ConnectionEventListenerpublic void connectionErrorOccurred(ConnectionEvent event)
public int getActiveConnections()
Connection objects that have been issued by
getConnection() for which Connection.close() has
not yet been called.public PrintWriter getLogWriter()
getLogWriter 在接口中 CommonDataSourcepublic void setLogWriter(PrintWriter logWriter)
setLogWriter 在接口中 CommonDataSourcepublic <T> T unwrap(Class<T> iface) throws SQLException
unwrap 在接口中 Wrapperiface - the classSQLExceptionpublic boolean isWrapperFor(Class<?> iface) throws SQLException
isWrapperFor 在接口中 Wrapperiface - the classSQLExceptionpublic Logger getParentLogger()
getParentLogger 在接口中 CommonDataSourceCopyright © 2022. All rights reserved.