简体   繁体   English

spring hibernate乐观锁定问题

[英]spring hibernate optimistic locking issue

This is my Repository layer: 这是我的Repository层:

class RepositoryImpl implements Repository{ 
    @Override
    public Serializable saveOrUpdate(Object obj) {      
        return getSession().save(obj);
    }

    @Override
    public Object get(Class refClass, Serializable key)
    {
            return getSession().get(refClass, key);
    }
}

This is my Service layer: 这是我的服务层:

class ServiceImpl implements Service{

    @Autowired
    Repository repository;

    @Override
    @Transactional
    public Object findUserById(Serializable key) {
        // TODO Auto-generated method stub
        return repository.get(User.class,key);
    }

    @Override
    @Transactional
    public Serializable saveUser(Object o) {
        // TODO Auto-generated method stub
        return repository.saveOrUpdate(o);
    }   
}

Here I am accessing and updating objects asynchronously: 在这里,我以异步方式访问和更新对象:

class SomeClass{
    @Autowired
    Service service;    
    public void asynchronousSaveOrUpdate(){
        User u = service.findUserbyId(1);
        service.saveUser(1);
    }
    public void asynchronousfindUsersById(){
        service.findUserbyId(1);
    }
}

I got this exception: 我有这个例外:

[1236]: optimistic locking failed; nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.deadline.core.model.User#1236]
        at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:672)
        at org.springframework.orm.hibernate3.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:793)
        at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:664)
        at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754)
        at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)
        at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:393)
        at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:120)

Note: I don't want Transactional for ServiceImpl.findUserById() method. 注意:我不希望Transactional用于ServiceImpl.findUserById()方法。

Doing @Transactional(readOnly =true) or @Transactional(propagation =Propagation.REQUIRES_NEW) will solve this problem? 执行@Transactional(readOnly =true)@Transactional(propagation =Propagation.REQUIRES_NEW)会解决这个问题吗?

By calling session.merge() this issue will be solved, however I cannot use it. 通过调用session.merge()这个问题将得到解决,但我无法使用它。

You'll have to provide a little more information about what is going on. 您将不得不提供有关正在发生的事情的更多信息。 Is this error occurring every time you save a user, or only when you call saveUser(user) at the same time from multiple threads? 每次保存用户时是出现此错误,还是仅在多个线程中同时调用saveUser(user)时发生此错误? Or is it happening anytime you call findUserById() or saveUser() ? 或者它是否在您调用findUserById()saveUser()

You might want to look into the Isolation parameter for the @Transactional annotation. 您可能希望查看@Transactional注释的Isolation参数。 The default is Isolation.DEFAULT (which is the "...default isolation level of the underlying datastore"). 默认值为Isolation.DEFAULT (这是“...基础数据存储的默认隔离级别”)。

The way you are using this function should determine what value you want to supply for this argument. 使用此函数的方式应确定要为此参数提供的值。 Will your saveUser(user) function almost always conflict with with another call to that function? 你的saveUser(user)函数几乎总是与另一个函数调用冲突吗? Then you might want to use: 然后你可能想要使用:

@Transactional(isolation = Isolation.READ_COMMITTED)

Edit: I will say it does sound like this is functioning properly, though. 编辑:我会说它确实听起来像是运作正常。 It makes sense that if your object is modified in another thread, and then you try to commit that same object from another thread at the same time, you'd get StaleObjectStateException 有意义的是,如果你的对象在另一个线程中被修改,然后你试图同时从另一个线程提交同一个对象,你会得到StaleObjectStateException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM