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.  [ 5 posts ] 
Author Message
 Post subject: Hibernate synchronization
PostPosted: Mon Mar 03, 2008 10:42 am 
Newbie

Joined: Mon Mar 03, 2008 10:07 am
Posts: 3
Hello. I've a question about synchronization with Hibernate. I'm using Hibernate 3.2 with MySql 5.
I know the different ways to achieve this (optimistic, pessimistic lock, ecc..) but my scenario is pretty different.
I've to web apps. The first application read from DB and populate an object called Content.
The obj is loaded and sent to the client with a simple DAO method:


Code:
public Content getContent(String contentID) throws Exception{
         Content c=null;
         Session session=null;
         Transaction tx=null;
         try{
            //DEBUG
            System.out.println("extracting content +contentID);
            //
            Long cID=Long.parseLong(contentID);
            c=new Content();
            session=HibernateUtil.getContentDBSessionFactory().openSession();
            tx=session.beginTransaction();
            c=(Content)session.createCriteria(Content.class).setFetchMode("contentData",FetchMode.JOIN)
               .add(Restrictions.idEq(cID)).uniqueResult();
            tx.commit();
         }catch(Exception e){
            if(tx!=null /*&& tx.wasCommitted()*/)
               tx.rollback();
            throw e;
         }finally{
            if(session!=null && session.isOpen()){
                  session.close();
                  
            }
         }
         return c;
      }



Through the second web app, an admin can remove a content with this DAO method:


Code:
public void deleteContent(String contentID) throws Exception{
      Session session=null;
      Transaction tx=null;
      Content c=getContent(contentID);
      try{
         session=HibernateUtil.getContentDBSessionFactory().openSession();
         tx=session.beginTransaction();
         session.delete(c);
         tx.commit();         
      }catch(Exception e){
         if(tx!=null)
            tx.rollback();
         throw e;
      }finally{
         if(session!=null && session.isOpen()){
            session.close();
         }
      }
   }


Now my question is: do I need any type of synchronization to assure that the two applications does not conflict if in the same instant the two DAO instances read and delete the same object? Or this type of sync is in some way guaranteed? Thank you


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 03, 2008 10:53 am 
Senior
Senior

Joined: Fri Jun 01, 2007 12:41 pm
Posts: 121
Quote:
do I need any type of synchronization to assure that the two applications does not conflict if in the same instant the two DAO instances read and delete the same object? Or this type of sync is in some way guaranteed?


Yes. you need synchronization. You have two options:

1. Using hibernate LockMode to set isolotion levels on DB from Session/
2. Register a user synchronization callback for this transaction. I never tried this.!!!

Code:
public void registerSynchronization(Synchronization synchronization)
                             throws HibernateException


Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 03, 2008 11:14 am 
Newbie

Joined: Mon Mar 03, 2008 10:07 am
Posts: 3
Thank you for the reply. but.. How can I do that? I know of different kinds of locking, but they are specified in each query.
I can't modify all the DAO to include locking. Is there a way to set this for all the transactions?
And then, what type of locking should I use? LockMode.WRITE?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 03, 2008 11:35 am 
Senior
Senior

Joined: Fri Jun 01, 2007 12:41 pm
Posts: 121
Quote:
Is there a way to set this for all the transactions?


You can use the second option. Register a user synchronization callback for the transaction.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 04, 2008 5:18 am 
Newbie

Joined: Mon Mar 03, 2008 10:07 am
Posts: 3
Thank you. I read all the ways that hibernate provides for sync, but I can't find what is suitable for me. Because my two transactions consist of a single read (first) and a read plus a delete (second method).
Now, I can't understand what type of concurrency problem can arise from this two methods...
I read about session.lock. It seems to be the most appropriate type of locking, but I don't want to place a useless locking that will slow up the application.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.