[英]Hibernate persist method throws exception after method execute next step
I am getting some strange results with hibernate persist method. 我用休眠持久方法得到了一些奇怪的结果。 Here is the explanation.
这是解释。
1) UserServiceImpl 1)UserServiceImpl
@Override
@Transactional
public void saveUser(UserDto userDto) throws Exception {
userDao.saveUser(userDto);
Queue queue = new ActiveMQQueue("users");
messageSender.sendUserMessage(queue, userDto);
}
2) UserDaoImpl 2)UserDaoImpl
@Override
public void saveUser(UserDto userDto) throws Exception {
PCLog.addInfoEntry("UserDaoImpl - SaveUser", "START");
getEntityManager().persist(userDto);
PCLog.addInfoEntry("UserDaoImpl - SaveUser", "END");
}
3) JSMListener 3)JSMListener
@JmsListener(destination="users")
public String processUser(UserDto userDto) throws JsonProcessingException {
PCLog.addInfoEntry("JMSMessageListener -> ProcesUSers", "USER START");
userElasticSearchDaoImpl.save(userDto);
return "ACK from handleMessage";
}
now when I pass duplicate email id than my userDao method should throw exception and than my next step ** messageSender.sendUserMessage(queue, userDto); 现在,当我传递重复的电子邮件ID时,我的userDao方法将引发异常,并且下一步将导致异常** messageSender.sendUserMessage(queue,userDto); ** should not called.
**不应该打电话。 but when I passed duplicate email id , UserDaoImpl throws error but before that my next step start execution.
但是当我传递重复的电子邮件ID时,UserDaoImpl引发错误,但在此之前我的下一步开始执行。
below is the log message which I used for debug 以下是我用于调试的日志消息
INFO com.vinayak.life.util.PCLog - INFO - UserDaoImpl - SaveUser START
Hibernate: select nextval ('t_user_master_c_um_npk_id_seq')
Hibernate: select nextval ('t_user_roles_c_ur_npk_role_id_seq')
Hibernate: select nextval ('t_user_roles_c_ur_npk_role_id_seq')
INFO com.vinayak.life.util.PCLog - INFO - UserDaoImpl - SaveUser END
INFO com.vinayak.life.util.PCLog - INFO - JMSMessageListener -> ProcesUSers : USER START
Hibernate: insert into t_user_master (c_um_lnm_access_profile_id, c_um_bnm_account_not_expired, c_um_bnm_is_locked, c_um_nnm_age, c_um_dnm_birth_date, c_um_dnm_created_date_time, c_um_vnm_email_address, ....
WARN org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 0, SQLState: 23505
ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - ERROR: duplicate key value violates unique constraint "t_user_master_c_um_vnm_username_key"
Detail: Key (c_um_vnm_username)=(test123) already exists.
from above log its clear that USER DAO START and USER DAO END statement execute first , than it called JMSMessageListener Process USER and after that INSERT Statement from UserDaoImpl execute and throws error. 从上面记录清楚,首先执行USER DAO START和USER DAO END语句,然后调用JMSMessageListener Process USER,然后执行UserDaoImpl的INSERT语句并引发错误。
Please suggest me what I am doing wrong. 请告诉我我在做什么错。
Thanks 谢谢
Inserts/Updates/deletes
are always executed
(committed) at the end of the transaction in hibernate, meaning Hibernate delays executing Inserts/Updates/deletes till the end of the transaction (that's the feature of Hibernate). Inserts/Updates/deletes
始终在休眠状态下在事务结束时executed
(提交),这意味着Hibernate将执行插入/更新/删除操作延迟到事务结束时(这是Hibernate的功能)。
So you have to force a flush manually (entityManager.flush())
if you want you insert
to be executed sooner. 因此,如果要更快地执行
insert
,则必须手动强制执行刷新(entityManager.flush())
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.