spring手动控制事务开启_Spring+Mybatis 手动控制事务

@Override

publicbooleantestDelete(StringjobCode)throwsException {

booleanflag=false;

//1.获取事务控制管理器

DataSourceTransactionManagertransactionManager= HqznContext.getBean(

"transactionManager", DataSourceTransactionManager.class);

//2.获取事务定义

DefaultTransactionDefinitiondef=newDefaultTransactionDefinition();

//3.设置事务隔离级别,开启新事务

def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);

//4.获得事务状态

TransactionStatusstatus=transactionManager.getTransaction(def);

try{

//5.具体的数据库操作(多个)

BOBaseJobr=newBOBaseJob();

r.setJobCode("SW001");

r.setJobName("事务001");

BOBaseJobMapper.deleteByPrimaryKey(jobCode);

BOBaseJobMapper.insert(r);

flag=true;

transactionManager.commit(status);

}catch(Exceptione) {

transactionManager.rollback(status);

}

returnflag;

}

结合日志分析:

(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)] - 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]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值