if ( this.appScopedEntityManager != null && !this.appScopedEntityManager.isOpen() ) {
throw new RuntimeException( "Provided APP_SCOPED_ENTITY_MANAGER is not open" );
}
if ( this.appScopedEntityManager == null ) {
EntityManagerHolder emHolder = (EntityManagerHolder) TransactionSynchronizationManager.getResource( this.emf );
if ( emHolder == null ) {
this.appScopedEntityManager = this.emf.createEntityManager();
emHolder = new EntityManagerHolder( this.appScopedEntityManager );
TransactionSynchronizationManager.bindResource( this.emf,
emHolder );
internalAppScopedEntityManager = true;
} else {
this.appScopedEntityManager = emHolder.getEntityManager();
}
this.env.set( EnvironmentName.APP_SCOPED_ENTITY_MANAGER,
emHolder.getEntityManager() );
}
}
if ( TransactionSynchronizationManager.isActualTransactionActive() ) {
this.appScopedEntityManager.joinTransaction();
}