javax.persistence.EntityManager.isOpen()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(10.5k)|赞(0)|评价(0)|浏览(180)

本文整理了Java中javax.persistence.EntityManager.isOpen()方法的一些代码示例,展示了EntityManager.isOpen()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。EntityManager.isOpen()方法的具体详情如下:
包路径:javax.persistence.EntityManager
类名称:EntityManager
方法名:isOpen

EntityManager.isOpen介绍

[英]Determine whether the entity manager is open.
[中]确定实体管理器是否打开。

代码示例

代码示例来源:origin: spring-projects/spring-framework

/**
 * Close the given JPA EntityManager,
 * catching and logging any cleanup exceptions thrown.
 * @param em the JPA EntityManager to close (may be {@code null})
 * @see javax.persistence.EntityManager#close()
 */
public static void closeEntityManager(@Nullable EntityManager em) {
  if (em != null) {
    try {
      if (em.isOpen()) {
        em.close();
      }
    }
    catch (PersistenceException ex) {
      logger.debug("Could not close JPA EntityManager", ex);
    }
    catch (Throwable ex) {
      logger.debug("Unexpected exception on closing JPA EntityManager", ex);
    }
  }
}

代码示例来源:origin: Activiti/Activiti

public void close() {
 if (closeEntityManager && entityManager != null && !entityManager.isOpen()) {
  try {
   entityManager.close();
  } catch (IllegalStateException ise) {
   throw new ActivitiException("Error while closing EntityManager, may have already been closed or it is container-managed", ise);
  }
 }
}

代码示例来源:origin: org.springframework/spring-orm

/**
 * Close the given JPA EntityManager,
 * catching and logging any cleanup exceptions thrown.
 * @param em the JPA EntityManager to close (may be {@code null})
 * @see javax.persistence.EntityManager#close()
 */
public static void closeEntityManager(@Nullable EntityManager em) {
  if (em != null) {
    try {
      if (em.isOpen()) {
        em.close();
      }
    }
    catch (PersistenceException ex) {
      logger.debug("Could not close JPA EntityManager", ex);
    }
    catch (Throwable ex) {
      logger.debug("Unexpected exception on closing JPA EntityManager", ex);
    }
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Test
@SuppressWarnings("unchecked")
public void testEntityManagerProxyIsProxy() {
  assertTrue(Proxy.isProxyClass(sharedEntityManager.getClass()));
  Query q = sharedEntityManager.createQuery("select p from Person as p");
  q.getResultList();
  assertTrue("Should be open to start with", sharedEntityManager.isOpen());
  sharedEntityManager.close();
  assertTrue("Close should have been silently ignored", sharedEntityManager.isOpen());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
@SuppressWarnings("unchecked")
public void testEntityManagerProxyIsProxy() {
  EntityManager em = entityManagerFactory.createEntityManager();
  assertTrue(Proxy.isProxyClass(em.getClass()));
  Query q = em.createQuery("select p from Person as p");
  List<Person> people = q.getResultList();
  assertNotNull(people);
  assertTrue("Should be open to start with", em.isOpen());
  em.close();
  assertFalse("Close should work on application managed EM", em.isOpen());
}

代码示例来源:origin: hibernate/hibernate-orm

protected EntityManager createEntityManager(Map properties) {
    // always reopen a new EM and close the existing one
    if ( em != null && em.isOpen() ) {
      em.close();
    }
    em = entityManagerFactory.createEntityManager( properties );
    return em;
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void deferredQueryWithSingleResult() {
  EntityManagerFactory emf = mock(EntityManagerFactory.class);
  EntityManager targetEm = mock(EntityManager.class);
  Query query = mock(Query.class);
  given(emf.createEntityManager()).willReturn(targetEm);
  given(targetEm.createQuery("x")).willReturn(query);
  given(targetEm.isOpen()).willReturn(true);
  EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf);
  em.createQuery("x").getSingleResult();
  verify(query).getSingleResult();
  verify(targetEm).close();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void deferredQueryWithUpdate() {
  EntityManagerFactory emf = mock(EntityManagerFactory.class);
  EntityManager targetEm = mock(EntityManager.class);
  Query query = mock(Query.class);
  given(emf.createEntityManager()).willReturn(targetEm);
  given(targetEm.createQuery("x")).willReturn(query);
  given(targetEm.isOpen()).willReturn(true);
  EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf);
  em.createQuery("x").executeUpdate();
  verify(query).executeUpdate();
  verify(targetEm).close();
}

代码示例来源:origin: stackoverflow.com

throw e;
} finally {
  if (em != null && em.isOpen()) {
    em.close();

代码示例来源:origin: spring-projects/spring-framework

@Test
public void deferredQueryWithResultList() {
  EntityManagerFactory emf = mock(EntityManagerFactory.class);
  EntityManager targetEm = mock(EntityManager.class);
  Query query = mock(Query.class);
  given(emf.createEntityManager()).willReturn(targetEm);
  given(targetEm.createQuery("x")).willReturn(query);
  given(targetEm.isOpen()).willReturn(true);
  EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf);
  em.createQuery("x").getResultList();
  verify(query).getResultList();
  verify(targetEm).close();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
@SuppressWarnings("unchecked")
public void testEntityManagerProxyIsProxy() {
  EntityManager em = createContainerManagedEntityManager();
  assertTrue(Proxy.isProxyClass(em.getClass()));
  Query q = em.createQuery("select p from Person as p");
  List<Person> people = q.getResultList();
  assertTrue(people.isEmpty());
  assertTrue("Should be open to start with", em.isOpen());
  try {
    em.close();
    fail("Close should not work on container managed EM");
  }
  catch (IllegalStateException ex) {
    // OK
  }
  assertTrue(em.isOpen());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testTransactionCommitWithSharedEntityManagerAndPropagationSupports() {
  given(manager.isOpen()).willReturn(true);
  tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
  tt.execute(status -> {
    bean.sharedEntityManager.clear();
    return null;
  });
  verify(manager).clear();
  verify(manager).close();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testTransactionCommitWithSharedEntityManagerUnsynchronizedAndPropagationSupports() {
  given(manager.isOpen()).willReturn(true);
  tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
  tt.execute(status -> {
    bean.sharedEntityManagerUnsynchronized.clear();
    return null;
  });
  verify(manager).clear();
  verify(manager).close();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void deferredQueryWithResultStream() {
  EntityManagerFactory emf = mock(EntityManagerFactory.class);
  EntityManager targetEm = mock(EntityManager.class);
  Query query = mock(Query.class);
  given(emf.createEntityManager()).willReturn(targetEm);
  given(targetEm.createQuery("x")).willReturn(query);
  given(targetEm.isOpen()).willReturn(true);
  EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf);
  em.createQuery("x").getResultStream();
  verify(query).getResultStream();
  verify(targetEm).close();
}

代码示例来源:origin: hibernate/hibernate-orm

return;
if ( !em.isOpen() ) {
  em = null;
  return;
if ( em.isOpen() ) {
  em.close();
  log.warn( "The EntityManager is not closed. Closing it." );

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testInvalidIsolation() {
  tt.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE);
  given(manager.isOpen()).willReturn(true);
  try {
    tt.execute(new TransactionCallbackWithoutResult() {
      @Override
      protected void doInTransactionWithoutResult(TransactionStatus status) {
      }
    });
    fail("Should have thrown InvalidIsolationLevelException");
  }
  catch (InvalidIsolationLevelException ex) {
    // expected
  }
  verify(manager).close();
}

代码示例来源:origin: hibernate/hibernate-orm

private void mergeDocument(FlushModeType flushModeType, Long id) {
  final EntityManager entityManager = getOrCreateEntityManager();
  try {
    entityManager.setFlushMode( flushModeType );
    entityManager.getTransaction().begin();
    DocumentA doc = entityManager.find( DocumentA.class, id );
    doc.setDate( new Date() );
    for ( DocumentLineA line : doc.getLines() ) {
      line.setText( "Updated" );
    }
    DocumentLineA line = new DocumentLineA();
    line.setText( "line2" );
    doc.addLine( line );
    entityManager.merge( doc );
    entityManager.getTransaction().commit();
  }
  catch ( Exception e ) {
    if ( entityManager != null && entityManager.getTransaction().isActive() ) {
      entityManager.getTransaction().rollback();
    }
    throw e;
  }
  finally {
    if ( entityManager != null && entityManager.isOpen() ) {
      entityManager.close();
    }
  }
}

代码示例来源:origin: hibernate/hibernate-orm

private Long persistDocument(FlushModeType flushModeType) {
  final EntityManager entityManager = getOrCreateEntityManager();
  try {
    entityManager.setFlushMode( flushModeType );
    entityManager.getTransaction().begin();
    DocumentA doc = new DocumentA();
    doc.setNumber( "1" );
    doc.setDate( new Date() );
    DocumentLineA line = new DocumentLineA();
    line.setText( "line1" );
    doc.addLine( line );
    entityManager.persist( doc );
    entityManager.getTransaction().commit();
    return doc.getId();
  }
  catch ( Exception e ) {
    if ( entityManager != null && entityManager.getTransaction().isActive() ) {
      entityManager.getTransaction().rollback();
    }
    throw e;
  }
  finally {
    if ( entityManager != null && entityManager.isOpen() ) {
      entityManager.close();
    }
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testPropertiesForTransactionalEntityManager() {
  Properties props = new Properties();
  props.put("foo", "bar");
  EntityManager em = mock(EntityManager.class);
  given(mockEmf.createEntityManager(props)).willReturn(em);
  given(em.getDelegate()).willReturn(new Object());
  given(em.isOpen()).willReturn(true);
  PersistenceAnnotationBeanPostProcessor pabpp = new MockPersistenceAnnotationBeanPostProcessor();
  DefaultPrivatePersistenceContextFieldWithProperties transactionalField =
      new DefaultPrivatePersistenceContextFieldWithProperties();
  pabpp.postProcessProperties(null, transactionalField, "bean");
  assertNotNull(transactionalField.em);
  assertNotNull(transactionalField.em.getDelegate());
  verify(em).close();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testTransactionCommitWithPropagationSupports() {
  given(manager.isOpen()).willReturn(true);
  final List<String> l = new ArrayList<>();
  l.add("test");
  tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
  assertTrue(!TransactionSynchronizationManager.hasResource(factory));
  assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
  Object result = tt.execute(new TransactionCallback() {
    @Override
    public Object doInTransaction(TransactionStatus status) {
      assertTrue(!TransactionSynchronizationManager.hasResource(factory));
      assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
      assertTrue(!status.isNewTransaction());
      EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
      return l;
    }
  });
  assertSame(l, result);
  assertTrue(!TransactionSynchronizationManager.hasResource(factory));
  assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
  verify(manager).flush();
  verify(manager).close();
}

相关文章