spring 使用注解事物管理例子

1,spring 使用注解的形式进行事物管理.主要代码如下,整个项目的代码点击这里下载.


service代码如下:


package com.junlenet.spring.transactional.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.junlenet.spring.transactional.Util;
import com.junlenet.spring.transactional.mappers.RoleMapper;
import com.junlenet.spring.transactional.model.Role;

/**
 * spring service
 * @author huweijun
 * @date 2016年8月10日 下午8:19:49
 */
@Service
public class SpringTransactionalService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private AnotherSpringTransactionalService anotherTxService;

    /**
     * 无事务注解 (提交至数据库)
     * @author huweijun
     * @date 2016年8月10日 下午8:37:57
     */
    public void noTransAnnotation() {
        Role role = new Role();
        role.setName("not trans annotation");
        role.setCreateTime(new Date());
        roleMapper.insert(role);
        throw new RuntimeException("not trans annotation exception");
    }

   /**
    * 正常提交(事物提交)
    * @author huweijun
    * @date 2016年8月10日 下午8:39:46
    */
    @Transactional
    public void normalCommit() {
        Role role = new Role();
        role.setCreateTime(new Date());
        role.setName("normal commit");
        roleMapper.insert(role);
    }

    /**
     * 抛出异常(事物提交)
     * @throws Exception
     * @author huweijun
     * @date 2016年8月10日 下午8:40:15
     */
    @Transactional
    public void commitOnCheckedException() throws Exception {
        Role role = new Role();
        role.setName("commit on checked exception");
        role.setCreateTime(new Date());
        roleMapper.insert(role);
        throw new Exception("rollback checked exception");
    }

    /**
     * 为某异常回滚(事物回滚)
     * @throws Exception
     * @author huweijun
     * @date 2016年8月10日 下午8:41:13
     */
    @Transactional(rollbackFor = Exception.class )
    public void rollbackOnCheckedException() throws Exception {
        Role role = new Role();
        role.setName("roll back on checked exception");
        role.setCreateTime(new Date());
        roleMapper.insert(role);
        throw new Exception("rollback checked exception");
    }

    /**
     * 事务方法中抛异常(事物未提交)
     * @author huweijun
     * @date 2016年8月10日 下午8:42:14
     */
    @Transactional
    public void throwExceptionInMethod() {
        Role role = new Role();
        role.setName("throwExceptionInTransMethod");
        role.setCreateTime(new Date());
        roleMapper.insert(role);
        throw new RuntimeException("throw exception in transaction method");
    }

   /**
    * 同一事务中获取未提交数据(事物未提交)
    * @author huweijun
    * @date 2016年8月10日 下午8:49:39
    */
    @Transactional
    public void getNotCommitDataInTheSameTransaction(){
        Role role = new Role();
        role.setName("not commit role");
        role.setCreateTime(new Date());
        roleMapper.insert(role);
        List<Role> roles = roleMapper.selectRoleByName("not commit role");
        System.out.println(roles.get(0).getName());//有数据的
        throw new RuntimeException("not commit data");
    }

   /**
    * 事务方法中调用同一个类中的另一个事务方法(事物未提交)
    * @author huweijun
    * @date 2016年8月10日 下午8:50:06
    */
    @Transactional
    public void invokeRequireNewTransMethodInSameClass() {
        requireNewTransactionMethod();//事物未提交
        throw new RuntimeException("throw trans method in same class exception.");
    }

    /**
     *  务方法中调用另一个类中的另一个事务方法
     * @author huweijun
     * @date 2016年8月10日 下午8:56:26
     */
    @Transactional
    public void invokeRequireNewTransMethodInAnotherService(){
        Role role = new Role();
        role.setName("require_new_transaction_in_another_service");
        role.setCreateTime(new Date());
        roleMapper.insert(role);// 事物未提交
        anotherTxService.requireNewTrans(); //事物提交
        throw new RuntimeException("throw invoke require new trans method in another service");
    }

    /**
     * 开启新的事物
     * @author huweijun
     * @date 2016年8月10日 下午8:57:54
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void requireNewTransactionMethod() {
        Util.printStackInfo();
        Role role = new Role();
        role.setCreateTime(new Date());
        role.setName("require_new_transaction");
        roleMapper.insert(role);
    }
    
}

another service

package com.junlenet.spring.transactional.service;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.junlenet.spring.transactional.Util;
import com.junlenet.spring.transactional.mappers.RoleMapper;
import com.junlenet.spring.transactional.model.Role;

/**
 * 另一个spring service
 * @author huweijun
 * @date 2016年8月10日 下午8:20:23
 */
@Service
public class AnotherSpringTransactionalService {

    @Autowired
    private RoleMapper roleMapper;

    /**
     * 另开启一个新的事物
     * @author huweijun
     * @date 2016年8月10日 下午8:57:41
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public  void requireNewTrans(){
        Util.printStackInfo();
        Role role = new Role();
        role.setCreateTime(new Date());
        role.setName("anotherTxService require new trans");
        roleMapper.insert(role);
    }
}


猜你喜欢

转载自blog.csdn.net/huweijun_2012/article/details/52176433