在objectoptimisticlockingfailureexception上的@transactional method中插入未回滚

1aaf6o9v  于 2021-06-25  发布在  Mysql
关注(0)|答案(0)|浏览(261)
@Service
public class TransferService {

    @Autowired
    TransferRepository transferRepository;
    @Autowired
    AccountRepository accountRepository;

    @Transactional(REQUIRES_NEW)
    public void transfer(Account fromAccount, Account toAccount, 
        BigDecimal amount) throws InterruptedException {

    fromAccount.setBalance(fromAccount.getBalance().subtract(amount));
    accountRepository.save(fromAccount);

    if(fromAccount.getId() == 1L){
        Thread.currentThread().sleep(10000);
    }

    toAccount.setBalance(toAccount.getBalance().add(amount));
    accountRepository.save(toAccount);

    transferRepository.save(new Transfer(fromAccount, toAccount,amount));
    }
}

db表中的初始状态 Account :

id | balance | version    
1  | 100     | 0    
2  | 100     | 0
``` `Transfer` 表为空。
当我打电话时:
  1. transfer(account1, account2, 10);
  2. transfer(account2, account1, 20);
数据库表 `Account` : 

id | balance | version
1 | 120 | 0
2 | 80 | 0

数据库表 `Transfer` : 

id | ammount | fromAccountId | toAccountId
1 | 20 | 2 | 1
2 | 10 | 1 | 2

日志清楚地表明它将回滚: `Transfer#2` ,  `Account#2` 以及 `Account#1` ,但它不只是回滚帐户,而是存在id为2的转账,但应该回滚。
非常感谢您的帮助!!!
日志:
2018-01-14 12:14:27.652调试16006---[nio-8080-exec-7]o.s.orm.jpa.jpatransactionmanager在entitymanager[sessionimpl(persistencecontext[entitykeys=[entitykey[com.erabanq.entity.transfer#2]、entitykey[com.erabanq.entity.account#2]、entitykey[com.erabanq.entity.account#1]、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}unsolvedInsertDependencies=null])]
2018-01-14 12:14:27.657错误16006---[nio-8080-exec-7]o.h.i.ExceptionApperstandardImpl:hh000346:托管刷新期间出错[批更新从更新[0]返回意外的行计数];实际行数:0;期望值:1]
2018-01-14 12:14:27.658 info 16006---[nio-8080-exec-7]o.h.e.j.b.internal.abstractbatchimpl:hhh000010:批处理发布时仍包含jdbc语句
2018-01-14 12:14:27.678调试16006---[nio-8080-exec-7]o.s.orm.jpa.jpatransactionmanager:提交异常后启动事务回滚
org.springframework.orm.objectoptimisticlockingfailureexception:批更新从更新[0]返回意外的行计数;实际行数:0;期望值:1;嵌套异常为org.hibernate.stalestateexception:批更新从更新[0]返回意外的行计数;实际行数:0;期望值:1
这里是数据库日志:

START...............................
2018-01-14T15:48:03.913784Z 8 Query set session transaction read only
2018-01-14T15:48:03.913963Z 8 Query SET autocommit=0
2018-01-14T15:48:03.921491Z 8 Query select account0_.id as id1_0_0_, account0_.balance as balance2_0_0_, account0_.version as version3_0_0_ from account account0_ where account0_.id=1
2018-01-14T15:48:03.930767Z 8 Query commit
2018-01-14T15:48:03.930929Z 8 Query SET autocommit=1
2018-01-14T15:48:03.931281Z 8 Query select @@session.tx_read_only
2018-01-14T15:48:03.931607Z 8 Query set session transaction read write
2018-01-14T15:48:03.932052Z 8 Query set session transaction read only
2018-01-14T15:48:03.932173Z 8 Query SET autocommit=0
2018-01-14T15:48:03.932750Z 8 Query select account0_.id as id1_0_0_, account0_.balance as balance2_0_0_, account0_.version as version3_0_0_ from account account0_ where account0_.id=2
2018-01-14T15:48:03.933613Z 8 Query commit
2018-01-14T15:48:03.933744Z 8 Query SET autocommit=1
2018-01-14T15:48:03.933968Z 8 Query select @@session.tx_read_only
2018-01-14T15:48:03.934134Z 8 Query set session transaction read write
2018-01-14T15:48:03.937186Z 8 Query SET autocommit=0
2018-01-14T15:48:11.995802Z 9 Query set session transaction read only
2018-01-14T15:48:11.996386Z 9 Query SET autocommit=0
2018-01-14T15:48:11.999184Z 9 Query select account0_.id as id1_0_0_, account0_.balance as balance2_0_0_, account0_.version as version3_0_0_ from account account0_ where account0_.id=2
2018-01-14T15:48:12.002778Z 9 Query commit
2018-01-14T15:48:12.003217Z 9 Query SET autocommit=1
2018-01-14T15:48:12.003848Z 9 Query select @@session.tx_read_only
2018-01-14T15:48:12.004822Z 9 Query set session transaction read write
2018-01-14T15:48:12.006758Z 9 Query set session transaction read only
2018-01-14T15:48:12.008320Z 9 Query SET autocommit=0
2018-01-14T15:48:12.012266Z 9 Query select account0_.id as id1_0_0_, account0_.balance as balance2_0_0_, account0_.version as version3_0_0_ from account account0_ where account0_.id=1
2018-01-14T15:48:12.015808Z 9 Query commit
2018-01-14T15:48:12.016214Z 9 Query SET autocommit=1
2018-01-14T15:48:12.016792Z 9 Query select @@session.tx_read_only
2018-01-14T15:48:12.017278Z 9 Query set session transaction read write
2018-01-14T15:48:12.018332Z 9 Query SET autocommit=0
2018-01-14T15:48:12.078209Z 9 Query insert into transfer (ammount, from_id, to_id, type) values (15, 2, 1, null)
2018-01-14T15:48:12.082738Z 9 Query update account set balance=85.00, version=1 where id=2 and version=0
2018-01-14T15:48:12.084494Z 9 Query update account set balance=115.00, version=1 where id=1 and version=0
2018-01-14T15:48:12.084946Z 9 Query commit
2018-01-14T15:48:12.085086Z 9 Query SET autocommit=1
2018-01-14T15:48:13.952199Z 8 Query insert into transfer (ammount, from_id, to_id, type) values (10, 1, 2, null)
2018-01-14T15:48:13.954992Z 8 Query update account set balance=90.00, version=1 where id=1 and version=0
2018-01-14T15:48:13.956614Z 8 Query rollback
2018-01-14T15:48:13.957371Z 8 Query SET autocommit=1

我使用的是spring数据jpa2.1,hibernate-core-5.2.12.final.jar

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题