简体   繁体   中英

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

@Override
@Transactional
public void saveUser(UserDto userDto) throws Exception {
    userDao.saveUser(userDto);
    Queue queue = new ActiveMQQueue("users");
    messageSender.sendUserMessage(queue, userDto);
}

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

@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); ** should not called. but when I passed duplicate email id , UserDaoImpl throws error but before that my next step start execution.

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.

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).

So you have to force a flush manually (entityManager.flush()) if you want you insert to be executed sooner.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM