Java 连接池与Spring&;冬眠

Java 连接池与Spring&;冬眠,java,hibernate,spring,connection-pooling,Java,Hibernate,Spring,Connection Pooling,如何使用Spring和Hibernate配置连接池 谢谢 Venu如果您在web应用程序容器中运行,请使用容器的内置连接池 否则,在Hibernate中使用ApacheDBCP:,您可以在Hibernate中配置CP30连接池。查看教程。 IBM有一个很好的关于如何使用DBCP组件的教程。您可以使用DBCP组件 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-meth

如何使用Spring和Hibernate配置连接池

谢谢


Venu

如果您在web应用程序容器中运行,请使用容器的内置连接池


否则,在Hibernate中使用ApacheDBCP:

,您可以在Hibernate中配置CP30连接池。查看教程。
IBM有一个很好的关于如何使用DBCP组件的教程。

您可以使用DBCP组件

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">

        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="initialSize" value="10" />
        <property name="maxActive" value="5" />
        <property name="maxWait" value="5000" />
    </bean>


    <!-- Hibernate Configuration -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
        p:dataSource-ref="dataSource">
        <property name="annotatedClasses">
            <list>
                <value>com.project.domain.Domain1</value>
                <value>com.project.domain.Domain1</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    ${hibernate.dialect}
                </prop>
                <prop key="hibernate.show_sql">
                    ${hibernate.show_sql}
                </prop>
                <prop key="hibernate.generate_statistics">
                    ${hibernate.show_statistics}
                </prop>
            </props>
        </property>
    </bean>

com.project.domain.Domain1
com.project.domain.Domain1
${hibernate.dial}
${hibernate.show_sql}
${hibernate.show_statistics}

300000
60000
假的
真的

如果您想使用所有Java连接池提供程序中最好的一个,请尝试HikariCP。 在servlet上下文中使用HikariCP将数据源bean配置为:

<beans:bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource"  destroy-method="close">
                <beans:property name="dataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlDataSource"/>
                <beans:property name="maximumPoolSize" value="5" />
                <beans:property name="maxLifetime" value="30000" />
                <beans:property name="idleTimeout" value="30000" />
                <beans:property name="dataSourceProperties">
                          <beans:props>
                              <beans:prop key="url">jdbc:mysql://localhost:3306/exampledb</beans:prop>
                              <beans:prop key="user">root</beans:prop>
                              <beans:prop key="password"></beans:prop>
                               <beans:prop key="prepStmtCacheSize">250</beans:prop>
                               <beans:prop key="prepStmtCacheSqlLimit">2048</beans:prop>
                               <beans:prop key="cachePrepStmts">true</beans:prop>
                               <beans:prop key="useServerPrepStmts">true</beans:prop>
                          </beans:props>
                </beans:property>
</beans:bean>

jdbc:mysql://localhost:3306/exampledb
根
250
2048
真的
真的

然后使用此数据源创建EntityManagerFactorybean

目前DBCP的实际版本是
org.apache.commons.dbcp2.BasicDataSource
2012年我回答了这个问题。。。然而,2018年。。HikariCP是更好的连接池库。
<beans:bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource"  destroy-method="close">
                <beans:property name="dataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlDataSource"/>
                <beans:property name="maximumPoolSize" value="5" />
                <beans:property name="maxLifetime" value="30000" />
                <beans:property name="idleTimeout" value="30000" />
                <beans:property name="dataSourceProperties">
                          <beans:props>
                              <beans:prop key="url">jdbc:mysql://localhost:3306/exampledb</beans:prop>
                              <beans:prop key="user">root</beans:prop>
                              <beans:prop key="password"></beans:prop>
                               <beans:prop key="prepStmtCacheSize">250</beans:prop>
                               <beans:prop key="prepStmtCacheSqlLimit">2048</beans:prop>
                               <beans:prop key="cachePrepStmts">true</beans:prop>
                               <beans:prop key="useServerPrepStmts">true</beans:prop>
                          </beans:props>
                </beans:property>
</beans:bean>