我有一個與JPA &相關的問題,我配置了一些hibernate偵聽器,用於將Db實體索引/取消索引到Elastic Search中。問題基本上是,即使我在持久化實體的方法中引發異常,並且此方法標記爲@Transactional(rollbackFor = {Throwable.class}),onPostInsert方法的偵聽器也會被調用。我的配置如下。jpa @Transactional + ElasticSearchEventListener(PostInsertEventListener ...)
的監聽器類:
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 ------
我發現這個bug從2006年,它仍然是開放的,這似乎是我的問題:https://hibernate.atlassian.net/browse/HHH-1582
難道這這種方式工作?