为什么在Spring-Hibernate配置中配置dataSource和sessionFactory? [英] Why configure both dataSource and sessionFactory in Spring-Hibernate Configuration?

查看:143
本文介绍了为什么在Spring-Hibernate配置中配置dataSource和sessionFactory?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 < hibernate构型> 
< 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>更新< / property>
< / session-factory>
< / hibernate-configuration>

我的 webapp-servlet.xml spring config file:

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

< bean id =dataSourceclass =org.apache.commons.dbcp.BasicDataSource>
< property name =driverClassNamevalue =com.mysql.jdbc.Driver/>
< property name =urlvalue =jdbc:mysql:// localhost:3306 / test/>
< property name =usernamevalue =root/>
< property name =passwordvalue =root/>
< property name =maxActivevalue =10/>
< / bean>
< / beans>




  1. 为什么我需要在所有数据都配置一个DataSource bean是否已包含在hibernate配置文件中? Hibernate是否有一些可以使用的默认值?

  2. 什么是其他的 DataSource s我可以使用吗?
  3. 我是否缺少任何其他bean或配置参数/属性以使hibernate与我的应用程序一起工作? / div>


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


    2. 您可以选择以下选项: 使用嵌入式数据库 - 对测试和学习有好处

    3. 使用 DriverManagerDataSource - 这是一个简单的非集中可用于测试的数据源等(不推荐用于生产用途) 使用连接池(如DBCP或c3p0)
    4. 如果您部署到应用程序服务器,则可以使用应用程序服务器提供的连接池使用JNDI




    5. 您当前的配置已足够,但它不支持 Spring事务管理。为了启用它,您需要


      • 声明 HibernateTransactionManager < tx:annotation-driven> 来启用声明式事务管理(如果你想使用编程事务,则声明 TransactionTemplate

      • 管理(使用它来克服声明式事务管理的限制)
      • 另外不要忘记从Hibernate配置中删除事务相关的属性,因为它们可能会与Spring冲突交易管理




    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.xml file:

    <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.xml spring config file:

    <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. You don't need both of them. You can either get rid of hibernate.cfg.xml and configure everything in LocalSessionFactoryBean, or reuse your existing hibernate.cfg.xml as is (in this case you don't need to configure DataSource in 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 TransactionTemplate if 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

    这篇关于为什么在Spring-Hibernate配置中配置dataSource和sessionFactory?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆