博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring事务监控(分布式事务1)
阅读量:5912 次
发布时间:2019-06-19

本文共 3330 字,大约阅读时间需要 11 分钟。

hot3.png

先写一个最最简单的插入例子来探讨Spring事务监控.

model

@Datapublic class User implements Serializable {    private long id;    private String name;    private int age;}

dao(此处数据库有一个user表,3个字段,id自增)

@Mapperpublic interface UserDao {    @Options(useGeneratedKeys = true, keyProperty = "id")    @Insert("insert into user (name,age) values (#{name},#{age})")    void insert(User user);}

service

public interface UserService {    public void insert(User user);}
@Transactional@Servicepublic class UserServiceImpl implements UserService {    @Autowired    private ApplicationEventPublisher publisher;    @Autowired    private UserDao userDao;    @Override    public void insert(User user) {        userDao.insert(user);        //重点在于此处对事务进行侦听        publisher.publishEvent(user);    }}

listener(对Spring事务侦听,并进行处理)

@Componentpublic class UserTransactionEventListener {    @TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT)    public void beforeCommit(PayloadApplicationEvent
event) { System.out.println("before commit, id: " + event.getPayload().getId()); } @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) public void afterCommit(PayloadApplicationEvent
event) { System.out.println("after commit, id: " + event.getPayload().getId()); } @TransactionalEventListener(phase = TransactionPhase.AFTER_COMPLETION) public void afterCompletion(PayloadApplicationEvent
event) { System.out.println("after completion, id: " + event.getPayload().getId()); } @TransactionalEventListener(phase = TransactionPhase.AFTER_ROLLBACK) public void afterRollback(PayloadApplicationEvent
event) { System.out.println("after rollback, id: " + event.getPayload().getId()); }}

controller

@RestControllerpublic class UserController {    @Autowired    private UserService userService;    @PostMapping("/save")    public Map
save(@RequestBody User user) { Map
result = new HashMap(); userService.insert(user); result.put("code","200"); result.put("body",user); result.put("msg","success"); return result; }}

运行结果

{

    "msg": "success",
    "code": "200",
    "body": {
        "id": 1,
        "name": "miky",
        "age": 21
    }
}

日志片段

2018-09-17 14:42:44.384  INFO 10696 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''

2018-09-17 14:42:44.387  INFO 10696 --- [           main] com.guanjian.TransactionApplication      : Started TransactionApplication in 3.987 seconds (JVM running for 5.122)
2018-09-17 14:45:46.915  INFO 10696 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-09-17 14:45:46.915  INFO 10696 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2018-09-17 14:45:46.930  INFO 10696 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 15 ms
2018-09-17 14:45:47.047  INFO 10696 --- [nio-8080-exec-1] com.alibaba.druid.pool.DruidDataSource   : {dataSource-1} inited
before commit, id: 1
after commit, id: 1
after completion, id: 1

说明侦听成功,可以进行分布式事务的下一步处理,发送MQ消息等等.

转载于:https://my.oschina.net/u/3768341/blog/2052397

你可能感兴趣的文章
linux sar命令详解
查看>>
通过Gearman实现MySQL到Redis的数据复制
查看>>
eclipse 自动为getter和setter添加注释
查看>>
我的友情链接
查看>>
DataSet
查看>>
Quartz.NET 前一次任务未执行完成时不触发下次的解决方法
查看>>
python unittest之断言及示例
查看>>
online_judge_1106
查看>>
JAVA_内部类
查看>>
jxl 导入excel
查看>>
虚拟机linux上网问题
查看>>
XMLHttpRequest - 原始AJAX初步
查看>>
laravel/lumen 单元测试
查看>>
csu2161: 漫漫上学路(Hash+最短路)
查看>>
重复引用错误:duplicate symbols for architecture x86_64
查看>>
ucenter1.5通讯过程分析(转载)
查看>>
浏览器中可以访问,但是git命令、go get命令使用时却无法连接
查看>>
Apache Spark源码走读之7 -- Standalone部署方式分析
查看>>
如何避免重构带来的危险
查看>>
有序的双链表
查看>>