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:DataLayer.CtrlJugadorDB.java

@Override
public Boolean existsE(String email) {
    AnnotationConfiguration config = new AnnotationConfiguration();
    config.addAnnotatedClass(Partida.class);
    config.addAnnotatedClass(Casella.class);
    config.addAnnotatedClass(Jugador.class);
    config.addAnnotatedClass(UsuariRegistrat.class);
    config.addAnnotatedClass(Joc2048.class);
    config.configure("hibernate.cfg.xml");
    SessionFactory factory = config.buildSessionFactory();
    Session session = factory.getCurrentSession();
    session.beginTransaction();/*from w ww .j  av a 2 s.  c  om*/
    List<Jugador> l = session.createQuery("from Jugador where email = :em").setParameter("em", email).list();
    session.getTransaction().commit();
    factory.close();
    return !l.isEmpty();
}

From source file:DataLayer.CtrlJugadorDB.java

@Override
public Set<Jugador> all() {
    AnnotationConfiguration config = new AnnotationConfiguration();
    config.addAnnotatedClass(Partida.class);
    config.addAnnotatedClass(Casella.class);
    config.addAnnotatedClass(Jugador.class);
    config.addAnnotatedClass(UsuariRegistrat.class);
    config.addAnnotatedClass(Joc2048.class);
    config.configure("hibernate.cfg.xml");
    SessionFactory factory = config.buildSessionFactory();
    Session session = factory.getCurrentSession();
    session.beginTransaction();//from   w w w . j ava2s  .  c o m
    List<Jugador> l = session.createQuery("from Jugador").list();
    session.getTransaction().commit();
    factory.close();
    Set<Jugador> r = new HashSet();
    for (Jugador j : l) {
        r.add(j);
    }
    return r;
}

From source file:DataLayer.CtrlPartidaDB.java

@Override
public Partida get(Integer idPartida) throws Exception {
    AnnotationConfiguration config = new AnnotationConfiguration();
    config.addAnnotatedClass(Partida.class);
    config.addAnnotatedClass(Casella.class);
    config.addAnnotatedClass(Jugador.class);
    config.addAnnotatedClass(UsuariRegistrat.class);
    config.addAnnotatedClass(Joc2048.class);
    config.configure("hibernate.cfg.xml");
    SessionFactory factory = config.buildSessionFactory();
    Session session = factory.getCurrentSession();
    session.beginTransaction();/*from  w w w .  j a v a2  s . co  m*/
    List<Partida> l = session.createQuery("from Partida where idPartida = :idP").setParameter("idP", idPartida)
            .list();
    session.getTransaction().commit();
    factory.close();
    if (!l.isEmpty()) {
        return l.get(0);
    }
    throw new Exception("partidaNoExisteix");
}

From source file:DataLayer.CtrlPartidaDB.java

@Override
public boolean exists(Integer idPartida) {
    AnnotationConfiguration config = new AnnotationConfiguration();
    config.addAnnotatedClass(Partida.class);
    config.addAnnotatedClass(Casella.class);
    config.addAnnotatedClass(Jugador.class);
    config.addAnnotatedClass(UsuariRegistrat.class);
    config.addAnnotatedClass(Joc2048.class);
    config.configure("hibernate.cfg.xml");
    SessionFactory factory = config.buildSessionFactory();
    Session session = factory.getCurrentSession();
    session.beginTransaction();//from   w w  w .j  a va  2 s. c  om
    List<Partida> l = session.createQuery("from Partida where idPartida = :idP").setParameter("idP", idPartida)
            .list();
    session.getTransaction().commit();
    factory.close();
    return !l.isEmpty();
}

From source file:DataLayer.CtrlPartidaDB.java

@Override
public Set<Partida> all() {
    AnnotationConfiguration config = new AnnotationConfiguration();
    config.addAnnotatedClass(Partida.class);
    config.addAnnotatedClass(Casella.class);
    config.addAnnotatedClass(Jugador.class);
    config.addAnnotatedClass(UsuariRegistrat.class);
    config.addAnnotatedClass(Joc2048.class);
    config.configure("hibernate.cfg.xml");
    SessionFactory factory = config.buildSessionFactory();
    Session session = factory.getCurrentSession();
    session.beginTransaction();/*  www.j  a  va  2 s  .  com*/
    List<Partida> l = session.createQuery("from Partida").list();
    session.getTransaction().commit();
    factory.close();
    Set<Partida> r = new HashSet();
    for (Partida p : l) {
        r.add(p);
    }
    return r;
}

From source file:DataLayer.CtrlUsuariRegistratDB.java

@Override
public UsuariRegistrat get(String username) throws Exception {
    AnnotationConfiguration config = new AnnotationConfiguration();
    config.addAnnotatedClass(Partida.class);
    config.addAnnotatedClass(Casella.class);
    config.addAnnotatedClass(Jugador.class);
    config.addAnnotatedClass(UsuariRegistrat.class);
    config.addAnnotatedClass(Joc2048.class);
    config.configure("hibernate.cfg.xml");
    SessionFactory factory = config.buildSessionFactory();
    Session session = factory.getCurrentSession();
    session.beginTransaction();//from  w w  w.j ava 2  s . c  o  m
    List<UsuariRegistrat> l = session.createQuery("from UsuariRegistrat where username = :usr")
            .setParameter("usr", username).list();
    session.getTransaction().commit();
    factory.close();
    if (!l.isEmpty()) {
        return l.get(0);
    }
    throw new Exception("usernameNoExisteix"); //To change body of generated methods, choose Tools | Templates.
}

From source file:DataLayer.CtrlUsuariRegistratDB.java

@Override
public Boolean exists(String username) {
    AnnotationConfiguration config = new AnnotationConfiguration();
    config.addAnnotatedClass(Partida.class);
    config.addAnnotatedClass(Casella.class);
    config.addAnnotatedClass(Jugador.class);
    config.addAnnotatedClass(UsuariRegistrat.class);
    config.addAnnotatedClass(Joc2048.class);
    config.configure("hibernate.cfg.xml");
    SessionFactory factory = config.buildSessionFactory();
    Session session = factory.getCurrentSession();
    session.beginTransaction();/*from ww  w.j av  a2 s.c  o m*/
    List<UsuariRegistrat> l = session.createQuery("from UsuariRegistrat where username = :usr")
            .setParameter("usr", username).list();
    session.getTransaction().commit();
    factory.close();
    return !l.isEmpty();
}

From source file:DataLayer.CtrlUsuariRegistratDB.java

@Override
public Set<UsuariRegistrat> all() {
    AnnotationConfiguration config = new AnnotationConfiguration();
    config.addAnnotatedClass(Partida.class);
    config.addAnnotatedClass(Casella.class);
    config.addAnnotatedClass(Jugador.class);
    config.addAnnotatedClass(UsuariRegistrat.class);
    config.addAnnotatedClass(Joc2048.class);
    config.configure("hibernate.cfg.xml");
    SessionFactory factory = config.buildSessionFactory();
    Session session = factory.getCurrentSession();
    session.beginTransaction();/*  w ww .  j av  a 2 s  .c  o  m*/
    List<UsuariRegistrat> l = session.createQuery("from UsuariRegistrat").list();
    session.getTransaction().commit();
    factory.close();
    Set<UsuariRegistrat> r = new HashSet();
    for (UsuariRegistrat uR : l) {
        r.add(uR);
    }
    return r;
}

From source file:de.decidr.model.logging.DefaultLogger.java

License:Apache License

/**
 * Fetches the global log level from the database.
 * //from www  . j  a v  a  2s .co  m
 * (we cannot instantiate any transaction coordinator because it might use
 * the default logger, creating a cyclic dependency)
 * 
 * @return log level
 */
private static Level getGlobalLogLevel() {
    Level result = Level.DEBUG;
    Session session = null;
    SessionFactory sessionFactory = null;
    try {
        sessionFactory = new Configuration().configure().buildSessionFactory();
        session = sessionFactory.openSession();

        Transaction tx = session.beginTransaction();
        try {
            result = Level.toLevel((String) session.createQuery("select s.logLevel from SystemSettings s")
                    .setMaxResults(1).uniqueResult());
            tx.commit();
        } catch (Throwable t) {
            tx.rollback();
        }
    } finally {
        if (session != null) {
            try {
                session.close();
            } catch (Throwable t) {
                // ignore
            }
        }
        if (sessionFactory != null) {
            try {
                sessionFactory.close();
            } catch (Throwable t) {
                // ignore
            }
        }
    }

    return result;
}

From source file:de.decidr.model.webservice.helper.DBAccess.java

License:Apache License

/**
 * @param dwfmID/*from   w ww. j  a  v a 2 s.  c  om*/
 * @param invokeNodeId
 * @return gets the relevatn DWDL and the relevant WSDL file for the proxy-service from the Database
 */
public DbDataBean getDBData(long dwfmID, long invokeNodeId) {

    DbDataBean dataBean = new DbDataBean();

    SessionFactory sf = new Configuration().configure().buildSessionFactory();

    // actial session
    Session se = sf.getCurrentSession();

    try {
        // save transaction to avoid NullPointerException on error handling.
        Transaction tr = null;

        try {
            // initialize new transaction
            tr = se.beginTransaction();

            execDeployedWorkflowModelQuery(dwfmID, dataBean, se);
            execFileQuery(getFileIDFromDwdl(dataBean, invokeNodeId), dataBean, se);
            execServerLoadViewQuery(ServerTypeEnum.Ode.toString(), dataBean, se);

            se.getTransaction().commit();

        } catch (Throwable e) {
            // if no transaction has been initialized try rollback

            if (tr != null) {
                tr.rollback();
                e.printStackTrace();
            }
        }
    } finally {
        // try to close session if this does not work 
        // at least try to close the transaction.
        try {
            if (se.isOpen()) {
                se.close();
            }
            if (!sf.isClosed()) {
                sf.close();
            }
        } catch (Throwable e) {
            se.disconnect();
        }
    }

    return dataBean;
}