Spring 连接池休眠

发布于 2024-10-29 19:42:23 字数 60 浏览 1 评论 0原文

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

谢谢维努

How to configure connection pooling with Spring and Hibernate?

Thanks

Venu

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

百变从容 2024-11-05 19:42:23

您可以使用 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>

You can use DBCP component

<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>
难如初 2024-11-05 19:42:23

在 Hibernate 中,您可以在 Hibernate 中配置 CP30 连接池。在此处查看教程
IBM 有一个很好的教程,介绍如何将 Hibernate 与 Spring 集成

In Hibernate, you can configure CP30 Connection Pooling in Hibernate. View a tutorial here.
IBM has a good tutorial on how to integrate Hibernate with Spring.

凉城 2024-11-05 19:42:23

如果您想使用所有 Java 连接池提供程序中最好的,请尝试 HikariCP。
在 servlet-context 中使用 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>

然后使用此数据源创建 EntityManagerFactory bean。

If you want to use Best among all Java Connection Pool providers try HikariCP.
Configure a datasource bean using HikariCP in servlet-context as:

<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>

Then use this datasource to create EntityManagerFactory bean.

趁年轻赶紧闹 2024-11-05 19:42:23

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

否则,使用 Apache DBCP: http://commons.apache.org/dbcp/

If you're running in a web application container, use the built-in connection pooling of your container.

Otherwise, use Apache DBCP: http://commons.apache.org/dbcp/

韵柒 2024-11-05 19:42:23
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="<put database connection url here>" />
<property name="username" value="XXXXXX" />
<property name="password" value="XXXXXXXX" />
<property name="driverClassName" value="<database driver here>" />
</bean>

<bean id="pool" class="org.apache.commons.pool.impl.GenericObjectPool">
<property name="minEvictableIdleTimeMillis"><value>300000</value></property>
<property name="timeBetweenEvictionRunsMillis"><value>60000</value></property>
</bean>

<bean id="dsConnectionFactory" class="org.apache.commons.dbcp.DataSourceConnectionFactory">
<constructor-arg><ref bean="dataSource"/></constructor-arg>
</bean>

<bean id="poolableConnectionFactory"   class="org.apache.commons.dbcp.PoolableConnectionFactory">
<constructor-arg index="0"><ref bean="dsConnectionFactory"/></constructor-arg>
<constructor-arg index="1"><ref bean="pool"/></constructor-arg>
<constructor-arg index="2"><null/></constructor-arg>
<constructor-arg index="3"><null/></constructor-arg>
<constructor-arg index="4"><value>false</value></constructor-arg>
<constructor-arg index="5"><value>true</value></constructor-arg>
</bean>

<bean id="pooledDS" class="org.apache.commons.dbcp.PoolingDataSource" depends-on="poolableConnectionFactory">
<constructor-arg><ref bean="pool"/></constructor-arg>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="<put database connection url here>" />
<property name="username" value="XXXXXX" />
<property name="password" value="XXXXXXXX" />
<property name="driverClassName" value="<database driver here>" />
</bean>

<bean id="pool" class="org.apache.commons.pool.impl.GenericObjectPool">
<property name="minEvictableIdleTimeMillis"><value>300000</value></property>
<property name="timeBetweenEvictionRunsMillis"><value>60000</value></property>
</bean>

<bean id="dsConnectionFactory" class="org.apache.commons.dbcp.DataSourceConnectionFactory">
<constructor-arg><ref bean="dataSource"/></constructor-arg>
</bean>

<bean id="poolableConnectionFactory"   class="org.apache.commons.dbcp.PoolableConnectionFactory">
<constructor-arg index="0"><ref bean="dsConnectionFactory"/></constructor-arg>
<constructor-arg index="1"><ref bean="pool"/></constructor-arg>
<constructor-arg index="2"><null/></constructor-arg>
<constructor-arg index="3"><null/></constructor-arg>
<constructor-arg index="4"><value>false</value></constructor-arg>
<constructor-arg index="5"><value>true</value></constructor-arg>
</bean>

<bean id="pooledDS" class="org.apache.commons.dbcp.PoolingDataSource" depends-on="poolableConnectionFactory">
<constructor-arg><ref bean="pool"/></constructor-arg>
</bean>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文