spring事务的配置使用

来源:互联网 发布:淘宝特卖入口 编辑:程序博客网 时间:2024/06/06 19:57

第一种:编程式事务管理其中一种
配置文件中配置信息:

<bean id="txManagerForStatus"       class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <property name="dataSource" ref="dataSource" />    </bean>

事务使用的代码:

    @Autowired    @Qualifier("sqlSession")    private SqlSessionTemplate sqlSession;    //    @Autowired    @Qualifier("txManagerForStatus")    private DataSourceTransactionManager txManager;@Override    public int updateByPrimaryKey(JobMessage vo) {        log.info("updateByPrimaryKey start,vo=" +vo);        int exeStatus = 1;        TransactionDefinition td = new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRED);        TransactionStatus ts = this.txManager.getTransaction(td);        try {            exeStatus = sqlSession.update("jobMessage.updateByPrimaryKey", vo);        } catch (Throwable t) {            txManager.rollback(ts);            log.error(t.getMessage());        }        txManager.commit(ts);        String msUrl = PropertiesUtil.getPropertyValue("MS_URL");        log.info("MS_URL=" + msUrl);        MessageFormat myFormat=new MessageFormat( msUrl);         String url =myFormat.format(new String[]{String.valueOf(vo.getJobSeq()),vo.getTaskId()});        HttpClientUtils.doGet(url);        log.info("updateByPrimaryKey end ");        return exeStatus;    }

第二种:声明式事务
配置信息:

<bean id="sitdbTxManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <property name="dataSource" ref="sitdb" />    </bean>    <tx:annotation-driven transaction-manager="sitdbTxManager"/>

还要在spring-servlet.xml中添加tx名字空间

<beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:context="http://www.springframework.org/schema/context"    xmlns:jms="http://www.springframework.org/schema/jms"    xmlns:tx="http://www.springframework.org/schema/tx"    xmlns:aop="http://www.springframework.org/schema/aop"

MyBatis自动参与到spring事务管理中,无需额外配置,只要org.mybatis.spring.SqlSessionFactoryBean引用的数据源与DataSourceTransactionManager引用的数据源一致即可,否则事务管理会不起作用。
另外需要下载依赖包aopalliance.jar放置到WEB-INF/lib目录下。否则spring初始化时会报异常
java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor

默认情况下,只有来自外部的方法调用才会被AOP代理捕获,也就是,类内部方法调用本类内部的其他方法并不会引起事务行为,即使被调用方法使用@Transactional注解进行修饰。

@Transactional(readOnly = true)public class DefaultFooService implements FooService {  public Foo getFoo(String fooName) {    // do something  }  // these settings have precedence for this method  //方法上注解属性会覆盖类注解上的相同属性  @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)  public void updateFoo(Foo foo) {    // do something  }}

@Transactional属性

 属性类型描述valueString可选的限定描述符,指定使用的事务管理器propagationenum: Propagation可选的事务传播行为设置isolationenum: Isolation可选的事务隔离级别设置readOnlyboolean读写或只读事务,默认读写timeoutint (in seconds granularity)事务超时时间设置rollbackForClass对象数组,必须继承自Throwable导致事务回滚的异常类数组rollbackForClassName类名数组,必须继承自Throwable导致事务回滚的异常类名字数组noRollbackForClass对象数组,必须继承自Throwable不会导致事务回滚的异常类数组noRollbackForClassName类名数组,必须继承自Throwable不会导致事务回滚的异常类名字数组

用法

@Transactional 可以作用于接口、接口方法、类以及类方法上。当作用于类上时,该类的所有 public 方法将都具有该类型的事务属性,同时,我们也可以在方法级别使用该标注来覆盖类级别的定义。

虽然 @Transactional 注解可以作用于接口、接口方法、类以及类方法上,但是 Spring 建议不要在接口或者接口方法上使用该注解,因为这只有在使用基于接口的代理时它才会生效。另外, @Transactional 注解应该只被应用到 public 方法上,这是由 Spring AOP 的本质决定的。如果你在 protected、private 或者默认可见性的方法上使用 @Transactional 注解,这将被忽略,也不会抛出任何异常。

引用:http://www.cnblogs.com/xusir/p/3650522.html
两种的区别:
声明式的事务最小的粒度只能作用到方法级别,无法做到像编程式事务那样可以作用到代码块级别。变通方法:可以将需要进行事务管理的代码块独立为方法等等

0 0
原创粉丝点击