本文整理汇总了Java中javax.persistence.EntityManagerFactory.close方法的典型用法代码示例。如果您正苦于以下问题:Java EntityManagerFactory.close方法的具体用法?Java EntityManagerFactory.close怎么用?Java EntityManagerFactory.close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.persistence.EntityManagerFactory
的用法示例。
在下文中一共展示了EntityManagerFactory.close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: jpaWithActiveSpanOnlyWithParent
import javax.persistence.EntityManagerFactory; //导入方法依赖的package包/类
@Test
public void jpaWithActiveSpanOnlyWithParent() {
try (Scope activeSpan = mockTracer.buildSpan("parent").startActive(true)) {
EntityManagerFactory entityManagerFactory =
Persistence.createEntityManagerFactory("jpa_active_span_only");
Employee employee = new Employee();
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
entityManager.persist(employee);
entityManager.getTransaction().commit();
entityManager.close();
entityManagerFactory.close();
assertNotNull(employee.id);
}
List<MockSpan> finishedSpans = mockTracer.finishedSpans();
assertEquals(9, finishedSpans.size());
checkSameTrace(finishedSpans);
assertNull(mockTracer.scopeManager().active());
}
示例2: jpa_with_parent_and_active_span_only
import javax.persistence.EntityManagerFactory; //导入方法依赖的package包/类
@Test
public void jpa_with_parent_and_active_span_only() {
try (Scope ignored = mockTracer.buildSpan("parent").startActive(true)) {
EntityManagerFactory entityManagerFactory = Persistence
.createEntityManagerFactory("jpa_active_span_only");
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
entityManager.persist(new Employee());
entityManager.persist(new Employee());
entityManager.getTransaction().commit();
entityManager.close();
entityManagerFactory.close();
}
List<MockSpan> spans = mockTracer.finishedSpans();
assertEquals(11, spans.size());
checkSameTrace(spans);
assertNull(mockTracer.activeSpan());
}
示例3: main
import javax.persistence.EntityManagerFactory; //导入方法依赖的package包/类
public static void main(String[] args) {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("pl.edu.bogdan.training.db.entity");
EntityManager em = entityManagerFactory.createEntityManager();
// begining of transaction
em.getTransaction().begin();
// Creating query in the way very similar to SQL
Query query = em.createQuery("Select r from Role r where r.name = :name");
query.setParameter("name", "ordinary");
// We obtaining result as a list of entities
List<Role> roles = query.getResultList();
for (Role role : roles) {
System.out.println(role.getName());
for (User u : role.getUsers()) {
System.out.println(" " + u.getLastName());
}
}
em.getTransaction().commit();
em.close();
entityManagerFactory.close();
}
示例4: jpa_with_active_span_only
import javax.persistence.EntityManagerFactory; //导入方法依赖的package包/类
@Test
public void jpa_with_active_span_only() {
EntityManagerFactory entityManagerFactory = Persistence
.createEntityManagerFactory("jpa_active_span_only");
Employee employee = new Employee();
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
entityManager.persist(employee);
entityManager.getTransaction().commit();
entityManager.close();
entityManagerFactory.close();
assertNotNull(employee.id);
List<MockSpan> finishedSpans = mockTracer.finishedSpans();
assertEquals(0, finishedSpans.size());
assertNull(mockTracer.activeSpan());
}
示例5: deleteTest
import javax.persistence.EntityManagerFactory; //导入方法依赖的package包/类
@Test
public void deleteTest() {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("db1start");
EntityManager manager = factory.createEntityManager();
Cidade cidade = manager.find(Cidade.class, 3L);
manager.getTransaction().begin();
manager.remove(cidade);
manager.getTransaction().commit();
factory.close();
}
示例6: createAndCloseDummyEMF
import javax.persistence.EntityManagerFactory; //导入方法依赖的package包/类
/**
* Create and close a dummy EMF to give the PersistenceProvider a chance to call
* punit.addTransformer(). This has to occur as early as possible as weaving needs
* to be done before the first entity class is loaded. So we can not wait till the
* real DataSource is found.
*/
private void createAndCloseDummyEMF(PersistenceProvider provider) {
DataSource dummyDataSource = new DummyDataSource();
punit.setJtaDataSource(dummyDataSource);
punit.setNonJtaDataSource(dummyDataSource);
try {
EntityManagerFactory emf = provider.createContainerEntityManagerFactory(punit, null);
emf.close();
} catch (Exception e) {
LOGGER.debug("Error while creating the Dummy EntityManagerFactory to allow weaving.", e);
}
punit.setJtaDataSource(null);
punit.setNonJtaDataSource(null);
}
示例7: replaceDefaultDBPassword
import javax.persistence.EntityManagerFactory; //导入方法依赖的package包/类
public void replaceDefaultDBPassword() throws Exception {
try (Connection connection = getSQLConnection();
PreparedStatement changePasswordStatement = connection.prepareStatement("ALTER USER " + this.connectionParams.getLogin().toUpperCase() + " SET PASSWORD ?")) {
connection.setAutoCommit(false);
String newPassword = RandomStringUtils.randomAscii(32);
try {
// change password in DB
changePasswordStatement.setString(1, newPassword);
changePasswordStatement.execute();
// put password in keystore
this.connectionParams.updatePassword(newPassword);
connection.commit();
} catch (Exception ex) {
log.error("Error while changing DB password.", ex);
try {
connection.rollback();
} catch (Exception he) {
log.error("Error rolling back transaction", he);
}
throw ex;
}
EntityManagerFactory oldEmf = this.emf;
// reinitialize session factory
start(this.properties);
try {
oldEmf.close();
} catch (PersistenceException pe) {
// Ignore this
}
}
}
示例8: selectSqlNativoTest
import javax.persistence.EntityManagerFactory; //导入方法依赖的package包/类
@Test
public void selectSqlNativoTest() {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("db1start");
EntityManager manager = factory.createEntityManager();
Query query = manager.createNativeQuery("Select * from cidade c");
List<Cidade> cidades = query.getResultList();
factory.close();
}
示例9: main
import javax.persistence.EntityManagerFactory; //导入方法依赖的package包/类
public static void main(String[] args) {
EntityManagerFactory entityManagerFactory = Persistence
.createEntityManagerFactory("pl.edu.bogdan.training.db.entity");
EntityManager em = entityManagerFactory.createEntityManager();
// begining of transaction
em.getTransaction().begin();
// Object used to create more sophisticated conditions
CriteriaBuilder cb = em.getCriteriaBuilder();
// this object is used to create query object
CriteriaQuery<Role> cq = cb.createQuery(Role.class);
// We set the root of query (we will use this root to getting values of columns)
Root<Role> from = cq.from(Role.class);
// we composing query using cb object
cq.where(cb.equal(from.get("name"), "ordinary"));
// creating query and executing it
TypedQuery<Role> tq = em.createQuery(cq);
List<Role> roles = tq.getResultList();
for (Role role : roles) {
System.out.println(role.getName());
}
em.getTransaction().commit();
em.close();
entityManagerFactory.close();
}
示例10: main
import javax.persistence.EntityManagerFactory; //导入方法依赖的package包/类
public static void main(String[] args) {
EntityManagerFactory entityManagerFactory
= Persistence.createEntityManagerFactory("pl.edu.bogdan.training.db.entity");
EntityManager em = entityManagerFactory.createEntityManager();
em.getTransaction().begin();
User user = new User("Paweł", "Bogdan", 28, null);
em.persist(user);
em.getTransaction().commit();
em.close();
entityManagerFactory.close();
}
示例11: metodoTest
import javax.persistence.EntityManagerFactory; //导入方法依赖的package包/类
@Test
public void metodoTest() {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("db1start");
EntityManager manager = factory.createEntityManager();
Cidade cidade = new Cidade();
cidade.setNome("Maringa");
manager.getTransaction().begin();
manager.persist(cidade);
manager.getTransaction().commit();
factory.close();
}
示例12: closeQuietly
import javax.persistence.EntityManagerFactory; //导入方法依赖的package包/类
static void closeQuietly(final EntityManagerFactory emf) {
if (emf != null) {
try {
emf.close();
} catch (final Exception ex) {
LOG.warn("Error closing entity manager factory: {}", ex.getMessage(), ex);
}
}
}
示例13: closeEntityManagerFactory
import javax.persistence.EntityManagerFactory; //导入方法依赖的package包/类
private static void closeEntityManagerFactory(final EntityManagerFactory emf) {
try {
if (emf != null && emf.isOpen()) {
emf.close();
}
} catch (final Exception ex) {
LOG.warn(ex.getMessage(), ex);
}
}
示例14: contextDestroyed
import javax.persistence.EntityManagerFactory; //导入方法依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public void contextDestroyed(ServletContextEvent sce) {
JPAEMFProvider.setEmf(null);
EntityManagerFactory emf = (EntityManagerFactory) sce
.getServletContext().getAttribute("my.application.emf");
if (emf != null) {
emf.close();
}
}
示例15: main
import javax.persistence.EntityManagerFactory; //导入方法依赖的package包/类
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("mysql-testdb");
emf.close();
}