在Hibernate Search中进行索引时出错(在事务完成之前)

时间:2013-01-07 14:10:09

标签: java spring-mvc transactions spring-batch hibernate-search

我已经厌倦了解决这个问题所以如果有人能告诉我我错在哪里,我将不胜感激。

问题是我在我的Spring MVC项目中使用Spring-BatchHibernate Full-Text Search。所以从批处理作业Tasklet我调用以下代码:

 A a=aDao.merge(a);
 b.setA(a);
 bDao.save(b);

在对这些entites进行保存更新时,我收到异常,堆栈跟踪如下:

org.springframework.orm.hibernate3.HibernateSystemException: Error while indexing in Hibernate Search (before transaction completion); nested exception is org.hibernate.HibernateException: Error while indexing in Hibernate Search (before transaction completion)
    at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:690)
    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.support.TransactionTemplate.execute(TransactionTemplate.java:147)
    at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:264)
    at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:76)
    at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:367)
    at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:214)
    at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:143)
    at org.springframework.batch.core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:250)
    at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:195)
    at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:135)
    at org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:61)
    at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:60)
    at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:144)
    at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:124)
    at org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:135)
    at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:281)
    at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:120)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)
Caused by: org.hibernate.HibernateException: Error while indexing in Hibernate Search (before transaction completion)
    at org.hibernate.search.backend.impl.EventSourceTransactionContext$DelegateToSynchronizationOnBeforeTx.doBeforeTransactionCompletion(EventSourceTransactionContext.java:175)
    at org.hibernate.engine.ActionQueue$BeforeTransactionCompletionProcessQueue.beforeTransactionCompletion(ActionQueue.java:543)
    at org.hibernate.engine.ActionQueue.beforeTransactionCompletion(ActionQueue.java:216)
    at org.hibernate.impl.SessionImpl.beforeTransactionCompletion(SessionImpl.java:571)
    at org.hibernate.jdbc.JDBCContext.beforeTransactionCompletion(JDBCContext.java:250)
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:138)
    at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:656)
    ... 21 more
Caused by: org.hibernate.LazyInitializationException: could not initialize proxy - no Session
    at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:167)
    at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:215)
    at org.hibernate.search.util.HibernateHelper.unproxy(HibernateHelper.java:62)
    at org.hibernate.search.engine.DocumentBuilderIndexedEntity.buildDocumentFields(DocumentBuilderIndexedEntity.java:394)
    at org.hibernate.search.engine.DocumentBuilderIndexedEntity.buildDocumentFields(DocumentBuilderIndexedEntity.java:481)
    at org.hibernate.search.engine.DocumentBuilderIndexedEntity.buildDocumentFields(DocumentBuilderIndexedEntity.java:481)
    at org.hibernate.search.engine.DocumentBuilderIndexedEntity.getDocument(DocumentBuilderIndexedEntity.java:379)
    at org.hibernate.search.engine.DocumentBuilderIndexedEntity.createAddWork(DocumentBuilderIndexedEntity.java:317)
    at org.hibernate.search.engine.DocumentBuilderIndexedEntity.addWorkToQueue(DocumentBuilderIndexedEntity.java:295)
    at org.hibernate.search.engine.WorkPlan$PerEntityWork.enqueueLuceneWork(WorkPlan.java:445)
    at org.hibernate.search.engine.WorkPlan$PerClassWork.enqueueLuceneWork(WorkPlan.java:246)
    at org.hibernate.search.engine.WorkPlan.getPlannedLuceneWork(WorkPlan.java:150)
    at org.hibernate.search.backend.WorkQueue.prepareWorkPlan(WorkQueue.java:134)
    at org.hibernate.search.backend.impl.BatchedQueueingProcessor.prepareWorks(BatchedQueueingProcessor.java:124)
    at org.hibernate.search.backend.impl.PostTransactionWorkQueueSynchronization.beforeCompletion(PostTransactionWorkQueueSynchronization.java:89)
    at org.hibernate.search.backend.impl.EventSourceTransactionContext$DelegateToSynchronizationOnBeforeTx.doBeforeTransactionCompletion(EventSourceTransactionContext.java:172)
    ... 27 more

我没有弄错。我正在使用org.springframework.orm.hibernate3.HibernateTransactionManager而我的Spring版本是3.2 Hibernate核心版本是3.6最终。

注意:此异常经常发生,但是当我创建新表或使用新数据库时,它的工作方式就像魅力一样,所有修改/插入都在数据库中得到正确反映。有人可以解释我这种行为或者我做过一些可疑的事情。< / p>

告诉我您是否需要更多详细信息。

谢谢。

代码更新:

<batch:job-repository id="jobRepository"
                          data-source="myJNDI"
                          transaction-manager="transactionManager"
                          isolation-level-for-create="READ_COMMITTED"
                          max-varchar-length="2500"
                          lob-handler="defaultLobHandler"
    />



  <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="safeSessionFactory" />
    </bean>

像这样我在applicationContext.xml中为Spring-Batch配置事务管理器。我尝试通过手动提交事务来手动打开和关闭会话,但异常保持不变。是的,我已将我的方法注释为@Transactional。

1 个答案:

答案 0 :(得分:1)

我发现问题的根本原因是Spring's Declarative Transaction Management。 Spring Batch in Action Reference:

  
      
  • 为批处理应用程序禁用Spring的声明性事务 - 不要使用tx:annotation-driven元素或任何XML   与声明交易管理相关的配置。

  •   
  • 如果启用声明性事务,请小心使用传播级别 - 如果从Spring Batch作业调用事务类,   Spring的事务传播可能会干扰Spring Batch   交易因为传播水平。要求_NEW   传播水平通常会导致问题,因为   应用程序代码在自己的事务中运行,独立于   Spring Batch事务。

  •   

以下是避免Spring Batch-managed和 春季管理的交易:

Transaction Diagram