Example usage for org.hibernate SessionFactory close

List of usage examples for org.hibernate SessionFactory close

Introduction

In this page you can find the example usage for org.hibernate SessionFactory close.

Prototype

void close() throws HibernateException;

Source Link

Document

Destroy this SessionFactory and release all resources (caches, connection pools, etc).

Usage

From source file:org.infinispan.test.hibernate.cache.AbstractGeneralDataRegionTest.java

License:LGPL

protected void withSessionFactoriesAndRegions(int num, SFRConsumer consumer) throws Exception {
    StandardServiceRegistryBuilder ssrb = createStandardServiceRegistryBuilder()
            .applySetting(AvailableSettings.CACHE_REGION_FACTORY, TestInfinispanRegionFactory.class.getName());
    Properties properties = CacheTestUtil.toProperties(ssrb.getSettings());
    List<StandardServiceRegistry> registries = new ArrayList<>();
    List<SessionFactory> sessionFactories = new ArrayList<>();
    List<GeneralDataRegion> regions = new ArrayList<>();
    for (int i = 0; i < num; ++i) {
        StandardServiceRegistry registry = ssrb.build();
        registries.add(registry);/*from   ww w.java  2 s  .  c om*/

        SessionFactory sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
        sessionFactories.add(sessionFactory);

        InfinispanRegionFactory regionFactory = (InfinispanRegionFactory) registry
                .getService(RegionFactory.class);
        GeneralDataRegion region = (GeneralDataRegion) createRegion(regionFactory,
                getStandardRegionName(REGION_PREFIX), properties, null);
        regions.add(region);
    }
    waitForClusterToForm(regions);
    try {
        consumer.accept(sessionFactories, regions);
    } finally {
        for (SessionFactory sessionFactory : sessionFactories) {
            sessionFactory.close();
        }
        for (StandardServiceRegistry registry : registries) {
            StandardServiceRegistryBuilder.destroy(registry);
        }
    }
}

From source file:org.infinispan.test.hibernate.cache.commons.AbstractGeneralDataRegionTest.java

License:LGPL

protected void withSessionFactoriesAndRegions(int num, SFRConsumer consumer) throws Exception {
    StandardServiceRegistryBuilder ssrb = createStandardServiceRegistryBuilder().applySetting(
            AvailableSettings.CACHE_REGION_FACTORY,
            TestRegionFactoryProvider.load().getRegionFactoryClass().getName());
    Properties properties = CacheTestUtil.toProperties(ssrb.getSettings());
    List<StandardServiceRegistry> registries = new ArrayList<>();
    List<SessionFactory> sessionFactories = new ArrayList<>();
    List<InfinispanBaseRegion> regions = new ArrayList<>();
    for (int i = 0; i < num; ++i) {
        StandardServiceRegistry registry = ssrb.build();
        registries.add(registry);/*from ww  w.j  a  v a  2s .  co  m*/

        SessionFactory sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
        sessionFactories.add(sessionFactory);

        TestRegionFactory regionFactory = TestRegionFactoryProvider.load()
                .wrap(registry.getService(RegionFactory.class));
        InfinispanBaseRegion region = createRegion(regionFactory, REGION_PREFIX + "/who-cares");
        regions.add(region);
    }
    waitForClusterToForm(regions);
    try {
        consumer.accept(sessionFactories, regions);
    } finally {
        for (SessionFactory sessionFactory : sessionFactories) {
            sessionFactory.close();
        }
        for (StandardServiceRegistry registry : registries) {
            StandardServiceRegistryBuilder.destroy(registry);
        }
    }
}

From source file:org.infinispan.test.hibernate.cache.commons.stress.CorrectnessTestCase.java

License:LGPL

@AfterClassOnce
public void afterClass() {
    for (SessionFactory sf : sessionFactories) {
        if (sf != null)
            sf.close();
    }/*from w w w  .j av  a  2 s .  c o m*/
    TestResourceTracker.testFinished(getClass().getSimpleName());
}

From source file:org.infinispan.test.hibernate.cache.commons.tm.JBossStandaloneJtaExampleTest.java

License:LGPL

@Test
public void testPersistAndLoadUnderJta() throws Exception {
    Item item;/* w  w w .  j a  v  a2  s . c o m*/
    SessionFactory sessionFactory = buildSessionFactory();
    try {
        UserTransaction ut = (UserTransaction) ctx.lookup("UserTransaction");
        ut.begin();
        try {
            Session session = sessionFactory.openSession();
            assertEquals(TransactionStatus.ACTIVE, session.getTransaction().getStatus());
            item = new Item("anItem", "An item owned by someone");
            session.persist(item);
            // IMO the flush should not be necessary, but session.close() does not flush
            // and the item is not persisted.
            session.flush();
            session.close();
        } catch (Exception e) {
            ut.setRollbackOnly();
            throw e;
        } finally {
            if (ut.getStatus() == Status.STATUS_ACTIVE)
                ut.commit();
            else
                ut.rollback();
        }

        ut = (UserTransaction) ctx.lookup("UserTransaction");
        ut.begin();
        try {
            Session session = sessionFactory.openSession();
            assertEquals(TransactionStatus.ACTIVE, session.getTransaction().getStatus());
            Item found = (Item) session.load(Item.class, item.getId());
            Statistics stats = session.getSessionFactory().getStatistics();
            log.info(stats.toString());
            assertEquals(item.getDescription(), found.getDescription());
            assertEquals(0, stats.getSecondLevelCacheMissCount());
            assertEquals(1, stats.getSecondLevelCacheHitCount());
            session.delete(found);
            // IMO the flush should not be necessary, but session.close() does not flush
            // and the item is not deleted.
            session.flush();
            session.close();
        } catch (Exception e) {
            ut.setRollbackOnly();
            throw e;
        } finally {
            if (ut.getStatus() == Status.STATUS_ACTIVE)
                ut.commit();
            else
                ut.rollback();
        }

        ut = (UserTransaction) ctx.lookup("UserTransaction");
        ut.begin();
        try {
            Session session = sessionFactory.openSession();
            assertEquals(TransactionStatus.ACTIVE, session.getTransaction().getStatus());
            assertNull(session.get(Item.class, item.getId()));
            session.close();
        } catch (Exception e) {
            ut.setRollbackOnly();
            throw e;
        } finally {
            if (ut.getStatus() == Status.STATUS_ACTIVE)
                ut.commit();
            else
                ut.rollback();
        }
    } finally {
        if (sessionFactory != null)
            sessionFactory.close();
    }

}

From source file:org.jboss.samples.rs.webservices.MafecommApplication.java

License:Apache License

public void addOrder(Order order) throws Exception {
    SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
    Session session = sessionFactory.openSession();
    session.beginTransaction();/*from  www  . j av  a  2 s  . c  om*/

    session.save(order);

    session.getTransaction().commit();
    session.close();
    sessionFactory.close();
}

From source file:org.jboss.samples.rs.webservices.MafecommApplication.java

License:Apache License

public void editOrder(Order order) throws Exception {
    SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
    Session session = sessionFactory.openSession();
    session.beginTransaction();//w  w  w.  j  ava  2  s  .c om

    session.update(order);

    session.getTransaction().commit();
    session.close();
    sessionFactory.close();
}

From source file:org.jboss.samples.rs.webservices.MafecommApplication.java

License:Apache License

public ArrayList<Order> getOrders() throws Exception {
    SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
    Session session = sessionFactory.openSession();
    session.beginTransaction();//w w  w  .  j a v  a2s  . c  o  m

    Query query = session.createQuery("from Order");
    ArrayList<Order> orders = (ArrayList<Order>) query.list();

    session.close();
    sessionFactory.close();

    return orders;
}

From source file:org.jboss.samples.rs.webservices.MafecommApplication.java

License:Apache License

public void addProduct(Product product) throws Exception {
    SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
    Session session = sessionFactory.openSession();
    session.beginTransaction();//from  ww  w.  ja va  2s.co  m

    session.save(product);

    session.getTransaction().commit();
    session.close();
    sessionFactory.close();
}

From source file:org.jboss.samples.rs.webservices.MafecommApplication.java

License:Apache License

public void editProduct(Product product) throws Exception {
    SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
    Session session = sessionFactory.openSession();
    session.beginTransaction();/*from w  w  w . ja v a  2s  .c  o  m*/

    session.update(product);

    session.getTransaction().commit();
    session.close();
    sessionFactory.close();
}

From source file:org.jboss.samples.rs.webservices.MafecommApplication.java

License:Apache License

public Product getProduct(int productId) throws Exception {
    Product product;// ww  w.  j a  v a 2s .  c o  m

    SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
    Session session = sessionFactory.openSession();
    session.beginTransaction();

    Query query = session.createQuery("from Product P WHERE P.productId = :product_Id");
    query.setParameter("product_Id", productId);
    List<Product> results = (List<Product>) query.list();
    product = results.get(0);

    session.close();
    sessionFactory.close();

    return product;
}