【问题标题】:illegally attempted to associate a proxy with two open Sessions (JBPM)非法尝试将代理与两个打开的会话 (JBPM) 相关联
【发布时间】:2018-01-23 12:36:04
【问题描述】:

我有一个使用 JBPM (5.4) 的应用程序。它执行几个应该在同一事务上运行的工作项操作。在每个动作中,它都会创建一个 entityManager。 当我尝试运行其中一个进程时,我得到非法尝试将代理与两个打开的会话关联异常。

有没有办法在会话之间合并对象?我无法控制 jbpm 进程中的会话/事务。

问候

【问题讨论】:

    标签: java hibernate jpa jbpm


    【解决方案1】:

    javax.transaction 有同步接口,在这种情况下我认为你应该使用它。

    此代码来自 seam/Hibernate 和 jbpm 之间的集成。

    public class ManagedJbpmContext implements Synchronization
    {
       private static final LogProvider log = Logging.getLogProvider(ManagedJbpmContext.class);
    
       private JbpmContext jbpmContext;
       private boolean synchronizationRegistered;
    
       @Create
       public void create() throws NamingException, RollbackException, SystemException
       {
          jbpmContext = Jbpm.instance().getJbpmConfiguration().createJbpmContext();
          assertNoTransactionManagement();
          log.debug( "created seam managed jBPM context");
       }
    
       private void assertNoTransactionManagement()
       {
          DbPersistenceServiceFactory dpsf = (DbPersistenceServiceFactory) jbpmContext.getJbpmConfiguration()
                .getServiceFactory(Services.SERVICENAME_PERSISTENCE);
          if ( dpsf.isTransactionEnabled() )
          {
             throw new IllegalStateException("jBPM transaction management is enabled, disable in jbpm.cfg.xml");
          }
       }
    
       @Unwrap
       public JbpmContext getJbpmContext() throws NamingException, RollbackException, SystemException
       {
          joinTransaction();
          return jbpmContext;
       }
    
       private void joinTransaction() throws SystemException
       {
          UserTransaction transaction = Transaction.instance();
    
          if ( !transaction.isActiveOrMarkedRollback() )
          {
             throw new IllegalStateException("JbpmContext may only be used inside a transaction");
          }
    
          if ( !synchronizationRegistered && !Lifecycle.isDestroying() && transaction.isActive() )
          {
             jbpmContext.getSession().isOpen();
             try //TODO: what we really want here is if (!cmt)
             {
                transaction.registerSynchronization(this);
             }
             catch (UnsupportedOperationException uoe)
             {
                jbpmContext.getSession().getTransaction().registerSynchronization(this);
             }
             synchronizationRegistered = true;
          }
       }
    
       public void beforeCompletion()
       {
          log.debug( "flushing seam managed jBPM context" );
    
          jbpmContext.getSession().flush();
          log.debug( "done flushing seam managed jBPM context" );
       }
    
       public void afterCompletion(int status) 
       {
          synchronizationRegistered = false;
          if ( !Contexts.isEventContextActive() )
          {
             //in calls to MDBs and remote calls to SBs, the 
             //transaction doesn't commit until after contexts
             //are destroyed, so wait until the transaction
             //completes before closing the session
             //on the other hand, if we still have an active
             //event context, leave it open
             closeContext();
          }
       }
    
    
    
       private void closeContext()
       {
          log.debug( "destroying seam managed jBPM context" );
          jbpmContext.close();
          log.debug( "done destroying seam managed jBPM context" );
       }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2015-10-11
      • 2013-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-18
      相关资源
      最近更新 更多