JDBC連接池:Vibur DBCP

jopen 11年前發布 | 21K 次閱讀 數據庫連接池 Vibur DBCP

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.baiduhome.net/lib/view/home/1384754964649

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!