打开事务的后台日志(打上@Transactional注解)
2020-10-15 22:40:40.324 DEBUG 5544 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet : POST "/praise", parameters={}
2020-10-15 22:40:40.325 DEBUG 5544 --- [nio-8080-exec-3] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.xiaogang.redissondemo.praise.controller.PraiseController#add(PraiseAddParam)
2020-10-15 22:40:40.325 DEBUG 5544 --- [nio-8080-exec-3] m.m.a.RequestResponseBodyMethodProcessor : Read "application/json;charset=UTF-8" to [PraiseAddParam(blogId=100, userId=101)]
2020-10-15 22:40:40.325 DEBUG 5544 --- [nio-8080-exec-3] o.s.j.d.DataSourceTransactionManager : Creating new transaction with name [com.xiaogang.redissondemo.praise.service.impl.PraiseServiceImpl.addPraise]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
2020-10-15 22:40:40.368 DEBUG 5544 --- [nio-8080-exec-3] o.s.j.d.DataSourceTransactionManager : Acquired Connection [HikariProxyConnection@219078864 wrapping com.mysql.cj.jdbc.ConnectionImpl@1027ecc0] for JDBC transaction
2020-10-15 22:40:40.369 DEBUG 5544 --- [nio-8080-exec-3] o.s.j.d.DataSourceTransactionManager : Switching JDBC Connection [HikariProxyConnection@219078864 wrapping com.mysql.cj.jdbc.ConnectionImpl@1027ecc0] to manual commit
2020-10-15 22:40:40.412 DEBUG 5544 --- [nio-8080-exec-3] c.x.r.p.m.P.selectByBlogUserId : ==> Preparing: select * from praise where blog_id =? and user_id = ? and status = 1 and is_active = 1
2020-10-15 22:40:40.412 DEBUG 5544 --- [nio-8080-exec-3] c.x.r.p.m.P.selectByBlogUserId : ==> Parameters: 100(Integer), 101(Integer)
2020-10-15 22:40:40.456 DEBUG 5544 --- [nio-8080-exec-3] c.x.r.p.m.P.selectByBlogUserId : <== Total: 1
44
2020-10-15 22:40:40.456 DEBUG 5544 --- [nio-8080-exec-3] o.s.j.d.DataSourceTransactionManager : Initiating transaction commit
2020-10-15 22:40:40.457 DEBUG 5544 --- [nio-8080-exec-3] o.s.j.d.DataSourceTransactionManager : Committing JDBC transaction on Connection [HikariProxyConnection@219078864 wrapping com.mysql.cj.jdbc.ConnectionImpl@1027ecc0]
2020-10-15 22:40:40.543 DEBUG 5544 --- [nio-8080-exec-3] o.s.j.d.DataSourceTransactionManager : Releasing JDBC Connection [HikariProxyConnection@219078864 wrapping com.mysql.cj.jdbc.ConnectionImpl@1027ecc0] after transaction
218
2020-10-15 22:40:40.543 DEBUG 5544 --- [nio-8080-exec-3] m.m.a.RequestResponseBodyMethodProcessor : Using 'application/json', given [*/*] and supported [application/json, application/*+json, application/json, application/*+json]
2020-10-15 22:40:40.543 DEBUG 5544 --- [nio-8080-exec-3] m.m.a.RequestResponseBodyMethodProcessor : Writing [ApiResult(code=1, msg=ok)]
2020-10-15 22:40:40.544 DEBUG 5544 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet : Completed 200 OK
不开事务(没有@Transactional注解)
2020-10-15 22:44:02.862 DEBUG 11096 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet : POST "/praise", parameters={}
2020-10-15 22:44:02.862 DEBUG 11096 --- [nio-8080-exec-3] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.xiaogang.redissondemo.praise.controller.PraiseController#add(PraiseAddParam)
2020-10-15 22:44:02.863 DEBUG 11096 --- [nio-8080-exec-3] m.m.a.RequestResponseBodyMethodProcessor : Read "application/json;charset=UTF-8" to [PraiseAddParam(blogId=100, userId=101)]
2020-10-15 22:44:02.863 DEBUG 11096 --- [nio-8080-exec-3] o.s.jdbc.datasource.DataSourceUtils : Fetching JDBC Connection from DataSource
2020-10-15 22:44:02.905 DEBUG 11096 --- [nio-8080-exec-3] c.x.r.p.m.P.selectByBlogUserId : ==> Preparing: select * from praise where blog_id =? and user_id = ? and status = 1 and is_active = 1
2020-10-15 22:44:02.905 DEBUG 11096 --- [nio-8080-exec-3] c.x.r.p.m.P.selectByBlogUserId : ==> Parameters: 100(Integer), 101(Integer)
2020-10-15 22:44:02.948 DEBUG 11096 --- [nio-8080-exec-3] c.x.r.p.m.P.selectByBlogUserId : <== Total: 1
85
85
2020-10-15 22:44:02.948 DEBUG 11096 --- [nio-8080-exec-3] m.m.a.RequestResponseBodyMethodProcessor : Using 'application/json', given [*/*] and supported [application/json, application/*+json, application/json, application/*+json]
2020-10-15 22:44:02.948 DEBUG 11096 --- [nio-8080-exec-3] m.m.a.RequestResponseBodyMethodProcessor : Writing [ApiResult(code=1, msg=ok)]
2020-10-15 22:44:02.949 DEBUG 11096 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet : Completed 200 OK
可以看到事务耗费了很多时间,如果业务方法不需要事务,不要加上@Transactional注解
总结:就一个简单的查询,加上@Transactional注解之后,整个接口执行时间大概是200ms,但是不要@Transactional注解,执行时间是90ms,如果高频访问,时间稳定在47ms。
所以,如果业务只是查询,不需要事务,不要使用事务,就算使用,也使用合理的事务和隔离级别,对系统的性能提升非常有帮助
版权声明:本文为u013958151原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。