persistence@glassfish.java.net

Re: Problems with optimistic concurrency?

From: Adam Bien <abien_at_adam-bien.com>
Date: Mon, 16 Jun 2008 16:25:19 +0200

Hi James,

James Sutherland schrieb:
> That is odd. Could you please include the full stack trace to the exception.
> Also what about Timestamp locking does not work?
I'm not able to create a new entity with Timestamp, or any of the
wrappers, Long, Integer, Short using em.merge.
> It should work, as long as
> you use a valid timestamp type in the database that stores milliseconds
> correctly (what database and type are you using?).
>
Oracle Database 10g Express. The problem is actually not the optimistic
lock itself (=collisions), rather than the creation of new entities
using EntityManager#merge.
I'm not able to create a new Entity with any of the wrapper types.


> Is the object you are merging new or existing?
>
I only tested new one. There is only a problem with the wrapper types,
not the primitives. The workaround for the primitives is the initialization.

regards && thanks,

adam

>
>
> Adam Bien wrote:
>
>> Hi All,
>>
>> I'm testing now the optimistic concurrency behavior. All primitive
>> datatypes seems to work fine:
>>
>> @Entity
>> public class Entity{
>>
>> @Version
>> private int version;
>>
>> }
>>
>> @Entity
>> public class Entity{
>>
>> @Version
>> private long version;
>>
>> }
>>
>> etc.
>>
>>
>> However there is a problem with Long, Integer, Short, and bigger one
>> with Timestamp.
>>
>> It seems to be necessary to initialize the Wrappers, otherwise I get an
>> exception:
>>
>> "nested exception is: java.rmi.ServerException: RemoteException occurred
>> in server thread; nested exception is: java.rmi.RemoteException: null;
>> nested exception is: javax.persistence.OptimisticLockException:
>> Exception [TOPLINK-5010] (Oracle TopLink Essentials - 2.0.1 (Build
>> b03-fcs (04/05/2008))):
>> oracle.toplink.essentials.exceptions.OptimisticLockException Exception
>> Description: The object [Entity_at_1fc2f8c] cannot be merged because it has
>> changed or been deleted since it was last read. {3}Class> Entity"
>>
>>
>> already at the creation time.
>>
>> The workaround is simple - you have just to initialize the Wrapper at
>> creation time:
>>
>> @Entity
>> public class Entity{
>>
>> @Id
>> @GeneratedValue
>> private int id;
>> @Version
>> private Long version = new Long(0);
>>
>> }
>>
>>
>> The intiialization shouldn't be necessary: ...= new Long(0);
>>
>> With Timestamp it seems not to work in general...
>>
>> I'm creating the objects with em#merge and not em#persist,
>>
>> regards,
>>
>> adam
>>
>>
>
>
> -----
> ---
> http://wiki.eclipse.org/User:James.sutherland.oracle.com James Sutherland
> http://www.eclipse.org/eclipselink/
> EclipseLink , http://www.oracle.com/technology/products/ias/toplink/
> TopLink
> Wiki: http://wiki.eclipse.org/EclipseLink EclipseLink ,
> http://wiki.oracle.com/page/TopLink TopLink
> Forums: http://forums.oracle.com/forums/forum.jspa?forumID=48 TopLink ,
> http://www.nabble.com/EclipseLink-f26430.html EclipseLink
> Book: http://en.wikibooks.org/wiki/Java_Persistence Java Persistence
>


-- 
 Consultant, Author, Java Champion
 
 Homepage: www.adam-bien.com
 Weblog: blog.adam-bien.com
 eMail:  abien_at_adam-bien.com
 Mobile: 0049(0)170 280 3144
 Books: Enterprise Architekturen (ISBN: 393504299X),
        Java EE 5 Architekturen  (ISBN: 3939084247),
        J2EE Patterns, J2EE Hotspots, Enterprise Frameworks and Struts