java 如何为连接池配置 Hibernate、Spring 和 Apache dbcp?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27102265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:13:16  来源:igfitidea点击:

How to configure Hibernate, Spring and Apache dbcp for connection pooling?

javamysqlspringhibernatespring-mvc

提问by phoenix

I have a problem integrating Spring, Hibernate, and Apache DBCP. I have used the DBCPConnectionProviderfrom here.

我在集成 Spring、Hibernate 和 Apache DBCP 时遇到了问题。我已经使用了DBCPConnectionProviderfrom here

I have the following xml configuration for the above said.

我有上面所说的以下xml配置。

<context:component-scan base-package="com.my.app"/>

<tx:annotation-driven/>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="packagesToScan">
        <list>
            <value>com.my.app.model</value>
        </list>
    </property>

    <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"/>

    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>

            <prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>
            <prop key="hibernate.connection.url">jdbc:mysql://localhost:3306/app</prop>
            <prop key="hibernate.connection.username">foo</prop>
            <prop key="hibernate.connection.password">bar</prop>
            <prop key="hibernate.connection.provider_class">org.hibernate.connection.DBCPConnectionProvider</prop>

            <!-- Connection pooling related properties -->

            <prop key="hibernate.dbcp.initialSize">8</prop>
            <prop key="hibernate.dbcp.maxActive">20</prop>
            <prop key="hibernate.dbcp.maxIdle">5</prop>
            <prop key="hibernate.dbcp.minIdle">0</prop>
            <prop key="hibernate.dbcp.maxWait">10000</prop>
            <prop key="hibernate.dbcp.minEvictableIdleTimeMillis">180000</prop>
            <prop key="hibernate.dbcp.timeBetweenEvictionRunsMillis">180000</prop>
            <prop key="hibernate.dbcp.testWhileIdle">true</prop>
            <prop key="hibernate.dbcp.testOnReturn">true</prop>
            <prop key="hibernate.dbcp.validationQuery">select 1</prop>
        </props>
    </property>
</bean>

<bean id="transactionManager"
      class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

When the database schema i.e. appis empty two tables will be created in mysql. But I am getting NullPointerExceptionin the getConnection() method of the DBCPConnectionProviderthat I mentioned. That means the dataSource is not instantiated. I think I have configured everything in the xml. What am I missing. How do you configure DBCP version 1.4 with Spring and Hibernate? Please provide your insights.

当数据库模式 ieapp为空时,将在 mysql 中创建两个表。但是我正在使用我提到NullPointerException的 getConnection() 方法DBCPConnectionProvider。这意味着数据源没有被实例化。我想我已经在 xml 中配置了所有内容。我错过了什么。您如何使用 Spring 和 Hibernate 配置 DBCP 1.4 版?请提供您的见解。

Here is the stack trace:

这是堆栈跟踪:

Caused by: java.lang.NullPointerException
  at org.hibernate.connection.DBCPConnectionProvider.getConnection(DBCPConnectionProvider.java:209)
  at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:417)
  at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:144)
  at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:119)
  at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:57)
  at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1326)
  at org.springframework.orm.hibernate3.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:555)
  ... 82 more

回答by M. Deinum

Don't do it like that. Configure the datasource you want to use in Spring as well as Hibernate. Ditch the hibernate.dbcpand hibernate.connectionproperties.

不要那样做。配置要在 Spring 和 Hibernate 中使用的数据源。放弃hibernate.dbcphibernate.connection属性。

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/app"/>
    <property name="username" value="foo"/>
    <property name="password" value="bar"/>
    // Other DBCP properties here
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource"  ref="dataSource"   
    <property name="packagesToScan">
        <list>
            <value>com.my.app.model</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
</bean>

Just add the dataSourceproperty to your AnnotationSessionFactoryBeanas dependency and done. Note you don't need the configurationClassproperty as it is already annotation based.

只需将该dataSource属性添加到您的AnnotationSessionFactoryBean依赖项中即可。请注意,您不需要该configurationClass属性,因为它已经是基于注释的。

A tip I wouldn't suggest using Commons-DBCP anymore as a datasource instead take a look at HikariCPas a better datasource implementation.

一个提示 我不建议再使用 Commons-DBCP 作为数据源,而是将HikariCP视为更好的数据源实现。

For more information in integrating/configuring Hibernate with Spring I suggest this sectionof the Reference Guide.

有关将 Hibernate 与 Spring 集成/配置的更多信息,我建议参考参考指南的这一部分