jpa @Transactional + ElasticSearchEventListener(PostInsertEventListener ...)

时间:2014-01-14 04:29:15

标签: java spring hibernate jpa elasticsearch

我遇到了与JPA& amp;我配置的一些hibernate监听器将Db实体索引/解索引为弹性搜索。问题基本上是调用监听器onPostInsert方法,即使我在持久化实体的方法中抛出异常,并且此方法被标记为@Transactional(rollbackFor = {Throwable.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();
        }
    }

    .......
}

侦听器已配置类:

@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);

        .......
    }
}

服务类:

@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();
    }
}

基于这种配置,我希望对话实体既不会保存在数据库中,也不会保存在弹性搜索中。实体不会保留在DB中,这是正确的但由于某种原因“onPostInsert”仍在执行...我在Elastic Search中获取实体,即使它不在数据库中。

有什么想法吗?我有点迷茫。 提前谢谢。

编辑1 ------

我从2006年发现了这个错误,它仍然是开放的,似乎是我的问题:https://hibernate.atlassian.net/browse/HHH-1582

这应该是这样的吗?

1 个答案:

答案 0 :(得分:0)

此处添加的提取请求https://hibernate.atlassian.net/browse/HHH-1582解决了此问题。