/** * Obtain a JPA EntityManager from the given factory. Is aware of a corresponding * EntityManager bound to the current thread, e.g. when using JpaTransactionManager. * <p>Same as {@code getEntityManager}, but throwing the original PersistenceException. * @param emf the EntityManagerFactory to create the EntityManager with * @param properties the properties to be passed into the {@code createEntityManager} * call (may be {@code null}) * @return the EntityManager, or {@code null} if none found * @throws javax.persistence.PersistenceException if the EntityManager couldn't be created * @see #getTransactionalEntityManager(javax.persistence.EntityManagerFactory) * @see JpaTransactionManager */ @Nullable public static EntityManager doGetTransactionalEntityManager(EntityManagerFactory emf, Map<?, ?> properties) throws PersistenceException { return doGetTransactionalEntityManager(emf, properties, true); }
/** * Obtain a JPA EntityManager from the given factory. Is aware of a corresponding * EntityManager bound to the current thread, e.g. when using JpaTransactionManager. * <p>Note: Will return {@code null} if no thread-bound EntityManager found! * @param emf the EntityManagerFactory to create the EntityManager with * @param properties the properties to be passed into the {@code createEntityManager} * call (may be {@code null}) * @return the EntityManager, or {@code null} if none found * @throws DataAccessResourceFailureException if the EntityManager couldn't be obtained * @see JpaTransactionManager */ @Nullable public static EntityManager getTransactionalEntityManager(EntityManagerFactory emf, @Nullable Map<?, ?> properties) throws DataAccessResourceFailureException { try { return doGetTransactionalEntityManager(emf, properties, true); } catch (PersistenceException ex) { throw new DataAccessResourceFailureException("Could not obtain JPA EntityManager", ex); } }
/** * Obtain a JPA EntityManager from the given factory. Is aware of a corresponding * EntityManager bound to the current thread, e.g. when using JpaTransactionManager. * <p>Same as {@code getEntityManager}, but throwing the original PersistenceException. * @param emf the EntityManagerFactory to create the EntityManager with * @param properties the properties to be passed into the {@code createEntityManager} * call (may be {@code null}) * @return the EntityManager, or {@code null} if none found * @throws javax.persistence.PersistenceException if the EntityManager couldn't be created * @see #getTransactionalEntityManager(javax.persistence.EntityManagerFactory) * @see JpaTransactionManager */ @Nullable public static EntityManager doGetTransactionalEntityManager(EntityManagerFactory emf, Map<?, ?> properties) throws PersistenceException { return doGetTransactionalEntityManager(emf, properties, true); }
/** * Obtain a JPA EntityManager from the given factory. Is aware of a corresponding * EntityManager bound to the current thread, e.g. when using JpaTransactionManager. * <p>Note: Will return {@code null} if no thread-bound EntityManager found! * @param emf the EntityManagerFactory to create the EntityManager with * @param properties the properties to be passed into the {@code createEntityManager} * call (may be {@code null}) * @return the EntityManager, or {@code null} if none found * @throws DataAccessResourceFailureException if the EntityManager couldn't be obtained * @see JpaTransactionManager */ @Nullable public static EntityManager getTransactionalEntityManager(EntityManagerFactory emf, @Nullable Map<?, ?> properties) throws DataAccessResourceFailureException { try { return doGetTransactionalEntityManager(emf, properties, true); } catch (PersistenceException ex) { throw new DataAccessResourceFailureException("Could not obtain JPA EntityManager", ex); } }
EntityManager target = EntityManagerFactoryUtils.doGetTransactionalEntityManager( this.targetFactory, this.properties, this.synchronizedWithTransaction);
public static boolean isTransactionalEntityManager(EntityManager em) { EntityManager target = EntityManagerFactoryUtils.doGetTransactionalEntityManager( em.getEntityManagerFactory(), em.getProperties(), true); return target != null; }
@Test public void testDoGetEntityManager() { // test null assertion try { EntityManagerFactoryUtils.doGetTransactionalEntityManager(null, null); fail("expected exception"); } catch (IllegalArgumentException ex) { // it's okay } EntityManagerFactory factory = mock(EntityManagerFactory.class); // no tx active assertNull(EntityManagerFactoryUtils.doGetTransactionalEntityManager(factory, null)); assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty()); }
EntityManager target = EntityManagerFactoryUtils.doGetTransactionalEntityManager( this.targetFactory, this.properties, this.synchronizedWithTransaction);
@Test public void testDoGetEntityManagerWithTx() throws Exception { try { EntityManagerFactory factory = mock(EntityManagerFactory.class); EntityManager manager = mock(EntityManager.class); TransactionSynchronizationManager.initSynchronization(); given(factory.createEntityManager()).willReturn(manager); // no tx active assertSame(manager, EntityManagerFactoryUtils.doGetTransactionalEntityManager(factory, null)); assertSame(manager, ((EntityManagerHolder)TransactionSynchronizationManager.unbindResource(factory)).getEntityManager()); } finally { TransactionSynchronizationManager.clearSynchronization(); } assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty()); }
EntityManager target = EntityManagerFactoryUtils.doGetTransactionalEntityManager( this.targetFactory, this.properties, this.synchronizedWithTransaction);
EntityManager target = EntityManagerFactoryUtils.doGetTransactionalEntityManager( this.targetFactory, this.properties, this.synchronizedWithTransaction);
EntityManager target = EntityManagerFactoryUtils.doGetTransactionalEntityManager( this.targetFactory, this.properties, this.synchronizedWithTransaction);
throws PersistenceException { return doGetTransactionalEntityManager(emf, properties, true);
@Override public EntityManager getEntityManager(String store) { if (!TransactionSynchronizationManager.isActualTransactionActive()) throw new IllegalStateException("No active transaction"); EntityManagerFactory emf; if (Stores.isMain(store)) emf = this.jpaEmf; else emf = beanLocator.get("entityManagerFactory_" + store); javax.persistence.EntityManager jpaEm = EntityManagerFactoryUtils.doGetTransactionalEntityManager(emf, null, true); if (!jpaEm.isJoinedToTransaction()) throw new IllegalStateException("No active transaction for " + store + " database"); EntityManager entityManager = createEntityManager(jpaEm); EntityManagerContext ctx = contextHolder.get(store); if (ctx != null) { entityManager.setSoftDeletion(ctx.isSoftDeletion()); } else { ctx = new EntityManagerContext(); ctx.setSoftDeletion(isSoftDeletion()); contextHolder.set(ctx, store); entityManager.setSoftDeletion(isSoftDeletion()); } EntityManager emProxy = (EntityManager) Proxy.newProxyInstance( getClass().getClassLoader(), new Class[]{EntityManager.class}, new EntityManagerInvocationHandler(entityManager, store) ); return emProxy; }