问题描述
这是我的存储库层:
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);
}
}
这是我的服务层:
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);
}
}
我遇到了这个异常:
[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)
注意:我不希望ServiceImpl.findUserById()方法使用 Transactional
.
Note: I don't want Transactional
for ServiceImpl.findUserById() method.
执行 @Transactional(readOnly = true)
或 @Transactional(propagation = Propagation.REQUIRES_NEW)
是否可以解决此问题?
Doing @Transactional(readOnly =true)
or @Transactional(propagation =Propagation.REQUIRES_NEW)
will solve this problem?
通过调用 session.merge()
可以解决此问题,但是我不能使用它.
By calling session.merge()
this issue will be solved, however I cannot use it.
推荐答案
您将不得不提供更多有关正在发生的事情的信息.每次您保存用户时,还是仅当您从多个线程中同时调用 saveUser(user)
时,才会发生此错误吗?还是在您每次调用 findUserById()
或 saveUser()
时发生?
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? Or is it happening anytime you call findUserById()
or saveUser()
?
您可能希望查看 @Transactional 注释的nofollow> Isolation 参数.默认值为 Isolation.DEFAULT
(这是"...基础数据存储的默认隔离级别").
You might want to look into the Isolation parameter for the @Transactional
annotation. The default is Isolation.DEFAULT
(which is the "...default isolation level of the underlying datastore").
使用此函数的方式应确定要为此参数提供什么值.您的 saveUser(user)
函数是否几乎总是与对该函数的另一个调用冲突?然后,您可能要使用:
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? Then you might want to use:
@Transactional(isolation = Isolation.READ_COMMITTED)
不过,我会说听起来确实工作正常.有意义的是,如果在另一个线程中修改了您的对象,然后尝试同时在另一个线程中提交同一对象,则会得到 StaleObjectStateException
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
这篇关于春季休眠乐观锁定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!