java 为什么在 Spring-Hibernate Configuration 中同时配置 dataSource 和 sessionFactory?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13653591/
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-10-31 13:30:41  来源:igfitidea点击:

Why configure both dataSource and sessionFactory in Spring-Hibernate Configuration?

javaspringhibernate

提问by Sotirios Delimanolis

I'm using Spring 3.1.2 and Hibernate 4.1.7 for my web application. I want to now configure both of these. I have my hibernate.cfg.xmlfile:

我的 Web 应用程序使用 Spring 3.1.2 和 Hibernate 4.1.7。我现在想配置这两个。我有我的hibernate.cfg.xml文件:

<hibernate-configuration>
    <session-factory>
        <property name="connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.pool_size">10</property>
        <property name="hibernate.connection.autocommit">false</property>
        <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
        <!-- 
        <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
         -->
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.hbm2ddl.auto">update</property>       
    </session-factory>
</hibernate-configuration>

My webapp-servlet.xmlspring config file:

我的webapp-servlet.xml弹簧配置文件:

<beans>
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="configLocation">
        <value>
            classpath:hibernate.cfg.xml
        </value>
    </property>
    <property name = "dataSource" ref = "dataSource"></property>
</bean>

<bean id = "dataSource" class = "org.apache.commons.dbcp.BasicDataSource">
    <property name = "driverClassName" value = "com.mysql.jdbc.Driver" />
    <property name = "url" value = "jdbc:mysql://localhost:3306/test" />
    <property name = "username" value = "root" />
    <property name = "password" value = "root" />
    <property name = "maxActive" value = "10" />
</bean>
</beans>
  1. Why do I need to configure a DataSource bean when all of the data needed is already included in the hibernate configuration file? Does Hibernate have some default it can use?
  2. What are some other DataSources I can use?
  3. Am I missing any other beans or configuration parameters/properties to get hibernate working with my application?
  1. 当所有需要的数据都已经包含在休眠配置文件中时,为什么我需要配置一个 DataSource bean?Hibernate 是否有一些可以使用的默认值?
  2. DataSource我可以使用哪些其他s?
  3. 我是否缺少任何其他 bean 或配置参数/属性来使我的应用程序休眠?

回答by axtavt

  1. You don't need both of them. You can either get rid of hibernate.cfg.xmland configure everything in LocalSessionFactoryBean, or reuse your existing hibernate.cfg.xmlas is (in this case you don't need to configure DataSourcein Spring config).

  2. You have the following options:

    • Use embedded database- it's good for testing and learning purposes

    • Use DriverManagerDataSource- it's a simple non-pooled datasource that can be used for testing, etc (not recommended for production use)

    • Use connection pool such as DBCP or c3p0

    • If you deploy to application server you can use connection pool provided by the application server using JNDI

  3. Your current configuration is sufficient, but it lacks support of Spring transaction management. In order to enable it you need to

    • Declare HibernateTransactionManager

    • Add <tx:annotation-driven>to enable declarative transaction management (@Transactional)

    • Declare TransactionTemplateif you want to use programmatic transaction management (use it to overcome limitations of declarative transaction management)

    • Also don't forget to remove transaction-related properties from Hibernate configuration since they may conflict with Spring transaction management

  1. 你不需要他们两个。您可以删除hibernate.cfg.xml并配置 中的所有内容LocalSessionFactoryBean,也可以hibernate.cfg.xml按原样重新使用现有的(在这种情况下,您不需要DataSource在 Spring 配置中进行配置)。

  2. 您有以下选择:

    • 使用嵌入式数据库- 有利于测试和学习目的

    • 使用DriverManagerDataSource- 它是一个简单的非池化数据源,可用于测试等(不推荐用于生产用途)

    • 使用 DBCP 或 c3p0 等连接池

    • 如果部署到应用程序服务器,则可以使用应用程序服务器提供的JNDI连接池

  3. 您当前的配置已经足够了,但是它缺乏对Spring 事务管理的支持。为了启用它,您需要

    • 宣布 HibernateTransactionManager

    • 添加<tx:annotation-driven>以启用声明式事务管理 ( @Transactional)

    • 声明TransactionTemplate是否要使用程序化事务管理(用它来克服声明式事务管理的局限性)

    • 另外不要忘记从 Hibernate 配置中删除与事务相关的属性,因为它们可能与 Spring 事务管理发生冲突