-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: A design issue regarding to concurrency
PostPosted: Sun Mar 10, 2013 7:28 pm 
Newbie

Joined: Sun Mar 10, 2013 7:15 pm
Posts: 2
Hello everyone,

In my application, I have some persistency objects (entity instances) which are loaded into memory and I use same instances throughout the lifecycle of the application. These objects can be modified by different threads and they are persisted into to the database with a code as follows:

Code:
    public static <T> T updateEntity(T entity, long id) {
        EntityManager em = null;
        EntityTransaction tx = null;
        try {
            em = GenericPersistenceManager.emf.createEntityManager();
            tx = em.getTransaction();
            tx.begin();
            entity = em.merge(entity);
            tx.commit();
            return entity;
           
        } catch (RuntimeException e) {
            if (tx.isActive()) {
                tx.rollback();
            }
            throw e;
        } finally {
            em.close();
        }
    }


Sometimes when two threads try to update same object I get an exception as follows:

Code:
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '1-6' for key 'PRIMARY'


I guess I'm getting this exception because of a concurrency issue. Because if I make the function abovesynchronized or if I run the threads one by one, I don't get the exception. So, what should I consider in this case? How can I handle this concurrency issue. I'm not so experienced in Hibernate. So, I will really appreciate your helps.

Thanks in advance


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.