Code example for EntityManagerHolder

0
        boolean isNewEm = false; 
        if (em == null) { 
            logger.debug("Opening JPA EntityManager in OpenEntityManagerAspect"); 
            em = createEntityManager(emf); 
            isNewEm = true; 
            TransactionSynchronizationManager.bindResource(emf, new EntityManagerHolder(em)); 
        } 
        else { 
            logger.debug("Using Existing JPA EntityManager in OpenEntityManagerAspect"); 
        } 
		try { 
			return pjp.proceed(); 
		} 
		finally { 
            if (isNewEm) { 
                logger.debug("Closing JPA EntityManager in OpenEntityManagerAspect"); 
                TransactionSynchronizationManager.unbindResource(emf); 
                EntityManagerFactoryUtils.closeEntityManager(em); 
            } 
		}