我们有java web应用程序,我们使用spring,hibernate3,我从DB获取记录的实际版本时遇到问题。
以下是代码段:
Class Scheduler
{
public void scheduleTests(………….)
{
.
.
.
TestCase testCase;
.
//Updating entity
getHibernateTemplate ().saveOrUpdate(testCase);
getHibernateTemplate ().flush();
.
.
.
Starts new thread
.
}
Thread run{
.
.
TestCase testCase = getTestCase(testCaseId);
.
.
.
.
updateTestCase(testCase);
}
getTestCase(testCaseId)
{
TestCase testCase = null;
try
{
testCase=(TestCase)getHibernateTemplate().get(TestCase.class, testCaseId);
}
catch (Exception ex)
{
ex.printStackTrace();
}
return testCase;
}
updateTestCase(TestCase testCase)
{
try
{
getHibernateTemplate ().saveOrUpdate(testCase);
getHibernateTemplate ().flush();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
在方法计划测试中,我更改对象TestCase并将其保存到DB中。然后新线程启动,在这个线程的开头我向DB询问DB的实际版本的TestCase。然后执行一些功能,并在TestCase的最终状态更新为DB。
问题是当我运行此场景超过1次。然后发生了奇怪的事情。在线程方法运行中,我从方法getTestCase旧版本的TestCase中获取,然后使用以下异常更新崩溃:
错误
org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:300) - Could not synchronize database state with session
"org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [at.h3g.portal.db.webshop.WsRcCheck#405]
at org.hibernate.persister.entity.AbstractEntityPersister.check(AbstractEntityPersister.java:1680)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2271)
at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2180)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2456)
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:91)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:988)
at org.springframework.orm.hibernate3.HibernateTemplate$29.doInHibernate(HibernateTemplate.java:815)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:366)
at org.springframework.
ERROR org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:300) - Could not synchronize database state with session
"org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [at.h3g.portal.db.webshop.WsRcCheck#405]
at org.hibernate.persister.entity.AbstractEntityPersister.check(AbstractEntityPersister.java:1680)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2271)
at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2180)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2456)
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:91)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:988)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:337)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:584)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:500)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:473)
at org.springframework.transaction.interceptor.TransactionAspectSupport.doCommitTransactionAfterReturning(TransactionAspectSupport.java:267)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:170)
为什么我的旧版TestCase?我能做什么? 这是Hibernate和Spring的配置。
在Web.Xml中:
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory</param-value>
</init-param>
</filter>
和上下文配置:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="mappingResources">
<list>
.
.
.
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle9Dialect
</prop>
<prop key="hibernate.connection.release_mode">
auto
</prop>
</props>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/SOMEDATASOURCE</value>
</property>
</bean>
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean id="transactionInterceptorTestScheduling"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionAttributeSource">
<value> org.company.db.Scheduler.updateTestCase=PROPAGATION_REQUIRES_NEW
</value>
</property>
</bean>
我想补充一点,在那个时候只创建了一个线程,没有其他任何东西可以访问数据库中的TestCase数据。唯一的问题是getTestCase返回旧版本的TestCase记录,我现在不知道原因。
感谢您的建议。
答案 0 :(得分:0)
问题是hibernate会话是否与当前线程有关。所以一个解决方案是不使用线程,或者应该创建事务拦截器,它将负责线程的会话。有关Spring How to get bean with prototype scope from ProxyFactoryBean
的更多信息,请参阅