我使用Spring @Transactional在一个函数中插入多个表。
对于每个实体的读/写,我使用EntityManager,假设在我的函数中,10个表正在使用数据进行更新,那么在这种情况下,所有10个表都被锁定,直到事务没有结束,这是用户体验方面的坏消息,因为它会导致用户等待\延迟,他们正在查看使用这些表的少数页面。
那么,在整个插入表过程中,我如何才能防止对表的读操作进行锁定呢?有没有一种方法可以避免使用事务,并进行单表独立插入?
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_UNCOMMITTED, noRollbackFor = {SQLException.class, IllegalStateException.class, PersistenceException.class})
public BaseImportDtoRoot importData(BaseImportDtoRoot baseDto) throws Exception {
try{
table1.fninsert(); .. call each class to insert entity wise
table2.fninsert();
}
catch(Exception e){
}
}
public class table1(){
fninsert(){
MstTable tb1= new MstTable ();
tb1= modMap.map(MstTableDto,
MstTable.class);
entityManager.persist(tb1);
entityManager.flush();
entityManager.clear();
}
1条答案
按热度按时间wd2eg0qa1#
是否有办法避免使用事务
您不能这样做,因为每当您保存数据时都会建立交易,因此您需要:
1.使用
@Transactional
,它将DML Package 在事务内1.手动创建事务处理
().开始();(数据);().提交();
并执行单表独立插入
我认为你的意思是为每个插入创建单独的事务。你可以通过创建多个方法来实现,这些方法带有
@Transactional
注解,并删除importData
方法上的@Transaction
注解。(这样做的话,importData
方法就不再是原子的了)如果我有什么误解的地方请纠正我。