@Override
public boolean testDelete(String jobCode) throws Exception {
boolean flag = false;
//1.获取事务控制管理器
DataSourceTransactionManager transactionManager = HqznContext.getBean(
"transactionManager", DataSourceTransactionManager.class);
//2.获取事务定义
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
//3.设置事务隔离级别,开启新事务
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
//4.获得事务状态
TransactionStatus status = transactionManager.getTransaction(def);
try {
//5.具体的数据库操作(多个)
BOBaseJob r = new BOBaseJob();
r.setJobCode("SW001");
r.setJobName("事务001");
BOBaseJobMapper.deleteByPrimaryKey(jobCode);
BOBaseJobMapper.insert(r);
flag = true;
transactionManager.commit(status);
} catch (Exception e) {
transactionManager.rollback(status);
}
return flag;
}
结合日志分析:
(1)获取Mybatis下的sqlSession
DEBUG: [org.mybatis.spring.SqlSessionUtils.(28)debug] - Creating SqlSession with JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@64861c5d]
DEBUG: [java.sql.Connection.(28)debug] - ooo Connection Opened
(2)Spring控制事务
DEBUG: [org.mybatis.spring.transaction.SpringManagedTransaction.(28)debug] - JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@64861c5d] will be managed by Spring
(3)事务开启
DEBUG: [org.mybatis.spring.SqlSessionUtils.(28)debug] - Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@eed9a0e]
DEBUG: [java.sql.PreparedStatement.(28)debug] - ==> Executing: update BASE_JOB set FLAG_YN = 'N' where JOB_CODE = ?
DEBUG: [java.sql.PreparedStatement.(28)debug] - ==> Parameters: 10(String)
(4)释放SqlSession
DEBUG: [org.mybatis.spring.SqlSessionUtils.(28)debug] - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@eed9a0e]
(5)获取当前事务
DEBUG: [org.mybatis.spring.SqlSessionUtils.(28)debug] - Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@eed9a0e] from current transaction
DEBUG: [java.sql.PreparedStatement.(28)debug] - ==> Executing: insert into BASE_JOB (JOB_CODE, JOB_NAME, CREATE_PERSON, FLAG_YN, MODIFY_PERSON, CREATE_TIME, MODIFY_TIME) values (?, ?, ?, 'Y', ?, ?, ?)
DEBUG: [java.sql.PreparedStatement.(28)debug] - ==> Parameters: SW001(String), 事务001(String), null, null, null, null
异常信息:
DEBUG: [com.alibaba.druid.pool.PreparedStatementPool.(123)put] - {conn-10002, pstmt-20007} enter cache
INFO : [org.springframework.beans.factory.xml.XmlBeanDefinitionReader.(317)loadBeanDefinitions] - Loading XML bean definitions from class path resource [org/springframework/jdbc/support/sql-error-codes.xml]
INFO : [org.springframework.jdbc.support.SQLErrorCodesFactory.(126)<init>] - SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase, Hana]
DEBUG: [org.mybatis.spring.SqlSessionUtils.(28)debug] - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1d595930]
事务回滚:
### Error updating database. Cause: com.microsoft.sqlserver.jdbc.SQLServerException: 违反了 PRIMARY KEY 约束“PK_BASE_JOB”。不能在对象“dbo.BASE_JOB”中插入重复键。重复键值为 (SW001)。
### The error may involve com.hqzn.mybatis.dao.interfaces.BOBaseJobMapper.insert-Inline
### The error occurred while setting parameters
### Cause: com.microsoft.sqlserver.jdbc.SQLServerException: 违反了 PRIMARY KEY 约束“PK_BASE_JOB”。不能在对象“dbo.BASE_JOB”中插入重复键。重复键值为 (SW001)。
; SQL []; 违反了 PRIMARY KEY 约束“PK_BASE_JOB”。不能在对象“dbo.BASE_JOB”中插入重复键。重复键值为 (SW001)。; nested exception is com.microsoft.sqlserver.jdbc.SQLServerException: 违反了 PRIMARY KEY 约束“PK_BASE_JOB”。不能在对象“dbo.BASE_JOB”中插入重复键。重复键值为 (SW001)。
(6)释放SqlSession
DEBUG: [org.mybatis.spring.SqlSessionUtils.(28)debug] - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@eed9a0e]
DEBUG: [org.mybatis.spring.SqlSessionUtils.(28)debug] - Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@eed9a0e]