jpa @Transactional + ElasticSearchEventListener (PostInsertEventListener…)

匿名 (未验证) 提交于 2019-12-03 01:45:01

问题:

I am having a problem related to JPA & some hibernate listeners I configured to index/deindex the Db entities into Elastic Search. The problem is basically that the listener onPostInsert method is called even if I throw an exception in the method where I am persisting an entity and this method is marked as @Transactional(rollbackFor = {Throwable.class}). My configuration is as follows.

The listener class:

public class ElasticSearchEventListener implements PostDeleteEventListener,     PostInsertEventListener, PostUpdateEventListener {      @Override     public void onPostInsert(PostInsertEvent event) {         log.debug("Listener indexing entity");         try {            updateElasticSearch(event.getEntity());         } catch (Exception e) {            log.debug("Error indexing object from listener");            e.printStackTrace();         }     }      ....... } 

The listener configured class:

@Service @Log4j public class ListenerConfigurerImpl implements ListenerConfigurer {     @Autowired     private EntityManagerFactory entityManagerFactory;      @Autowired     private ElasticSearchEventListener listener;      @PostConstruct @Override     public void registerListeners() {         log.debug("Registering event listeners");         HibernateEntityManagerFactory hibernateEntityManagerFactory = (HibernateEntityManagerFactory) this.entityManagerFactory;         SessionFactoryImpl sessionFactoryImpl = (SessionFactoryImpl) hibernateEntityManagerFactory.getSessionFactory();         EventListenerRegistry registry = sessionFactoryImpl.getServiceRegistry().getService(EventListenerRegistry.class);         registry.getEventListenerGroup(EventType.POST_COMMIT_INSERT).appendListener(listener);          .......     } } 

A service class:

@Service @Log4j public class ConversationServiceImpl implements ConversationService {      @Override             @Transactional(rollbackFor = {Throwable.class})     public void quotePackage(Long userId, CustomQuoteDTO dto) {         ......         Conversation conversation = Conversation.createAndAssign(user, agency, type, subject);         conversation = conversationRepository.save(conversation);         Long conversationId = conversation.getId();          if (1 == 1) throw new RuntimeException();     } } 

Based on this configuration, I would be expecting that the conversation entity is not saved neither in the DB nor Elastic Search. The entity is not persisted in the DB which is correct but for some reason the "onPostInsert" is still executing... and I get the entity in Elastic Search even if it is not in the Database.

Any ideas? I am a bit lost. Thanks in advance.

EDIT 1 ------

I have found this bug from 2006 and it is still open that seems to be my problem: https://hibernate.atlassian.net/browse/HHH-1582

Is this supposed to work this way?

回答1:

The pull request added here https://hibernate.atlassian.net/browse/HHH-1582 fixes this issue.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!