spring事务管理(一)

    科技2022-07-20  119

    spring事务的实现(一):手动编程

    手动编程的要点: 1.接口: DataSourceTransactionManager 2.事务管理模板类:transactionTemplate 手动编程步骤(以转账为例): 1.业务的层的哪些方法需要事务

    package com.cc.service; public interface IUserInfoService { /* * from:转出的账户 to:转入的账户 money:转的金额 * */ public void zz(int from, int to, int money) throws Exception; }

    2.在spring-datasource.xml中配置事务管理器 (注意:不同的orm框架 事务管理的方式不同)

    <!--1、spring针对MyBaits的事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!--2、手动事务模板 --> <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate"> <property name="transactionManager" ref="transactionManager"></property> </bean>

    3.在需要事务的业务层编写方法:

    import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.transaction.TransactionStatus; import org.springframework.transaction.support.TransactionCallbackWithoutResult; import org.springframework.transaction.support.TransactionTemplate; import com.cc.mapper.UserInfoMapper; import com.cc.model.UserInfo; import com.cc.service.IUserInfoService; //业务层 @Component public class UserInfoServiceImpl1 implements IUserInfoService { @Autowired private UserInfoMapper userInfoMapper; @Autowired//在业务类定义TransatcionTemplate事务模板类 private TransactionTemplate transactionTemplate; // @Autowired // public UserInfoServiceImpl1(PlatformTransactionManager transactionManager) { // this.transactionTemplate = new TransactionTemplate(transactionManager); // } /** * 转账业务:旺财(from)转账给来福(to),转money钱 * * 旺财(from) 更新操作 减money 来福 (to) 更新操作 加money money =200 * * * zz(1,2,300) */ @Override public void zz(int from, int to, int money) {//事务管理的转账方法 transactionTemplate.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { try { // 转账的操作 zzOptional(from, to, money); System.out.println("转账成功"); } catch (Exception ex) { System.out.println("转账失败,进行回滚"); status.setRollbackOnly(); } } }); } private void zzOptional(int from, int to, int money) throws Exception { // 一,==============旺财的钱减少300============== // 1.1查询旺财有多少钱 UserInfo wc = userInfoMapper.selectByPrimaryKey(from); System.out.println(wc); // 1.2扣旺财有300 wc.setMoney(wc.getMoney() - money); int result = userInfoMapper.updateByPrimaryKey(wc); // ==============二,来福的钱增加300============== // 1.1查询旺财有多少钱 UserInfo lf = userInfoMapper.selectByPrimaryKey(to); System.out.println(lf); // 1.2扣旺财有300 lf.setMoney(lf.getMoney() + money); int result2 = userInfoMapper.updateByPrimaryKey(lf); // ==============最后的结果============== if (result > 0 && result2 > 0) { System.out.println("转账成功!!"); } else { // int a = 10/0;//产生一个异常 throw new Exception("转账失败");// 产生一个异常 } } }

    4.编写测试类,测试方法

    @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:spring-datasource.xml") public class SpringTrasactionTest { @Autowired private UserInfoMapper userInfoMapper; // 编程(手动)式事务的业务层类 @Autowired private UserInfoServiceImpl1 UserInfoServiceImpl1; //转账成功的测试方法 @Test public void whenZzSuccess() { UserInfoServiceImpl1.zz(1, 2, 100); }
    Processed: 0.010, SQL: 8