我不知道如何表达标题,但我使用的是Spring boot,我有一个函数,如下所示:
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_UNCOMMITTED)
public Optional<List<SomeClass>> bulkInsert(List<SomeClass> models, boolean withRefresh) {
int rowsInserted = someDao.bulkInsert(models).length;
if (rowsInserted > 0 && withRefresh) {
// I don't want the getLatest() function to operate in a transaction
return Optional.of(getLatest(someValue, models.size()));
}
return Optional.empty();
};
@Transactional(propagation = NOT_SUPPORTED)
public List<SomeClass> getLatest(long someValue, int limit) {
TransactionStatus status = TransactionAspectSupport.currentTransactionStatus(); ### says the transaction is active
return someDao.getLatest(accountId, limit);
};基本上,我希望从事务中排除getLatest()。有可能吗?
发布于 2020-06-27 03:53:13
改用下面的代码:
@Transactional(propagation = NOT_SUPPORTED)
public List<SomeClass> getLatest(long someValue, int limit) {有关传播级别的说明,请参阅Spring Propagation Doc。
发布于 2020-06-27 04:13:22
如果您只想将事务用于
someDao.bulkInsert(models).length;然后,您应该将此代码与事务性方面包装在一起,并单独调用其他部分。如下所示:
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_UNCOMMITTED)
public int bulkInsert(List<SomeClass> models, boolean withRefresh) {
return someDao.bulkInsert(models).length;
}
public Optional<List<SomeClass>> abc(int rowInserted, boolean withRefresh){
if (rowsInserted > 0 && withRefresh) {
return Optional.of(getLatest(someValue, models.size())); ### I don't want the getLatest() function to operate in a transaction
}
return Optional.empty();
}
public List<SomeClass> getLatest(long someValue, int limit) {
TransactionStatus status = TransactionAspectSupport.currentTransactionStatus(); ### says the transaction is active
return someDao.getLatest(accountId, limit);
};最重要的是,您需要从此实例的外部调用此方法。
对于标题中的问题:不,您不能将部分代码带到事务之外,因为spring-aop使用代理,并且整个方法都与代理实例中的事务方面包装在一起。
https://stackoverflow.com/questions/62601227
复制相似问题