/** * The JPA SFSB interceptor will track the stack of SFSB invocations. The underlying EM will be obtained from * the current SFSB being invoked (via our JPA SFSB interceptor). * * Every entity manager call (to AbstractEntityManager) will call this method to get the underlying entity manager * (e.g. the Hibernate persistence provider). * * See org.jboss.ejb3.stateful.EJB3XPCResolver.getExtendedPersistenceContext() to see the as6 implementation of this. * * @return EntityManager */ @Override protected EntityManager getEntityManager() { internalAssociateWithJtaTx(); return underlyingEntityManager; }
entityManager1 = new ExtendedEntityManager(unitName, emf.createEntityManager(synchronizationType, properties), synchronizationType, tsr, transactionManager); entityManager1 = new ExtendedEntityManager(unitName, emf.createEntityManager(properties), synchronizationType, tsr, transactionManager); entityManager1.increaseReferenceCount(); if (ROOT_LOGGER.isDebugEnabled()) ROOT_LOGGER.debugf("inherited existing ExtendedEntityManager from SFSB invocation stack, unit name=%s, " + "%d beans sharing ExtendedEntityManager, useDeepInheritance = %b", unitName, entityManager1.getReferenceCount(), useDeepInheritance);
public synchronized void refCountedClose() { referenceCount--; if (referenceCount == 0) { if (underlyingEntityManager.isOpen()) { underlyingEntityManager.close(); if (isTraceEnabled) { ROOT_LOGGER.tracef("closed extended persistence context (%s)", puScopedName); } } } else if (isTraceEnabled) { ROOT_LOGGER.tracef("decremented extended persistence context (%s) owner count to %d", puScopedName, referenceCount); } // referenceCount should never be negative, if it is we need to fix the bug that caused it to decrement too much if (referenceCount < 0) { throw JpaLogger.ROOT_LOGGER.referenceCountedEntityManagerNegativeCount(referenceCount, getScopedPuName()); } }
@Override public Object processInvocation(InterceptorContext interceptorContext) throws Exception { final ComponentInstance componentInstance = interceptorContext.getPrivateData(ComponentInstance.class); try { return interceptorContext.proceed(); } finally { ManagedReference entityManagerRef = (ManagedReference) componentInstance.getInstanceData(SFSBInvocationInterceptor.CONTEXT_KEY); if(entityManagerRef != null) { Map<String, ExtendedEntityManager> entityManagers = (Map<String, ExtendedEntityManager>) entityManagerRef.getInstance(); for(Map.Entry<String, ExtendedEntityManager> entry : entityManagers.entrySet()) { // close all extended persistence contexts that are referenced by the bean being destroyed entry.getValue().refCountedClose(); } } } } }
entityManager1 = new ExtendedEntityManager(unitName, emf.createEntityManager(synchronizationType, properties), synchronizationType, tsr, transactionManager); entityManager1 = new ExtendedEntityManager(unitName, emf.createEntityManager(properties), synchronizationType, tsr, transactionManager); entityManager1.increaseReferenceCount(); if (ROOT_LOGGER.isDebugEnabled()) ROOT_LOGGER.debugf("inherited existing ExtendedEntityManager from SFSB invocation stack, unit name=%s, " + "%d beans sharing ExtendedEntityManager, useDeepInheritance = %b", unitName, entityManager1.getReferenceCount(), useDeepInheritance);
public synchronized void refCountedClose() { referenceCount--; if (referenceCount == 0) { if (underlyingEntityManager.isOpen()) { underlyingEntityManager.close(); if (isTraceEnabled) { ROOT_LOGGER.tracef("closed extended persistence context (%s)", puScopedName); } } } else if (isTraceEnabled) { ROOT_LOGGER.tracef("decremented extended persistence context (%s) owner count to %d", puScopedName, referenceCount); } // referenceCount should never be negative, if it is we need to fix the bug that caused it to decrement too much if (referenceCount < 0) { throw JpaLogger.ROOT_LOGGER.referenceCountedEntityManagerNegativeCount(referenceCount, getScopedPuName()); } }
@Override public Object processInvocation(InterceptorContext interceptorContext) throws Exception { final ComponentInstance componentInstance = interceptorContext.getPrivateData(ComponentInstance.class); try { return interceptorContext.proceed(); } finally { ManagedReference entityManagerRef = (ManagedReference) componentInstance.getInstanceData(SFSBInvocationInterceptor.CONTEXT_KEY); if(entityManagerRef != null) { Map<String, ExtendedEntityManager> entityManagers = (Map<String, ExtendedEntityManager>) entityManagerRef.getInstance(); for(Map.Entry<String, ExtendedEntityManager> entry : entityManagers.entrySet()) { // close all extended persistence contexts that are referenced by the bean being destroyed entry.getValue().refCountedClose(); } } } } }
/** * The JPA SFSB interceptor will track the stack of SFSB invocations. The underlying EM will be obtained from * the current SFSB being invoked (via our JPA SFSB interceptor). * * Every entity manager call (to AbstractEntityManager) will call this method to get the underlying entity manager * (e.g. the Hibernate persistence provider). * * See org.jboss.ejb3.stateful.EJB3XPCResolver.getExtendedPersistenceContext() to see the as6 implementation of this. * * @return EntityManager */ @Override protected EntityManager getEntityManager() { internalAssociateWithJtaTx(); return underlyingEntityManager; }
@Override public Object processInvocation(InterceptorContext interceptorContext) throws Exception { ComponentInstance componentInstance = interceptorContext.getPrivateData(ComponentInstance.class); Map<String, ExtendedEntityManager> entityManagers = null; if(componentInstance.getInstanceData(SFSBInvocationInterceptor.CONTEXT_KEY) == null) { // Get all of the extended persistence contexts in use by the bean (some of which may of been inherited from // other beans). entityManagers = new HashMap<String, ExtendedEntityManager>(); componentInstance.setInstanceData(SFSBInvocationInterceptor.CONTEXT_KEY, new ImmediateManagedReference(entityManagers)); } else { ManagedReference entityManagerRef = (ManagedReference) componentInstance.getInstanceData(SFSBInvocationInterceptor.CONTEXT_KEY); entityManagers = (Map<String, ExtendedEntityManager>)entityManagerRef.getInstance(); } final ExtendedEntityManager[] ems = CreatedEntityManagers.getDeferredEntityManagers(); for (ExtendedEntityManager e : ems) { entityManagers.put(e.getScopedPuName(), e); } return interceptorContext.proceed(); } }
@Override public void userTransactionStarted() throws SystemException { Map<String, ExtendedEntityManager> currentActiveEntityManagers = SFSBCallStack.currentSFSBCallStackInvocation(); if (currentActiveEntityManagers != null && currentActiveEntityManagers.size() > 0) { for (ExtendedEntityManager extendedEntityManager: currentActiveEntityManagers.values()) { extendedEntityManager.internalAssociateWithJtaTx(); } } } }
@Override public Object processInvocation(InterceptorContext interceptorContext) throws Exception { ComponentInstance componentInstance = interceptorContext.getPrivateData(ComponentInstance.class); Map<String, ExtendedEntityManager> entityManagers = null; if(componentInstance.getInstanceData(SFSBInvocationInterceptor.CONTEXT_KEY) == null) { // Get all of the extended persistence contexts in use by the bean (some of which may of been inherited from // other beans). entityManagers = new HashMap<String, ExtendedEntityManager>(); componentInstance.setInstanceData(SFSBInvocationInterceptor.CONTEXT_KEY, new ImmediateManagedReference(entityManagers)); } else { ManagedReference entityManagerRef = (ManagedReference) componentInstance.getInstanceData(SFSBInvocationInterceptor.CONTEXT_KEY); entityManagers = (Map<String, ExtendedEntityManager>)entityManagerRef.getInstance(); } final ExtendedEntityManager[] ems = CreatedEntityManagers.getDeferredEntityManagers(); for (ExtendedEntityManager e : ems) { entityManagers.put(e.getScopedPuName(), e); } return interceptorContext.proceed(); } }
@Override public void userTransactionStarted() throws SystemException { Map<String, ExtendedEntityManager> currentActiveEntityManagers = SFSBCallStack.currentSFSBCallStackInvocation(); if (currentActiveEntityManagers != null && currentActiveEntityManagers.size() > 0) { for (ExtendedEntityManager extendedEntityManager: currentActiveEntityManagers.values()) { extendedEntityManager.internalAssociateWithJtaTx(); } } } }
/** * Push the passed SFSB context handle onto the invocation call stack * * @param entityManagers the entity manager map */ public static void pushCall(Map<String, ExtendedEntityManager> entityManagers) { currentSFSBCallStack().add(entityManagers); if (entityManagers != null) { /** * JPA 2.0 spec section 7.9.1 Container Responsibilities: * "When a business method of the stateful session bean is invoked, * if the stateful session bean uses container managed transaction demarcation, * and the entity manager is not already associated with the current JTA transaction, * the container associates the entity manager with the current JTA transaction and * calls EntityManager.joinTransaction. * " */ for(ExtendedEntityManager extendedEntityManager: entityManagers.values()) { extendedEntityManager.internalAssociateWithJtaTx(); } } }
/** * Push the passed SFSB context handle onto the invocation call stack * * @param entityManagers the entity manager map */ public static void pushCall(Map<String, ExtendedEntityManager> entityManagers) { currentSFSBCallStack().add(entityManagers); if (entityManagers != null) { /** * JPA 2.0 spec section 7.9.1 Container Responsibilities: * "When a business method of the stateful session bean is invoked, * if the stateful session bean uses container managed transaction demarcation, * and the entity manager is not already associated with the current JTA transaction, * the container associates the entity manager with the current JTA transaction and * calls EntityManager.joinTransaction. * " */ for(ExtendedEntityManager extendedEntityManager: entityManagers.values()) { extendedEntityManager.internalAssociateWithJtaTx(); } } }