【问题标题】:org.hibernate.LazyInitializationException: could not initialize proxy - no Session - occurs rarelyorg.hibernate.LazyInitializationException:无法初始化代理 - 没有会话 - 很少发生
【发布时间】:2017-10-13 22:15:50
【问题描述】:

我的批处理程序引发了LazyInitializationException 异常。这并非每次都会发生。这种情况很少发生,并且当批次重新运行时,记录的处理过程不会出错。

我正在为使用以下代码处理的每 100 条记录刷新缓存

private void flushHibernateCache() {
    final String CM_FLUSH_HBM_CHACHE = "flushHibernateCache";
    final String CM_CLEAR_HBM_CHACHE = "clearHibernateCache";
    BigInteger sessionLimit = getParameters().getSESSION_FLUSH_LIMIT();
    if (notNull(sessionLimit) && recordsProcessed.mod(sessionLimit).intValue() == 0) {
        FrameworkSession session = (FrameworkSession) SessionHolder.getSession();
        session.flush(CM_FLUSH_HBM_CHACHE);
        session.clear(CM_CLEAR_HBM_CHACHE);
    }
}

当我将要刷新的记录数从 100 增加到大于要处理的实际记录数时,批处理运行不会出错。

以下是错误日志:

18:21:53,687 [BATCHWorker:2] ERROR (com.splwg.base.support.cluster.ClusteredExecutionContext) An error has occurred while processing distributed grid work.  A rollback will be issued.  Processing of other work will continue.
com.splwg.base.api.batch.ThreadAbortedException: org.hibernate.LazyInitializationException: could not initialize proxy - no Session
    at com.splwg.base.api.batch.AbstractCommitStrategy.handleReplayableCommit(AbstractCommitStrategy.java:219)
    at com.splwg.base.api.batch.AbstractCommitStrategy.__AW_doCommit(AbstractCommitStrategy.java:201)
    at com.splwg.base.api.batch.AbstractCommitStrategy.doCommit(AbstractCommitStrategy.java)
    at com.splwg.base.api.batch.AbstractCommitStrategy.__AW_concreteExecuteWork(AbstractCommitStrategy.java:75)
    at com.splwg.base.api.batch.AbstractCommitStrategy.concreteExecuteWork(AbstractCommitStrategy.java)
    at com.splwg.base.api.batch.CommitEveryUnitStrategy.concreteExecuteWork(CommitEveryUnitStrategy.java:36)
    at com.splwg.base.api.batch.AbstractExecutionStrategy.__AW_executeWork(AbstractExecutionStrategy.java:243)
    at com.splwg.base.api.batch.AbstractExecutionStrategy.executeWork(AbstractExecutionStrategy.java)
    at com.splwg.base.support.batch.JavaBatchWork.executeThreadWork(JavaBatchWork.java:109)
    at com.splwg.base.support.batch.JavaBatchWork.access$300(JavaBatchWork.java:29)
    at com.splwg.base.support.batch.JavaBatchWork$DoExecuteWorkInSession.doBatchWorkInSession(JavaBatchWork.java:92)
    at com.splwg.base.support.batch.BatchWorkInSessionExecutable.__AW_run(BatchWorkInSessionExecutable.java:62)
    at com.splwg.base.support.batch.BatchWorkInSessionExecutable.run(BatchWorkInSessionExecutable.java)
    at com.splwg.base.support.batch.JavaBatchWork.doExecuteWork(JavaBatchWork.java:51)
    at com.splwg.base.support.grid.AbstractGridWork.executeWork(AbstractGridWork.java:72)
    at com.splwg.base.support.cluster.ThreadWork.processBatchWork(ThreadWork.java:54)
    at com.splwg.base.support.cluster.BatchWorkManager.processWork(BatchWorkManager.java:78)
    at com.tangosol.coherence.commonj.WorkManager$ScheduleWork.__AW_run(WorkManager.java:2266)
    at com.tangosol.coherence.commonj.WorkManager$ScheduleWork.run(WorkManager.java)
    at com.tangosol.coherence.component.util.daemon.queueProcessor.service.grid.InvocationService.__AW_onInvocationRequest(InvocationService.CDB:12)
    at com.tangosol.coherence.component.util.daemon.queueProcessor.service.grid.InvocationService.onInvocationRequest(InvocationService.CDB)
    at com.tangosol.coherence.component.util.daemon.queueProcessor.service.grid.InvocationService$InvocationRequest.run(InvocationService.CDB:1)
    at com.tangosol.coherence.component.util.DaemonPool$WrapperTask.run(DaemonPool.CDB:1)
    at com.tangosol.coherence.component.util.DaemonPool$WrapperTask.run(DaemonPool.CDB:32)
    at com.tangosol.coherence.component.util.DaemonPool$Daemon.__AW_onNotify(DaemonPool.CDB:65)
    at com.tangosol.coherence.component.util.DaemonPool$Daemon.onNotify(DaemonPool.CDB)
    at com.tangosol.coherence.component.util.Daemon.__AW_run(Daemon.CDB:51)
    at com.tangosol.coherence.component.util.Daemon.run(Daemon.CDB)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.hibernate.LazyInitializationException: could not initialize proxy - no Session
    at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:149)
    at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:195)
    at org.hibernate.bytecode.cglib.proxy.pojo.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:190)
    at org.hibernate.proxy.HibernateProxy$$EnhancerByCGLIB$$c8ddfe27.getThreads(<generated>)
    at com.splwg.base.domain.batch.batchRun.BatchThread_Per.getOwningLists(BatchThread_Per.java:191)
    at com.splwg.base.support.context.FrameworkSession.__AW_registerChange(FrameworkSession.java:1694)
    at com.splwg.base.support.context.FrameworkSession.registerChange(FrameworkSession.java)
    at com.splwg.base.support.api.AbstractBusinessEntity.registerChange(AbstractBusinessEntity.java:153)
    at com.splwg.base.support.context.FrameworkSession.__AW_registerChanges(FrameworkSession.java:1303)
    at com.splwg.base.support.context.FrameworkSession.registerChanges(FrameworkSession.java)
    at com.splwg.base.support.context.FrameworkSession.setDTOCalled(FrameworkSession.java:1290)
    at com.splwg.base.support.api.AbstractBusinessEntity.completeSetAbstractDTO(AbstractBusinessEntity.java:166)
    at com.splwg.base.support.api.AbstractBusinessEntity$2.run(AbstractBusinessEntity.java:281)
    at com.splwg.base.support.api.AbstractBusinessEntity$JumpToFrameworkRunner.run(AbstractBusinessEntity.java:372)
    at com.splwg.base.support.interception.SessionInterceptionManager.notifyGenericCallStackJump(SessionInterceptionManager.java:290)
    at com.splwg.base.support.context.FrameworkSession.notifyGenericCallStackJump(FrameworkSession.java:2311)
    at com.splwg.base.support.api.AbstractBusinessEntity.executeCallStackJump(AbstractBusinessEntity.java:298)
    at com.splwg.base.support.api.AbstractBusinessEntity.setDTOEtc(AbstractBusinessEntity.java:284)
    at com.splwg.base.domain.batch.batchRun.BatchThreadInstance_Per.setDTO(BatchThreadInstance_Per.java:124)
    at com.splwg.base.api.batch.AbstractExecutionStrategy.updateUnitsProcessed(AbstractExecutionStrategy.java:140)
    at com.splwg.base.api.batch.AbstractExecutionStrategy.updateRestartDataAndCommit(AbstractExecutionStrategy.java:112)
    at com.splwg.base.api.batch.AbstractCommitStrategy.saveRestartDataAndCommit(AbstractCommitStrategy.java:259)
    at com.splwg.base.api.batch.AbstractCommitStrategy.__AW_doCommit(AbstractCommitStrategy.java:191)
    ... 27 more

我无法确定发生此错误的原因。请帮忙。

【问题讨论】:

  • 您正在某个地方与fetch = FetchType.LAZY 建立关系。您必须将 DB-Session 绑定到您的函数才能获取您的关系。

标签: java hibernate flush


【解决方案1】:

您正在尝试在没有休眠会话的情况下调用 BatchThread_Per.getOwningLists()。

org.hibernate.LazyInitializationException 是 Hibernate 中的常见错误。当您尝试在休眠会话范围之外初始化休眠对象时,会抛出它。

请检查您的代码。

【讨论】:

  • 嗨,迈克。但我根本没有在我的代码中使用 BatchThread_Per.getOwningLists() 。还有其他建议吗?
  • 你正在使用它。检查你的错误日志 com.splwg.base.domain.batch.batchRun.BatchThread_Per.getOwningLists 检查它被调用的地方
  • 这是从基础包中调用的。我没有从我的自定义代码中调用它。
猜你喜欢
  • 2016-07-29
  • 2011-12-01
  • 2014-04-21
  • 2019-05-10
  • 2015-11-09
  • 2021-08-10
  • 1970-01-01
  • 2013-10-22
  • 1970-01-01
相关资源
最近更新 更多