Spring声明式事务管理的配置

Spring声明式事物管理的配置有多种,这里只讲解两种配置。

这里用的例子来自:

在这里例子的基础上,我们添加Spring声明式事务管理的配置

第一种:使用tx标签配置的拦截器(这里是hibernate5)

  找到ApplicationContext.xml文件

  在这份文件中添加如下代码进行配置:

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

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="del*" propagation="REQUIRED" />
            <tx:method name="mod*" propagation="REQUIRED" />
            <tx:method name="*" propagation="REQUIRED" read-only="true" />
        </tx:attributes>
    </tx:advice>

    <aop:config>
        <aop:pointcut id="interceptorPointCuts" expression="execution(* news.dao.*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="interceptorPointCuts" />
    </aop:config>

 

例如:没有在配置文件里配置声明式事务管理时dao类如下:

package ssh.dao;

import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import news.entity.News;

@Repository
@Scope("prototype")//非单例
public class IndexDaoImpl implements IndexDao { @Autowired //@Qualifier("mySessionFactory") //@Resource(name="mySessionFactory") private SessionFactory sf; @Override public List showAllBookCard() { Session session = sf.openSession(); //Session session = sf.getCurrentSession(); session.getTransaction().begin(); Query query = session.createQuery("from BookCard"); List<BookCard> allBookCardList = query.getResultList(); session.getTransaction().commit(); session.close(); return allBookCardList; } }

 

配置文件用了声明式事务管理之后,我们就可以在dao类省略事务,改后如下:

package ssh.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import news.entity.News;

public class IndexDaoImpl implements IndexDao {

    @Autowired
    //@Qualifier("mySessionFactory")
    //@Resource(name="mySessionFactory")
    private SessionFactory sf;

    @Override
    public List showAllBookCard() {
        
        Session session = sf.getCurrentSession();
        Query query = session.createQuery("from BookCard");
        List<BookCard> allBookCardList  = query.getResultList();
        
        //Session session = sf.openSession();
        //session.getTransaction().begin();
        //session.getTransaction().commit();
        //session.close();
    
        return allBookCardList;
    }

}

 

第二种:全注解

 当我们使用全注解时,我们需要将第一种方法添加的代码进行修改,把下面这段代码删掉:

   <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="del*" propagation="REQUIRED" />
            <tx:method name="mod*" propagation="REQUIRED" />
            <tx:method name="*" propagation="REQUIRED" read-only="true" />
        </tx:attributes>
    </tx:advice>

    <aop:config>
        <aop:pointcut id="interceptorPointCuts"
         expression="execution(* news.dao.*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="interceptorPointCuts" />
    </aop:config>

留下这段代码,再加上另一句代码就行了:

<!--声明式事务管理-->
    <bean id="transactionManager"
     class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
  <tx:annotation-driven transaction-manager="transactionManager"/>

此时我们需要在service上加@Transactional注解(为什么在service加,而不在dao加呢,因为service调用dao的方法,还因为service不一定从dao拿数据,所以在service加的话会更加好一些),如下:

package ssh.service;

import java.util.List;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Service
@Scope("prototype")
public class IndexSerivceIpml implements IndexService {

    @Override
    public List showAllBookCard() {
        // TODO Auto-generated method stub
        return null;
    }

}

 

事物的传播属性:

  • Propagation :key属性确定代理应该给哪个方法增加事务行为。这样的属性最重要的部份是传播行为。有以下选项可供使用:
  • PROPAGATION_REQUIRED--支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。
  • PROPAGATION_REQUIRED--加入当前正要执行的事务不在另外一个事务里,那么就起一个新的事务
  • PROPAGATION_SUPPORTS--支持当前事务,如果当前没有事务,就以非事务方式执行。
  • PROPAGATION_MANDATORY--支持当前事务,如果当前没有事务,就抛出异常。
  • PROPAGATION_REQUIRES_NEW--新建事务,如果当前存在事务,把当前事务挂起。
  • PROPAGATION_NOT_SUPPORTED--以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
  • PROPAGATION_NEVER--以非事务方式执行,如果当前存在事务,则抛出异常。

 

两种配置各有特色,我们很难说两种配置方式孰优孰劣,但是如果能够灵活搭配两种方式,一定能够进一步提升开发效率。

 

posted on 2016-10-21 14:51  唯_爱  阅读(361)  评论(0编辑  收藏  举报

导航