You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by Karsten Ohme <wi...@t-online.de> on 2008/04/17 01:22:54 UTC

How to handle EntityExistExceptions

Hi,

I would like to know how to handle EntityExistExceptions correctly.

I have a DAO layer, which creates entities with persist(). Now I want to 
know that the creation is really successful. The EntityExistsException 
may be thrown directly in the persist() method or later. So with bad 
luck the EntityExistsException is thrown at commit time and delegated to 
the user. Is there a good way how to prevent this? In upper service 
layers I could check the existence of the entity to create, but what 
about concurrent transactions which commit earlier?

Regards,
Karsten

Re: How to handle EntityExistExceptions

Posted by Karsten Ohme <wi...@t-online.de>.
Dain Sundstrom schrieb:
> On Apr 16, 2008, at 4:22 PM, Karsten Ohme wrote:
>> Hi,
>>
>> I would like to know how to handle EntityExistExceptions correctly.
>>
>> I have a DAO layer, which creates entities with persist(). Now I want 
>> to know that the creation is really successful. The 
>> EntityExistsException may be thrown directly in the persist() method 
>> or later. So with bad luck the EntityExistsException is thrown at 
>> commit time and delegated to the user. Is there a good way how to 
>> prevent this? In upper service layers I could check the existence of 
>> the entity to create, but what about concurrent transactions which 
>> commit earlier?
> 
> I believe that if you call flush, you will get the exception 
> immediately.  One way to avoid this exception is to use auto generated 
> primary keys.

Thanks. I will try the flush approach. Auto generated primary keys might 
be enough in some situations, but I also have unique fields, are they 
also checked with autogenerated fields?

Regards,
Karsten
> 
> -dain
> 
> 


Re: How to handle EntityExistExceptions

Posted by Dain Sundstrom <da...@iq80.com>.
On Apr 16, 2008, at 4:22 PM, Karsten Ohme wrote:
> Hi,
>
> I would like to know how to handle EntityExistExceptions correctly.
>
> I have a DAO layer, which creates entities with persist(). Now I  
> want to know that the creation is really successful. The  
> EntityExistsException may be thrown directly in the persist() method  
> or later. So with bad luck the EntityExistsException is thrown at  
> commit time and delegated to the user. Is there a good way how to  
> prevent this? In upper service layers I could check the existence of  
> the entity to create, but what about concurrent transactions which  
> commit earlier?

I believe that if you call flush, you will get the exception  
immediately.  One way to avoid this exception is to use auto generated  
primary keys.

-dain