transaction学习列子

http://www.byteslounge.com/tutorials/spring-transaction-propagation-tutorial

package com.tgb.service;

import javax.annotation.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service(value="sService")
public class StudentService {

    @Resource
    private TeacherService tService;
    
    @Resource
    private JdbcTemplate jdbcTemplate;
    
    @Transactional(propagation = Propagation.REQUIRED)
    public void addStudent(){
        String sql = "insert into student(name) values('st0')";
        jdbcTemplate.execute(sql);
//      try{
//          int a = Integer.parseInt("s");
//      }catch(Exception e){
//          throw new RuntimeException();
//      }
//      tService.addTeacher();
//      tService.addTeacher6();
//      tService.addTeacher1();
        tService.addTeacher3();
//      tService.addTeacher4();
//      tService.addTeacher5();
        
//      throw new RuntimeException();
    }
    
    
    @Transactional(propagation = Propagation.MANDATORY)
    public void addStudent1(){
        String sql = "insert into student(name) values('st1')";
        jdbcTemplate.execute(sql);
    }
    
    @Transactional(propagation = Propagation.NESTED)
    public void addStudent2(){
        String sql = "insert into student(name) values('st2')";
        jdbcTemplate.execute(sql);
        tService.addTeacher2();
//      throw new RuntimeException();
    }
    
    @Transactional(propagation = Propagation.NEVER)
    public void addStudent3(){
        String sql = "insert into student(name) values('st3')";
        jdbcTemplate.execute(sql);
        tService.addTeacher6();
        throw new RuntimeException();
    }
    
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public void addStudent4(){
        String sql = "insert into student(name) values('st4')";
        jdbcTemplate.execute(sql);
        tService.addTeacher4();
        throw new RuntimeException();
    }
    
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void addStudent5(){
        String sql = "insert into student(name) values('st5')";
        jdbcTemplate.execute(sql);
        tService.addTeacher5();
        throw new RuntimeException();
    }
    
    @Transactional(propagation = Propagation.SUPPORTS)
    public void addStudent6(){
        String sql = "insert into student(name) values('st6')";
        jdbcTemplate.execute(sql);
        tService.addTeacher6();
        throw new RuntimeException();
    }


    public TeacherService gettService() {
        return tService;
    }


    public void settService(TeacherService tService) {
        this.tService = tService;
    }
}

package com.tgb.service;

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

@Service
public class TeacherService {

    
    @Autowired
    private JdbcTemplate jdbcTemplate;
    
    /**
     * REQUIRED
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void addTeacher(){
        String sql = "insert into teacher(name) values ('t0')";
        jdbcTemplate.execute(sql);
//      throw new RuntimeException();
    }
    
    
    /**
     * MANDATORY
     */
    @Transactional(propagation = Propagation.MANDATORY)
    public void addTeacher1(){
        String sql = "insert into teacher(name) values ('t1')";
        jdbcTemplate.execute(sql);
    }
    
    
    /**
     * NESTED
     */
    @Transactional(propagation = Propagation.NESTED)
    public void addTeacher2(){
        String sql = "insert into teacher(name) values ('t2')";
        jdbcTemplate.execute(sql);
    }
    
    
    /**
     * NEVER
     */
    @Transactional(propagation = Propagation.NEVER)
    public void addTeacher3(){
        String sql = "insert into teacher(name) values ('t3')";
        jdbcTemplate.execute(sql);
    }
    
    
    /**
     * NOT_SUPPORTED
     */
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public void addTeacher4(){
        String sql = "insert into teacher(name) values ('t4')";
        jdbcTemplate.execute(sql);
        throw new RuntimeException();
    }
    
    
    /**
     * REQUIRES_NEW
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void addTeacher5(){
        String sql = "insert into teacher(name) values ('t5')";
        jdbcTemplate.execute(sql);
    }
    
    
    /**
     * SUPPORTS
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    public void addTeacher6(){
        String sql = "insert into teacher(name) values ('t6')";
        jdbcTemplate.execute(sql);
    }
}

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 128,941评论 18 137
  • 前些日子从@张鑫旭微博处得一份推荐(Front-end-tutorial),号称最全的资源教程-前端涉及的所有知识...
    谷子多阅读 3,546评论 0 45
  • 【搜索分区】 《知识点》 Input这种带有功能的东西千万不要加超链接。因为会发生功能冲突,超链接本身也是一个功能...
    黑色的夜阅读 84评论 0 0
  • 荒郊猎小鹿 打死包白茅 少女春心动 猎人送上前 小树砍柴烧 小鹿死荒郊 白茅捆扎好 佳人请收好 请你慢慢来 没碰围...
    黄耶鲁阅读 268评论 0 8
  • 现在的宝妈都爱美,每天出门都打扮一番,小孩子也跟着爱美一番。所以宝妈们都为孩子选各种漂亮衣服,不错过任何打扮宝宝穿...
    有习惯阅读 185评论 0 0