Code example for EntityManagerHolder

0
	public EntityManager openEntityManager() { 
		if (TransactionSynchronizationManager.hasResource(entityManagerFactory)) { 
			return ((EntityManagerHolder) TransactionSynchronizationManager.getResource(entityManagerFactory)).getEntityManager(); 
		} else { 
			EntityManager entityManager = entityManagerFactory.createEntityManager(); 
			TransactionSynchronizationManager.bindResource(entityManagerFactory, new EntityManagerHolder(entityManager)); 
			return entityManager; 
		} 
	} 
	 
	/** 
	 * Suppression du EntityManager. 
	 */ 
	public void closeEntityManager() { 
		if (TransactionSynchronizationManager.hasResource(entityManagerFactory)) { 
			EntityManagerHolder entityManagerHolder = (EntityManagerHolder) TransactionSynchronizationManager.unbindResource(entityManagerFactory); 
			EntityManagerFactoryUtils.closeEntityManager(entityManagerHolder.getEntityManager()); 
		} 
	}