Entitymanager.flush() VS EntityManager.getTransaction().commit - What should I prefer?

前端 未结 5 678
我在风中等你
我在风中等你 2020-12-08 00:37

What should I prefer when updating the database? What are the pros & cons with either method and when shall I use the one or the other?

public void disem         


        
5条回答
  •  独厮守ぢ
    2020-12-08 00:59

    I would go for container managed transaction whenever possible. Bean managed transactions usually require significantly more code, because of the Exception possibilities. Also, it's more error prone (rollbacks, resource management).

    That said, I would use a flush after the commit in container managed mode. That way I can catch possible PersistenceExceptions in my storage module and convert it to some more meaningful Exception for my use case module. This because I don't want to handle storage-specific Exceptions here, because I might swap the storage module out for something that does not use JPA... which never happened to me :)

提交回复
热议问题