Spring-Boot应用程序 - @Transactional不起作用[重复]

时间:2021-11-05 20:41:26

This question already has an answer here:

这个问题在这里已有答案:

I've got a problem with my Spring-Boot Application. I'm trying to send a notification after i get a result from my function with @Transactional, but @Transactional doesn't work, the rollback doesn't work. This is the code.

我的Spring-Boot应用程序出了问题。我正在尝试使用@Transactional从我的函数获得结果后发送通知,但@Transactional不起作用,回滚不起作用。这是代码。

@Service
public class ParkingServiceImpl implements ParkingService {

   @Override
   public void getParkingFromBOAsyncWrapper() {
         ArrayList<BOUserParking> listUserParkingfromBO = getParkingFromBOTransaction();

        /** Send notification **/
        if(listUserParkingfromBO!=null){
             notificationService.sendNotification(listUserParkingfromBO);
        }
   }


   @Transactional
   @Override
   public ArrayList<BOUserParking> getParkingFromBOTransaction() {
              // .... insert or update on db
   }
}

2 个解决方案

#1


1  

Transactional will not work if called within the same class. Transactional (and similar e.g. Async) works via creating proxies between the calling classes. You must call it from another class to get the transactional functionality.

如果在同一个类中调用,则Transactional将不起作用。事务性(和类似的例如Async)通过在调用类之间创建代理来工作。您必须从另一个类调用它才能获得事务功能。

I would move your DAO calls to a RespositoryService that handles the transactionallity.

我会将您的DAO调用移动到处理事务性的RespositoryService。

#2


0  

Put @Transactional annotation inside DAO class method, not in class.

将@Transactional注释放在DAO类方法中,而不是在类中。

#1


1  

Transactional will not work if called within the same class. Transactional (and similar e.g. Async) works via creating proxies between the calling classes. You must call it from another class to get the transactional functionality.

如果在同一个类中调用,则Transactional将不起作用。事务性(和类似的例如Async)通过在调用类之间创建代理来工作。您必须从另一个类调用它才能获得事务功能。

I would move your DAO calls to a RespositoryService that handles the transactionallity.

我会将您的DAO调用移动到处理事务性的RespositoryService。

#2


0  

Put @Transactional annotation inside DAO class method, not in class.

将@Transactional注释放在DAO类方法中,而不是在类中。