Spring Hibernate 事务管理

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

Spring Hibernate transaction management

springhibernatetransactions

提问by Sushant Gupta

I have just started making a project using spring and hibernate. My DAO layer class extends HibernateDaoSupport. We are not using annotations. Earlier, we were using struts, hence we used getTransaction, commit, rollback .. methods provided by Session class. My requirement is very simple, for all DAO classes, if there is an exception, rollback otherwise commit. Please suggest a simplest way of introducing spring transaction management.

我刚刚开始使用 spring 和 hibernate 制作一个项目。我的 DAO 层类扩展了 HibernateDaoSupport。我们没有使用注释。之前,我们使用的是 struts,因此我们使用了 Session 类提供的 getTransaction、commit、rollback .. 方法。我的要求很简单,对于所有的 DAO 类,如果有异常,则回滚,否则提交。请提出一个最简单的介绍 spring 事务管理的方法。

回答by Sashi

A few things are not clear from your question. My explanation follows based on below assumptions -

从您的问题中,有几件事不清楚。我的解释基于以下假设 -

  • You are using spring to create a datasource and session factory
  • You are using Java 5 or above and could use annotations.
  • 您正在使用 spring 创建数据源和会话工厂
  • 您使用的是 Java 5 或更高版本,并且可以使用注释。

Here is what your spring configuration would look like.

这是您的弹簧配置的样子。

    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="org.hsqldb.jdbcDriver" />
    <property name="url" value="jdbc:hsqldb:hsql://localhost:9001" />
    <property name="username" value="sa" />
    <property name="password" value="" />
</bean>

<bean id="mySessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="myDataSource" />
    <property name="mappingResources">
        <list>
            <value>product.hbm.xml</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <value>
            hibernate.dialect=org.hibernate.dialect.HSQLDialect
        </value>
    </property>
</bean>

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

<tx:annotation-driven transaction-manager="transactionManager"  />

Once this is set up, you could use spring transactional annotations on your DAO methods as shown below. Spring would take care of starting transactions, committing your transactions or rolling back your transactions when exceptions are thrown. If you have business services, you would ideally use transactional annotations on your services instead of DAOs.

设置完成后,您可以在 DAO 方法上使用 spring 事务注释,如下所示。Spring 将负责启动事务、提交事务或在抛出异常时回滚事务。如果您有业务服务,最好在您的服务上使用事务注释而不是 DAO。

@Transactional(propagation=Propagation.REQUIRED)
public class MyTestDao extends HibernateDaoSupport {    
public void saveEntity(Entity entity){
    getHibernateTemplate().save(entity);
}
@Transactional(readOnly=true)
public Entity getEntity(Integer id){
    return getHibernateTemplate().get(Entity.class, id);
}
 }

Below code shows how transaction management could be achieve using spring's support for AOP rather than annotations.

下面的代码显示了如何使用 spring 对 AOP 的支持而不是注释来实现事务管理。

    <!-- Define your 'myDatasource' bean and 'mySessionFactory' bean as shown in previous code snippet -->
<!--  Then follow the steps shown below -->

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

<!-- this is the dao object that we want to make transactional -->
<bean id="testDao" class="com.xyz.daos.MyTestDao" />

<!-- the transactional advice  -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <!-- all methods starting with 'get' are read-only -->
        <tx:method name="get*" read-only="true" />
        <!-- other methods use the default transaction settings (see below) -->
        <tx:method name="*" propagation="REQUIRED" />
    </tx:attributes>
</tx:advice>

<!-- ensure that the above transactional advice runs for any execution of 
    a method in 'daos' package-->
<aop:config>
    <aop:pointcut id="allDaoMethods"
        expression="execution(* com.xyz.daos.*(..))" />
    <aop:advisor advice-ref="txAdvice" pointcut-ref="allDaoMethods" />
</aop:config>

For additional details, please see - Spring Declarative Transactions

有关其他详细信息,请参阅 - Spring 声明式事务