JDBC连接池:Vibur DBCP

jopen 11年前

Vibur DBCP是一个新的,并发JDBC连接池基于Java动态代理。它具有快速、简洁的源代码、支持Fairness参数,语句缓存,SQL查询日志等许多其它特性。还提供各种配置示例(与 Spring, Hibernate等集成)。是什么使得它不同于大多数竞争对手是其简洁,易于维护的源代码,其模块化设计,其中包括一个独立的专用对象池。

最重要的特征和特性:

  • Built using standard Java concurrency utilities and dynamic proxies, does not use any synchronized blocks/methods during normal pool operations.
  • Supports fairness parameter, which when set to true, guarantees that the threads invoking the pool's take methods will be selected to obtain a connection from it in FIFO order, and no thread will be starved out from accessing the pool's underlying resources.
  • The only external dependencies for Vibur DBCP are its object pool, slf4j/log4j, and ConcurrentLinkedHashMap. The CLHM dependency can be excluded if the JDBC Statement caching is not needed.
  • SQL queries logging and getConnection() calls logging if their execution time is longer than a given limit.
  • Caching support for JDBC Statements (Prepared and Callable).
  • Hibernate 3.x integration support.

其他特点:

  • Validation intervals support, i.e. the taken from the pool connection is not validated before every use but is validated only if a given time has passed since the connection's last use.
  • Intelligent pool sizing - the number of idle connections in the pool will be reduced based on heuristics for the number of connections recently used.
  • The underlying JDBC connection or Statement object can be retrieved from the respective proxy object via calling the proxy's unwrap method.
  • Ability to provide records for all JDBC connections which are currently taken, including the stack traces with which they were taken. Useful if debugging lost (unclosed) connections.
  • JMX support - the pool registers an MBean via which various pool parameters can be observed and/or set.

Hibernate 3.x 配置示例:
<hibernate-configuration>      <session-factory>          <!-- Database connection settings: -->          <property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property>          <property name="hibernate.connection.url">jdbc:hsqldb:mem:sakila;shutdown=false</property>          <property name="hibernate.connection.username">sa</property>          <property name="hibernate.connection.password"></property>            <property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect</property>            <property name="hibernate.current_session_context_class">thread</property>            <!-- Vibur DBCP specific properties: -->          <property name="hibernate.connection.provider_class">              org.vibur.dbcp.integration.ViburDBCPConnectionProvider          </property>            <property name="hibernate.vibur.poolInitialSize">10</property>          <property name="hibernate.vibur.poolMaxSize">100</property>            <property name="hibernate.vibur.connectionIdleLimitInSeconds">30</property>          <property name="hibernate.vibur.testConnectionQuery">SELECT 1</property>            <property name="hibernate.vibur.logQueryExecutionLongerThanMs">500</property>          <property name="hibernate.vibur.logStackTraceForLongQueryExecution">true</property>            <property name="hibernate.vibur.statementCacheMaxSize">200</property>      </session-factory>  </hibernate-configuration>

Spring (with Hibernate 3.x) 配置示例:

   <!-- Vibur DBCP bean definition: -->     <bean id="dataSource" class="org.vibur.dbcp.ViburDBCPDataSource" init-method="start" destroy-method="terminate">          <property name="driverClassName" value="org.hsqldb.jdbcDriver"/>          <property name="jdbcUrl" value="jdbc:hsqldb:mem:sakila;shutdown=false"/>          <property name="username" value="sa"/>          <property name="password" value=""/>                                    <property name="poolInitialSize">10</property>          <property name="poolMaxSize">100</property>            <property name="connectionIdleLimitInSeconds">30</property>          <property name="testConnectionQuery">SELECT 1</property>            <property name="logQueryExecutionLongerThanMs" value="500"/>          <property name="logStackTraceForLongQueryExecution" value="true"/>                            <property name="statementCacheMaxSize" value="200"/>      </bean>        <!-- Spring session factory (Hibernate 3) and transaction manager beans definitions: -->        <bean id="baseSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"               abstract="true" >          <property name="dataSource" ref="dataSource"/>          <property name="packagesToScan" value="the.project.packages"/>      </bean>        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"               parent="baseSessionFactory">          <property name="hibernateProperties">              <props>                  <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>                  <prop key="hibernate.cache.use_second_level_cache">false</prop>                  <prop key="hibernate.cache.use_query_cache">true</prop>              </props>          </property>      </bean>        <tx:annotation-driven transaction-manager="transactionManager"/>      <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">          <property name="sessionFactory" ref="sessionFactory"/>      </bean>

编程配置示例:
    public ViburDBCPDataSource createDataSourceWithStatementsCache() {          ViburDBCPDataSource ds = new ViburDBCPDataSource();            ds.setDriverClassName("org.hsqldb.jdbcDriver");          ds.setJdbcUrl("jdbc:hsqldb:mem:sakila;shutdown=false");          ds.setUsername("sa");          ds.setPassword("");            ds.setPoolInitialSize(10);          ds.setPoolMaxSize(100);            ds.setConnectionIdleLimitInSeconds(30);          ds.setTestConnectionQuery("SELECT 1");            ds.setLogQueryExecutionLongerThanMs(500);          ds.setLogStackTraceForLongQueryExecution(true);            ds.setStatementCacheMaxSize(200);            ds.start();            return ds;      }

项目主页:http://www.open-open.com/lib/view/home/1384754964649