首页 > 解决方案 > 休眠集成器+弹簧引导中没有发生回滚

问题描述

我们已经实现了休眠集成器,带有插入后和更新后事件监听器。能够成功拦截。在保存用事务注释包装的实体 A 时,事件侦听器被拦截,我们正在保存实体 B。到目前为止一切都很顺利。

但是现在,如果在第一个事务块上引发运行时异常,则实体 A 成功回滚,并且在数据库中看到实体 B 条目。没有实体 A,实体 B 就无法存在。

公共类 EntityEventListener 实现 PostInsertEventListener、PostUpdateEventListener {

  Logger logger = LogManager.getLogger(EntityEventListener.class);

private static AppService appService;


@Autowired
public void setYourServiceService(AppService transactionMetaDataService) {
    EntityEventListener.appService = appService;
}

/**
 * To handle any operations to be done post insert.
 */
@Override
public void onPostInsert(PostInsertEvent event) {

    if(event.getEntity() instanceof EntityA)
    {
      try {
            //Which is annotated with @Transactional(TxType.REQUIRES_NEW)
          appService.create(event.getEntity());           }
        catch (Exception e) {
                      }
    }
}

/**
 * To handle any operations to be done post update.
 */
@Override
public void onPostUpdate(PostUpdateEvent event) {

    if(event.getEntity() instanceof WalletTransaction)
    {
      logger.info("calling update .....");
  try {
        //Which is annotated with @Transactional(TxType.REQUIRES_NEW)   
      appService.update(event.getEntity());       } 
    catch (Exception e) {
              }

    }
}

@Override
public boolean requiresPostCommitHanding(EntityPersister persister) {
    return false;
} }

标签: spring-boothibernatetransactionsevent-handlinginterceptor

解决方案


推荐阅读