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