【问题标题】:Why configure both dataSource and sessionFactory in Spring-Hibernate Configuration?为什么在 Spring-Hibernate Configuration 中同时配置 dataSource 和 sessionFactory?
【发布时间】:2012-11-19 04:02:02
【问题描述】:

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

我的webapp-servlet.xmlspring 配置文件:

<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. 当所有需要的数据都已经包含在休眠配置文件中时,为什么我需要配置一个 DataSource bean? Hibernate 是否有一些可以使用的默认值?
  2. 还有哪些DataSources 我可以使用?
  3. 我是否缺少任何其他 bean 或配置参数/属性来让 hibernate 与我的应用程序一起工作?

【问题讨论】:

  • hibernate.cfg.xml 中的所有配置选项都可以在 LocalSessionFactoryBean 上使用,更喜欢后者并在 Hibernate 配置中跳过它们。
  • 好的。但我仍然只希望它们在一个地方,而不是在不同的文件或 bean 中重复。
  • 这就是我要说的,从hibernate.cfg.xml 中删除数据源配置并将其保留在Spring XML 中。您可以稍后使用相同的 dataSource bean,例如在JdbcTemplate.

标签: java spring hibernate


【解决方案1】:
  1. 你不需要这两个。您可以摆脱hibernate.cfg.xml 并在LocalSessionFactoryBean 中配置所有内容,或者按原样重用现有的hibernate.cfg.xml(在这种情况下,您不需要在Spring config 中配置DataSource)。

  2. 您有以下选择:

    • 使用embedded database - 有利于测试和学习目的

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

    • 使用连接池,如 DBCP 或 c3p0

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

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

    • 声明HibernateTransactionManager

    • 添加 &lt;tx:annotation-driven&gt; 以启用声明式事务管理 (@Transactional)

    • 如果您想使用程序化事务管理,请声明 TransactionTemplate(使用它来克服声明式事务管理的限制)

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

【讨论】:

  • Hibernate默认不使用C3P0吗?感谢交易管理,我也在寻找。接受为完整答案。
  • @SotiriosDelimanolis:如果您使用 hibernate.cfg.xml 配置它 - 是的。但是 Spring 将 Hibernate 配置与 DataSource 配置解耦,因此您可以使用不同的 DataSource 实现。
猜你喜欢
  • 2015-12-31
  • 1970-01-01
  • 2013-01-26
  • 1970-01-01
  • 2017-02-10
  • 1970-01-01
  • 1970-01-01
  • 2011-12-28
  • 2018-06-07
相关资源
最近更新 更多