Package org.h2.jdbcx

Class JdbcConnectionPool

  • All Implemented Interfaces:
    java.sql.Wrapper, java.util.EventListener, javax.sql.CommonDataSource, javax.sql.ConnectionEventListener, javax.sql.DataSource, JdbcConnectionPoolBackwardsCompat

    public final class JdbcConnectionPool
    extends java.lang.Object
    implements javax.sql.DataSource, javax.sql.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();
         }
     }
     
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected JdbcConnectionPool​(javax.sql.ConnectionPoolDataSource dataSource)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private void closeConnection​(javax.sql.PooledConnection pc)  
      void connectionClosed​(javax.sql.ConnectionEvent event)
      INTERNAL
      void connectionErrorOccurred​(javax.sql.ConnectionEvent event)
      INTERNAL
      static JdbcConnectionPool create​(java.lang.String url, java.lang.String user, java.lang.String password)
      Constructs a new connection pool for H2 databases.
      static JdbcConnectionPool create​(javax.sql.ConnectionPoolDataSource dataSource)
      Constructs a new connection pool.
      void dispose()
      Closes all unused pooled connections.
      int getActiveConnections()
      Returns the number of active (open) connections of this pool.
      java.sql.Connection getConnection()
      Retrieves a connection from the connection pool.
      java.sql.Connection getConnection​(java.lang.String user, java.lang.String password)
      INTERNAL
      private java.sql.Connection getConnectionNow()  
      int getLoginTimeout()
      Gets the maximum time in seconds to wait for a free connection.
      java.io.PrintWriter getLogWriter()
      INTERNAL
      int getMaxConnections()
      Gets the maximum number of connections to use.
      java.util.logging.Logger getParentLogger()
      [Not supported]
      boolean isWrapperFor​(java.lang.Class<?> iface)
      Checks if unwrap can return an object of this class.
      private void recycleConnection​(javax.sql.PooledConnection pc)
      This method usually puts the connection back into the pool.
      void setLoginTimeout​(int seconds)
      Sets the maximum time in seconds to wait for a free connection.
      void setLogWriter​(java.io.PrintWriter logWriter)
      INTERNAL
      void setMaxConnections​(int max)
      Sets the maximum number of connections to use from now on.
      <T> T unwrap​(java.lang.Class<T> iface)
      Return an object of this class if possible.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface javax.sql.CommonDataSource

        createShardingKeyBuilder
      • Methods inherited from interface javax.sql.DataSource

        createConnectionBuilder
    • Field Detail

      • DEFAULT_MAX_CONNECTIONS

        private static final int DEFAULT_MAX_CONNECTIONS
        See Also:
        Constant Field Values
      • dataSource

        private final javax.sql.ConnectionPoolDataSource dataSource
      • recycledConnections

        private final java.util.Queue<javax.sql.PooledConnection> recycledConnections
      • logWriter

        private java.io.PrintWriter logWriter
      • maxConnections

        private volatile int maxConnections
      • timeout

        private volatile int timeout
      • activeConnections

        private java.util.concurrent.atomic.AtomicInteger activeConnections
      • isDisposed

        private java.util.concurrent.atomic.AtomicBoolean isDisposed
    • Constructor Detail

      • JdbcConnectionPool

        protected JdbcConnectionPool​(javax.sql.ConnectionPoolDataSource dataSource)
    • Method Detail

      • create

        public static JdbcConnectionPool create​(javax.sql.ConnectionPoolDataSource dataSource)
        Constructs a new connection pool.
        Parameters:
        dataSource - the data source to create connections
        Returns:
        the connection pool
      • create

        public static JdbcConnectionPool create​(java.lang.String url,
                                                java.lang.String user,
                                                java.lang.String password)
        Constructs a new connection pool for H2 databases.
        Parameters:
        url - the database URL of the H2 connection
        user - the user name
        password - 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:
        getLoginTimeout in interface javax.sql.CommonDataSource
        Specified by:
        getLoginTimeout in interface javax.sql.DataSource
        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:
        setLoginTimeout in interface javax.sql.CommonDataSource
        Specified by:
        setLoginTimeout in interface javax.sql.DataSource
        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

        public java.sql.Connection getConnection()
                                          throws java.sql.SQLException
        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.
        Specified by:
        getConnection in interface javax.sql.DataSource
        Returns:
        a new Connection object.
        Throws:
        java.sql.SQLException - when a new connection could not be established, or a timeout occurred
      • getConnection

        public java.sql.Connection getConnection​(java.lang.String user,
                                                 java.lang.String password)
        INTERNAL
        Specified by:
        getConnection in interface javax.sql.DataSource
      • getConnectionNow

        private java.sql.Connection getConnectionNow()
                                              throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • recycleConnection

        private void recycleConnection​(javax.sql.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 - the pooled connection
      • closeConnection

        private void closeConnection​(javax.sql.PooledConnection pc)
      • connectionClosed

        public void connectionClosed​(javax.sql.ConnectionEvent event)
        INTERNAL
        Specified by:
        connectionClosed in interface javax.sql.ConnectionEventListener
      • connectionErrorOccurred

        public void connectionErrorOccurred​(javax.sql.ConnectionEvent event)
        INTERNAL
        Specified by:
        connectionErrorOccurred in interface javax.sql.ConnectionEventListener
      • getActiveConnections

        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:
        the number of active connections.
      • getLogWriter

        public java.io.PrintWriter getLogWriter()
        INTERNAL
        Specified by:
        getLogWriter in interface javax.sql.CommonDataSource
        Specified by:
        getLogWriter in interface javax.sql.DataSource
      • setLogWriter

        public void setLogWriter​(java.io.PrintWriter logWriter)
        INTERNAL
        Specified by:
        setLogWriter in interface javax.sql.CommonDataSource
        Specified by:
        setLogWriter in interface javax.sql.DataSource
      • unwrap

        public <T> T unwrap​(java.lang.Class<T> iface)
                     throws java.sql.SQLException
        Return an object of this class if possible.
        Specified by:
        unwrap in interface java.sql.Wrapper
        Parameters:
        iface - the class
        Returns:
        this
        Throws:
        java.sql.SQLException
      • isWrapperFor

        public boolean isWrapperFor​(java.lang.Class<?> iface)
                             throws java.sql.SQLException
        Checks if unwrap can return an object of this class.
        Specified by:
        isWrapperFor in interface java.sql.Wrapper
        Parameters:
        iface - the class
        Returns:
        whether or not the interface is assignable from this class
        Throws:
        java.sql.SQLException
      • getParentLogger

        public java.util.logging.Logger getParentLogger()
        [Not supported]
        Specified by:
        getParentLogger in interface javax.sql.CommonDataSource