Buggy code:
public class JTAEntityManager implements EntityManager { private static final Logger _logger = LoggerFactory . getLogger ( "org.apache.aries.jpa.container.context" ) ; private final EntityManagerFactory emf ; private final Map < String , Object > props ; private final JTAPersistenceContextRegistry reg ; private final AtomicLong instanceCount ; private final DestroyCallback callback ; private EntityManager detachedManager = null ; public JTAEntityManager ( EntityManagerFactory factory , Map < String , Object > properties , JTAPersistenceContextRegistry registry , AtomicLong activeCount , DestroyCallback onDestroy ) { } private EntityManager getPersistenceContext ( boolean forceTransaction ) { if ( forceTransaction ) { return reg . getCurrentPersistenceContext ( emf , props , instanceCount , callback ) ; } else { if ( reg . isTransactionActive ( ) ) { return reg . getCurrentPersistenceContext ( emf , props , instanceCount , callback ) ; } else { if ( ( ! ( ! ( ! ( reg . jtaIntegrationAvailable ( ) ) ) ) ) && ( JTAEntityManager . _logger . isDebugEnabled ( ) ) ) JTAEntityManager . _logger . debug ( "No<seq2seq4repair_space>integration<seq2seq4repair_space>with<seq2seq4repair_space>JTA<seq2seq4repair_space>transactions<seq2seq4repair_space>is<seq2seq4repair_space>available.<seq2seq4repair_space>No<seq2seq4repair_space>transaction<seq2seq4repair_space>context<seq2seq4repair_space>is<seq2seq4repair_space>active." ) ; if ( ( detachedManager ) == null ) { EntityManager temp = emf . createEntityManager ( props ) ; synchronized ( this ) { if ( ( detachedManager ) == null ) { <START_BUG> detachedManager = temp ; <END_BUG> temp = null ; } } if ( temp != null ) temp . close ( ) ; } return detachedManager ; } } } public void internalClose ( ) { } public void clear ( ) { } public void close ( ) { } public boolean contains ( Object arg0 ) { } public Query createNamedQuery ( String arg0 ) { } public Query createNativeQuery ( String arg0 ) { } @ SuppressWarnings ( "unchecked" ) public Query createNativeQuery ( String arg0 , Class arg1 ) { } public Query createNativeQuery ( String arg0 , String arg1 ) { } public Query createQuery ( String arg0 ) { } public < T > T find ( Class < T > arg0 , Object arg1 ) { } public void flush ( ) { } public Object getDelegate ( ) { } public FlushModeType getFlushMode ( ) { } public < T > T getReference ( Class < T > arg0 , Object arg1 ) { } public EntityTransaction getTransaction ( ) { } public boolean isOpen ( ) { } public void joinTransaction ( ) { } public void lock ( Object arg0 , LockModeType arg1 ) { } public < T > T merge ( T arg0 ) { } public void persist ( Object arg0 ) { } public void refresh ( Object arg0 ) { } public void remove ( Object arg0 ) { } public void setFlushMode ( FlushModeType arg0 ) { } public < T > TypedQuery < T > createNamedQuery ( String arg0 , Class < T > arg1 ) { } public < T > TypedQuery < T > createQuery ( CriteriaQuery < T > arg0 ) { } public < T > TypedQuery < T > createQuery ( String arg0 , Class < T > arg1 ) { } public void detach ( Object arg0 ) { } public < T > T find ( Class < T > arg0 , Object arg1 , Map < String , Object > arg2 ) { } public < T > T find ( Class < T > arg0 , Object arg1 , LockModeType arg2 ) { } public < T > T find ( Class < T > arg0 , Object arg1 , LockModeType arg2 , Map < String , Object > arg3 ) { } public CriteriaBuilder getCriteriaBuilder ( ) { } public EntityManagerFactory getEntityManagerFactory ( ) { } public LockModeType getLockMode ( Object arg0 ) { } public Metamodel getMetamodel ( ) { } public Map < String , Object > getProperties ( ) { } public void lock ( Object arg0 , LockModeType arg1 , Map < String , Object > arg2 ) { } public void refresh ( Object arg0 , Map < String , Object > arg1 ) { } public void refresh ( Object arg0 , LockModeType arg1 ) { } public void refresh ( Object arg0 , LockModeType arg1 , Map < String , Object > arg2 ) { } public void setProperty ( String arg0 , Object arg1 ) { } public < T > T unwrap ( Class < T > arg0 ) { } }

Fix:
detachedManager = new SynchronizedEntityManagerWrapper ( temp ) ;