jpa @Transactional + ElasticSearchEventListener(PostInsertEventListener ...) [英] jpa @Transactional + ElasticSearchEventListener (PostInsertEventListener...)
问题描述
听众类:
public class ElasticSearchEventListener实现PostDeleteEventListener,
PostInsertEventListener,PostUpdateEventListener {
@Override
public void onPostInsert(PostInsertEvent event){
log.debug 听众索引实体);
try {
updateElasticSearch(event.getEntity());
} catch(Exception e){
log.debug(从侦听器中索引对象的错误);
e.printStackTrace();
}
}
.......
}
侦听器配置的类:
@Service @ Log4j
public class ListenerConfigurerImpl implements ListenerConfigurer {
@Autowired
private EntityManagerFactory entityManagerFactory;
@Autowired
private ElasticSearchEventListener listener;
@PostConstruct @Override
public void registerListeners(){
log.debug(注册事件侦听器);
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实现ConversationService {
@Override
@Transactional(rollbackFor = {Throwable.class})
public void quotePackage(Long userId,CustomQuoteDTO dto){
... ...
对话对话= Conversation.createAndAssign(用户,代理,类型,主题);
conversation = conversationRepository.save(conversation);
Long conversationId = conversation.getId();
if(1 == 1)throw new RuntimeException();
}
}
根据这个配置,我会期待会话实体既不保存在数据库中也不保存弹性搜索。实体不会保留在DB中,这是正确的,但是由于某种原因,onPostInsert仍在执行...,即使不在数据库中,我也可以获取弹性搜索中的实体。
任何想法?我有点迷失了
提前感谢。
编辑1 ------
我找到了bug从2006年开始,它似乎是我的问题: https://hibernate.atlassian。 net / browse / HHH-1582
这是否应该这样工作?
此处添加的拉动请求是 https://hibernate.atlassian.net / browse / HHH-1582 修复了这个问题。
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?
The pull request added here https://hibernate.atlassian.net/browse/HHH-1582 fixes this issue.
这篇关于jpa @Transactional + ElasticSearchEventListener(PostInsertEventListener ...)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!