Announcement Announcement Module
Collapse
No announcement yet.
optimistic locking in Spring + Hibernate? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • optimistic locking in Spring + Hibernate?

    According to hibernate, I have done all the stuff needed to apply optimistic locking.
    As I know optimistic locking will be working in this way
    Code:
    private void update(int id){
      // code to open the session will go here.
    // getting persisted object
       TempEntity persistedEntity = (TempEntity) session.get(TempEntity .class, id);
    
      // code to close the session will go here
    
    // updating  the object detached value
    persistedEntity.setName("Mark");
    
      // code to open the session will go here
    session1.merge(persistedEntity);
      // code to close the session will go here
    }
    Now as much I know optimistic locking will work.
    Because version is only checked when object is detached.


    When session is handled by spring.I can't make object detached as done above.
    I can't close the session and open it again.
    Code:
    // Implementation class update function
    private void update(int id){
     
       TempEntity persistedEntity = dao.getById(id);
       persistedEntity.setName("Mark");
       dao.merge(persistedEntity);
    
    }
    
    // Dao class
     class TempDao extends HibernateDaoSupport
    {
    
    	public TempEntity  update(TempEntity  updatedTempEntity ) throws DataAccessException
    	{
    		return this.getHibernateTemplate().merge(updatedTempEntity);
    		
    	}
           public TempEntity getById(int id) throws DataAccessException
    	{
    		return this.getHibernateTemplate().get(TempEntity.class, id);
    	}
    
    }
    And finally optimistic locking is not working for me.
    I will be thank full if you provide any solution for this.

    Thanks

  • #2
    And why shouldn't that work? Works as expected imho... You retrieve an object (version x), change something and in the database nothing has changed (version is still x) so you will happily update everything. This would work regardless of 1 or 2 transactions...

    Comment


    • #3
      But the problem is that I can't apply optimistic locking.
      For example : If I open 2 different browser and logIn with 2 different user , load entity having version 1 in browser.
      If one user update that entity than version change to 2.
      But if another user who is having entity with version 1 update the entity than entity is getting updated and version change to 3.
      I am not getting any error Which I should get when optimistic locking is applied.
      Reason : persistedEntity [see from above code] is not getting detached because removing persisted object and updating it is done in same session which is manage by spring.Object only get detached if it is retrieved from one session and changed and updated in new session Which in my case can't be done.

      Comment


      • #4
        As stated you aren't appying optimistic locking so how could it work... You retrieve the object from the database BEFORE you are applying changes so no locking is going to be applied..

        The problem here is that you retrieve, change, commit in one session/transaction and as such it isn't going to work. You will need a detached entity (store the object in the http session for instance) and apply changes to that object and update then you will get an exception, in your case you will never get an exception.

        Comment

        Working...
        X