栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring的事务控制

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring的事务控制

Spring的事务控制

1 基于 XML 的声明式事务控制2 基于注解的声明式事务控制

1 基于 XML 的声明式事务控制

事务控制最常见的场景为银行的转账业务,要么同时成功,要么同时失败,这里搭建一两银行转账业务使用xml声明式事务控制。
1)环境所需pom.xml

war


    
        org.springframework
        spring-context
        5.0.5.RELEASE
    
    
        org.aspectj
        aspectjweaver
        1.8.4
    
    
        org.springframework
        spring-jdbc
        5.0.5.RELEASE
    
    
        org.springframework
        spring-tx
        5.0.5.RELEASE
    
    
        org.springframework
        spring-test
        5.0.5.RELEASE
    
    
        c3p0
        c3p0
        0.9.1.1
    
    
        mysql
        mysql-connector-java
        8.0.25
    
    
        junit
        junit
        4.12
    

2)controller层:

public class AccountController {
    public static void main(String[] args) {
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        AccountService accountService = app.getBean(AccountService.class);
        accountService.transfer("张无忌", "郭靖", 500);
    }
}

3)service层:

public class AccountServiceImpl implements AccountService {

    private AccountDao accountDao;

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }
    
    public void transfer(String outMan, String inMan, double money) {   //转账业务
        accountDao.out(outMan, money);
        int a = 1 / 0;      //自造异常
        accountDao.in(inMan, money);
    }
}

4)dao层:该层使用的是jdbcTemplate模板技术,如果使用其他技术后面applicationContext.xml中配置的平台事务管理器会有差别

public class AccountDaoImpl implements AccountDao {

    private JdbcTemplate jdbcTemplate;
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public void out(String outMan, double money) {
        jdbcTemplate.update("update account set money=money-? where name=?",money,outMan);
    }

    public void in(String inMan, double money) {
        jdbcTemplate.update("update account set money=money+? where name=?",money,inMan);
    }
}

5)applicationContext.xml配置



    
    
        
        
        
        
    

    
        
    

    
        
    

    
    
        
    

    
    
        
    
    
    
    
        
            
        
    
    
    
    
        
    
    


2 基于注解的声明式事务控制

① 使用 @Transactional 在需要进行事务控制的类或是方法上修饰,注解可用的属性同 xml 配置方式,例如隔离级别、传播行为等。
② 注解使用在类上,那么该类下的所有方法都使用同一套注解参数配置。
③ 使用在方法上,不同的方法可以采用不同的事务参数配置。
④ Xml配置文件中要开启事务的注解驱动

@Service("accountService")
@Transactional
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao;

    @Transactional(isolation = Isolation.READ_COMMITTED ,propagation = Propagation.REQUIRED)
    public void transfer(String outMan, String inMan, double money) {   //转账业务
        accountDao.out(outMan, money);
        int a = 1 / 0;      //自造异常
        accountDao.in(inMan, money);
    }
}

    
    
    
    



    





    




转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/782733.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号