Spring Hibernate 事务管理 [英] Spring Hibernate transaction management

查看:21
本文介绍了Spring Hibernate 事务管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

从您的问题中,有些事情没有说清楚.我的解释基于以下假设 -

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

  • 您正在使用 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"  />

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

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.

@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);
}
 }

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

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

    <!-- 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>

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

For additional details, please see - Spring Declarative Transactions

这篇关于Spring Hibernate 事务管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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