persistence@glassfish.java.net

Problems with optimistic concurrency?

From: Adam Bien <abien_at_adam-bien.com>
Date: Sat, 14 Jun 2008 14:49:58 +0200

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

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