-->
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.  [ 2 posts ] 
Author Message
 Post subject: Concurrent entity modification
PostPosted: Sat Dec 20, 2014 10:51 am 
Newbie

Joined: Sat Dec 20, 2014 10:22 am
Posts: 2
Hi all,

I have a case when N threads can modify the same entity concurrently. The problem I've faced with is that entity is fully overwritten by each thread.
E.g. if 1st thread tries to modify username and 2nd - password, I expect to see both username and password updates. But last thread overwrites entire entity. Of course if 2 threads try to modify the same column, only latest changes should be applied. But in case of different modifications it would be nice to see merge effect.
So my question is: how to handle such situations and prevent entire entity overwriting?

Here's saving API:
Code:
public void save(final T entity) {
        try {
            createNewSessionAndTransaction();
            if (session != null && entity != null) {
                session.saveOrUpdate(entity);
            }
            commitTransaction();
        } catch (HibernateException e) {
            rollbackTransaction();
        }
    }


Hibernate config contains:
Code:
<property name="current_session_context_class">thread</property>


Thanks,
Sergey


Top
 Profile  
 
 Post subject: Re: Concurrent entity modification
PostPosted: Sat Dec 20, 2014 1:12 pm 
Newbie

Joined: Sat Dec 20, 2014 10:22 am
Posts: 2
Problem was resolved by adding @DynamicUpdate annotation to entity and changing saveOrUpdate to merge:
Code:
    public void save(final T entity) {
        try {
            createNewSessionAndTransaction();
            if (session != null && entity != null) {
                session.merge(entity);
            }
            commitTransaction();
        } catch (HibernateException e) {
            rollbackTransaction();
        }
    }


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

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.