TransactionSynchronizationManager绑定一个事物并且在事物提交之后操作

博客展示了一段Java代码,使用Spring框架进行事务处理。通过引入相关类,开启事务注解,定义了一个保存数据的方法,将数据封装到模型中并调用DAO层方法保存,最后打印出数据的ID。

import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.transaction.support.TransactionSynchronizationAdapter;

@Transactional(readOnly = false,propagation=Propagation.REQUIRED)//开事物
public void save(String name,Integer age ,BigDecimal amount){
Zexample1Model zexample1Model = new Zexample1Model();
zexample1Model.setName(name+"_");
zexample1Model.setAge(age);
zexample1Model.setAmount(amount);
zexample1Model.setAddTime(new Date());
zexample1Model.setStatus(1);
zexample1Dao.save(zexample1Model);
System.out.println(“id=”+zexample1Model.getId());

TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
    @Override
    public void afterCommit() {
        System.out.println("send email after transaction commit...");
    }
});
System.out.println("this method complete....");

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值