spring事务设置中传播属性的实现
这几类属性的实现,主要需要如下几个方法: 1. 判断当前是否在一个事务中;isTransactionExist 2. 把方法加入当前事务中; putInCurTransaction 3. 挂起原有方法,创建新的事务; suspendCurAndNewTra 4. 事务嵌套,内部回滚; nestTransaction isTransactionExist(){ 当前线程启动事务时记录当前事务; 所以本线程可以知道自己是否在一个事务中; } putInCurTransaction{ 什么也不做,直接在当前连接下运行函数; } suspendCurAndNewTra{ 新建一个连接,SET autocommit=0;自然就使用了新的事务; } nestTransaction{ SAVEPOINT `SAVEPOINT_1`; 执行函数; 成功 commit ;失败则 ROLLBACK TO SAVEPOINT `SAVEPOINT_1`; } 有了这几个函数, 则上述7类传播属性都可以实现: REQUIRED{ isTransactionExist(){ putInCurTransaction() }else{ SET autocommit=1; 执行函数; commit; } } SUPPORTS{ putInCurTransaction(); } MANDATORY{ isTransactionExist(){ putInCurTransaction(); }else{ throw Exception(); } } REQUIRES_NEW{ con2=suspendCurAndNewTra(); con2.commit(); } NOT_SUPPORTED{ 建立连接, SET autocommit=0;执行函数; } NEVER{ isTransactionExist(){ throw Exception(); }else{ 建立连接, SET autocommit=0;执行函数; } } NESTED{ isTransactionExist(){ nestTransaction(); }else{ REQUIRED(); } } |
|