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.jboss.samples.rs.webservices.MafecommApplication.java

License:Apache License

public ArrayList<Product> getProducts() throws Exception {
    SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
    Session session = sessionFactory.openSession();
    session.beginTransaction();//  www. j  av  a2 s  .com

    Query query = session.createQuery("from Product");
    ArrayList<Product> products = (ArrayList<Product>) query.list();

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

    return products;
}

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

License:Apache License

public void deleteProduct(Product product) throws Exception {
    SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
    Session session = sessionFactory.openSession();
    session.beginTransaction();/*w  ww.j a v a 2  s . co m*/

    session.delete(product);

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

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

License:Apache License

public void deleteProductById(int productId) throws Exception {
    SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
    Session session = sessionFactory.openSession();
    session.beginTransaction();//from w ww  . j av  a2 s  .c  o m

    Query query = session.createQuery("DELETE FROM Product WHERE productId = :productId");
    query.setParameter("productId", productId);

    query.executeUpdate();

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

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

License:Apache License

public void addBnB(BnB bnb) throws Exception {
    SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
    Session session = sessionFactory.openSession();
    session.beginTransaction();// w w w.  j  av  a  2 s. c o m

    session.saveOrUpdate(bnb);

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

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

License:Apache License

public void editBnB(BnB bnb) throws Exception {
    SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
    Session session = sessionFactory.openSession();
    session.beginTransaction();/* w  w  w . j  ava2 s .  com*/

    session.update(bnb);

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

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

License:Apache License

public ArrayList<BnB> getBnBs() throws Exception {
    SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
    Session session = sessionFactory.openSession();
    session.beginTransaction();/*ww  w  .  j  a v a  2  s .  c  o  m*/

    Query query = session.createQuery("from BnB");
    ArrayList<BnB> bnbs = (ArrayList<BnB>) query.list();

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

    return bnbs;
}

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

License:Apache License

public void deleteBnb(BnB bnb) throws Exception {
    SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
    Session session = sessionFactory.openSession();
    session.beginTransaction();/*from w  ww  .  ja  va2  s . c o  m*/

    session.delete(bnb);

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

From source file:org.mgenterprises.openbooks.saving.server.setup.ServerSetup.java

License:Open Source License

private void setupInitialDB() {
    try {/*from   w  w  w. ja v a  2 s  .  c o  m*/
        //Start Hibernate
        HibernateConfigurationLoader hibernateConfigurationLoader = new HibernateConfigurationLoader(
                hibernateProperties);
        SessionFactory sessionFactory = hibernateConfigurationLoader.getConfiguration().buildSessionFactory();

        //Add the admin user
        //Let's use what we have built already
        UserManager userManager = new HibernateBackedUserManager(sessionFactory);
        UserProfile userProfile = new UserProfile();
        //Set the username
        userProfile.setUsername("admin");
        //Hash and set password
        userProfile.setPasswordHash(
                BCrypt.hashpw(String.copyValueOf(passwordField.getPassword()), BCrypt.gensalt()));
        //Persist User
        userManager.addUser(userProfile);
        //Cleanup
        sessionFactory.close();

    } catch (Throwable ex) {
        Logger.getLogger(ServerSetup.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:org.n52.sos.ds.hibernate.AbstractSessionFactoryProvider.java

License:Open Source License

@Override
public void cleanup() {
    lock.lock();//from  w  ww  . ja va2s .  com
    SessionFactory sessionFactory = getSessionFactory();
    try {
        if (getSessionFactory() != null) {
            try {
                if (SessionFactoryImpl.class.isInstance(sessionFactory) && Stoppable.class
                        .isInstance(((SessionFactoryImpl) sessionFactory).getConnectionProvider())) {
                    ((Stoppable) ((SessionFactoryImpl) sessionFactory).getConnectionProvider()).stop();
                }
                sessionFactory.close();
                LOG.info("Connection provider closed successfully!");
            } catch (HibernateException he) {
                LOG.error("Error while closing connection provider!", he);
            }
        }
    } finally {
        sessionFactory = null;
        lock.unlock();
    }
}

From source file:org.nanocontainer.persistence.hibernate.annotations.ConstructableAnnotationConfigurationTestCase.java

License:Open Source License

/**
 * Works Hibernate's configuration by attempting a write to the 'database'.  With the latest
 * hiberanates, the configuration isn't really built until the session factory is built, and even
 * then, some of the data doesn't exist until a write occurs.
 * @param config// w  ww .  j  a  va  2s . c  o  m
 */
private void attemptWrite(ConstructableAnnotationConfiguration config) {
    Pojo pojo = new Pojo();
    pojo.setFoo("Foo!");

    SessionFactory sessionFactory = config.buildSessionFactory();
    Session session = null;
    try {

        session = sessionFactory.openSession();
        Integer result = (Integer) session.save(pojo);
        assertNotNull(result);
        session.close();

        session = sessionFactory.openSession();
        Pojo pojo2 = (Pojo) session.load(Pojo.class, result);
        assertNotNull(pojo);
        assertEquals(pojo.getId(), pojo2.getId());
        assertEquals(pojo.getFoo(), pojo2.getFoo());
    } finally {
        if (session != null && session.isOpen()) {
            session.close();
        }

        sessionFactory.close();
    }
}