Spring Boot 学习 @Transactional注解 事务生效了吗?一种查看方式(打印log)

版权声明:The beautiful thing about learning is nobody can take it away from you. https://blog.csdn.net/xiuye2015/article/details/81058201

1.首先,找到spring boot的默认的事务管理器,一般继承至于

org.springframework.transaction.PlatformTransactionManager

2.设置事务管理器打印log,应该包含开始事务,结束事务的输出日志.

eg.我的工程包含JPA,MyBatis,在Service层编写异常,测试事务是否回滚:

@Transactional
	@Override
	public void insertException(Hello h) {
		this.helloMapper.insert(h);
		throw new RuntimeException("test rollback.");
	}

(1).找到spring boot默认的事务管理器.

@Resource
	private PlatformTransactionManager ptm;
@PostConstruct
	public void init() {
		
		LogUtil.log("tm="+ptm);
	}
	
tm=org.springframework.orm.jpa.JpaTransactionManager@2cce10bc

我找到了:

org.springframework.orm.jpa.JpaTransactionManager

(2).spring boot 可以设置logging.level.PackageName=true输出"该包"日志级别:

logging.level.com.xiuye.dao.mapper=debug # able to output sql 
logging.level.org.springframework.orm.jpa=debug # able to output tranaction info 

我看到了:[1]Creating new transaction...[2]Rolling back JPA transaction...

[INFO]	Started HelloServiceTests in 11.815 seconds (JVM running for 13.568) 
	org.springframework.boot.StartupInfoLogger:59 
	[main] 2018-07-15 22:46:47,482 
	pid:6408 
[DEBUG]	Creating new transaction with name [com.xiuye.service.impl.HelloServiceImpl.insertException]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; '' 
	org.springframework.transaction.support.AbstractPlatformTransactionManager:372 
	[main] 2018-07-15 22:46:47,698 
	pid:6408 
[DEBUG]	Opened new EntityManager [SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=ExecutableList{size=0} updates=ExecutableList{size=0} deletions=ExecutableList{size=0} orphanRemovals=ExecutableList{size=0} collectionCreations=ExecutableList{size=0} collectionRemovals=ExecutableList{size=0} collectionUpdates=ExecutableList{size=0} collectionQueuedOps=ExecutableList{size=0} unresolvedInsertDependencies=null])] for JPA transaction 
	org.springframework.orm.jpa.JpaTransactionManager:391 
	[main] 2018-07-15 22:46:47,700 
	pid:6408 
[DEBUG]	Exposing JPA transaction as JDBC transaction [org.springframework.orm.jpa.vendor.HibernateJpaDialect$HibernateConnectionHandle@5d32f5db] 
	org.springframework.orm.jpa.JpaTransactionManager:423 
	[main] 2018-07-15 22:46:47,716 
	pid:6408 
[DEBUG]	==>  Preparing: insert into hello(id,msg) values(null,?)  
	org.apache.ibatis.logging.jdbc.BaseJdbcLogger:159 
	[main] 2018-07-15 22:46:47,765 
	pid:6408 
[DEBUG]	==> Parameters: Rollback not successful! 23(String) 
	org.apache.ibatis.logging.jdbc.BaseJdbcLogger:159 
	[main] 2018-07-15 22:46:47,830 
	pid:6408 
[DEBUG]	<==    Updates: 1 
	org.apache.ibatis.logging.jdbc.BaseJdbcLogger:159 
	[main] 2018-07-15 22:46:47,907 
	pid:6408 
[DEBUG]	Initiating transaction rollback 
	org.springframework.transaction.support.AbstractPlatformTransactionManager:836 
	[main] 2018-07-15 22:46:47,909 
	pid:6408 
[DEBUG]	Rolling back JPA transaction on EntityManager [SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=ExecutableList{size=0} updates=ExecutableList{size=0} deletions=ExecutableList{size=0} orphanRemovals=ExecutableList{size=0} collectionCreations=ExecutableList{size=0} collectionRemovals=ExecutableList{size=0} collectionUpdates=ExecutableList{size=0} collectionQueuedOps=ExecutableList{size=0} unresolvedInsertDependencies=null])] 
	org.springframework.orm.jpa.JpaTransactionManager:557 
	[main] 2018-07-15 22:46:47,910 
	pid:6408 
[DEBUG]	Closing JPA EntityManager [SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=ExecutableList{size=0} updates=ExecutableList{size=0} deletions=ExecutableList{size=0} orphanRemovals=ExecutableList{size=0} collectionCreations=ExecutableList{size=0} collectionRemovals=ExecutableList{size=0} collectionUpdates=ExecutableList{size=0} collectionQueuedOps=ExecutableList{size=0} unresolvedInsertDependencies=null])] after transaction 
	org.springframework.orm.jpa.JpaTransactionManager:622 
	[main] 2018-07-15 22:46:47,915 
	pid:6408 
[DEBUG]	Closing JPA EntityManager 
	org.springframework.orm.jpa.EntityManagerFactoryUtils:418 
	[main] 2018-07-15 22:46:47,916 
	pid:6408 
事务生效了,而且查看数据库,那条数据没有成功insert.



阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页