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:de.lemo.dms.connectors.clix2010.ClixHibernateUtil.java

License:Open Source License

public static void closeSessionFactory(final DBConfigObject dbconfig) {
    final SessionFactory sessionFactory = ClixHibernateUtil.sessionFactories.remove(dbconfig);
    if (sessionFactory != null) {
        sessionFactory.close();
    }/*from  ww  w .  j  a v a 2 s.  c o  m*/
}

From source file:de.lemo.dms.connectors.lemo_0_8.HibernateUtil.java

License:Open Source License

public static void closeSessionFactory(final DBConfigObject dbconfig) {
    final SessionFactory sessionFactory = HibernateUtil.sessionFactories.remove(dbconfig);
    if (sessionFactory != null) {
        sessionFactory.close();
    }//from   ww  w.ja  v  a2 s  .co  m
}

From source file:demo_hibernate3.AppCtr.java

/**
 * @param args the command line arguments
 *///  www .j a  v  a  2 s .  co m
public static void main(String[] args) throws ParseException {
    SessionFactory sessionFactory = NewHibernateUtil.getSessionFactory();

    Session session = NewHibernateUtil.getSessionFactory().openSession();

    //open the session
    session.beginTransaction();

    //set date
    Date date = new SimpleDateFormat("yyyy-MM-dd").parse("2018-05-16");

    //insert
    Client myClient = new Client(new BigDecimal(7007), "James Bond", "514007007");
    Commande myCommande = new Commande(new BigDecimal(100100100), myClient, date);

    myCommande.setNocommande(new BigDecimal(700700700));

    //save the object
    session.save(myClient);
    session.save(myCommande);

    //update the object
    session.update(myCommande);

    //delete the object
    session.delete(myCommande);
    session.delete(myClient);

    //commit
    session.getTransaction().commit();

    //select
    Query query = session.createQuery("from Commande");

    List<Commande> resultat = query.list();

    for (Commande line : resultat) {
        System.out.println("NO commande: " + line.getNocommande() + " Date commande: " + line.getDatecommande()
                + " No client: " + line.getClient().getNomclient());
    }
    System.out.println("**************************************************************");
    //select 2
    Query query2 = session.createQuery("Select c.noclient, c.nomclient, count(noCommande)"
            + " from Client c, Commande m where c.noclient = m.client.noclient group by c.noclient, c.nomclient");

    List<Object[]> results = query2.list();

    for (Object[] line : results) {
        System.out.println("No client: " + line[0] + " Nom client: " + line[1] + " Nb commande " + line[2]);
    }

    if (session.isConnected()) {
        session.close();
    }
    if (!sessionFactory.isClosed()) {
        sessionFactory.close();
    }

}

From source file:DomainLayer.DomainControllers.CtrlJugarPartida.java

private void actualitzarDB(Partida p, Jugador jg, Boolean crea, Joc2048 joc) {
    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  a va2 s  . com*/
    if (crea) {
        session.save(p);
        session.update(joc);
    } else
        session.update(p);
    session.update(jg);
    Casella caselles[][] = p.getMatriu();
    for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < 4; ++j) {
            if (crea)
                session.save(caselles[i][j]);
            else
                session.update(caselles[i][j]);
        }
    }
    session.getTransaction().commit();
    factory.close();
}

From source file:DomainLayer.DomainModel.Joc2048.java

public static synchronized Joc2048 joc2048() {
    if (instance == null) {
        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   www . jav a2 s.  c o  m
        List<Joc2048> l = session.createQuery("from Joc2048").list();
        if (l.isEmpty()) {
            instance = new Joc2048();
            instance.setIdPartida(0);
            session.save(instance);
        } else
            instance = l.get(0);
        session.getTransaction().commit();
        factory.close();
    }
    return instance;
}

From source file:dvdrental.Find.java

public static void main(String[] args) {
    //session  // www .  j  a v a  2 s. c  om
    //1)Configuration
    Configuration cfg = new Configuration().configure();
    //2)SessionFactory
    StandardServiceRegistryBuilder srb = new StandardServiceRegistryBuilder()
            .applySettings(cfg.getProperties());
    StandardServiceRegistry sr = srb.build();

    SessionFactory sf = cfg.buildSessionFactory(sr);

    Session session = sf.openSession();
    Transaction tx = session.beginTransaction();

    Cjhlei n = (Cjhlei) session.get(Cjhlei.class, "bbc36063574036230157403626f80000");
    session.delete(n);

    tx.commit();
    session.close();
    sf.close();
}

From source file:edu.eci.cosw.jpa.sample.SimpleMainApp.java

License:Open Source License

public static void main(String a[]) {
    SessionFactory sf = getSessionFactory();
    Session s = sf.openSession();//from   w  w  w. jav a  2s  . c o  m
    Transaction tx = s.beginTransaction();

    tx.commit();
    s.close();
    sf.close();

}

From source file:edu.eci.cosw.test.ConnectionTest.java

@Test
public void connectionTest() {
    SessionFactory sf = null;
    try {//  w  ww  .j  av a  2 s.co m
        Configuration configuration = new Configuration();
        configuration.configure("hibernate.cfg.xml");
        ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
                .applySettings(configuration.getProperties()).buildServiceRegistry();
        sf = configuration.buildSessionFactory(serviceRegistry);
        Session s = sf.openSession();
        s.createQuery("from Pedido").list().size();
    } catch (Throwable e) {
        fail("Error en la configuracin de la conexin:" + e.getLocalizedMessage());
    } finally {
        if (sf != null) {
            sf.close();
        }
    }
}

From source file:eu.optimis.common.trec.db.ip.TrecIP2SPDAO.java

License:Apache License

public boolean addIP2SP(String serviceId, String componentId, String spId, String ipId, double serviceTime,
        double serviceRisk, double security, double reliability, double performance, double legal,
        double serviceTrust) throws Exception {
    Session session = null;/*www  .  ja  v  a 2 s  . co  m*/
    IpToSp ip2sp = new IpToSp();
    SessionFactory sf = HibernateUtil.getSessionFactory();
    try {
        session = sf.openSession();
        Transaction tx = session.beginTransaction();
        tx.begin();
        ip2sp.setServiceId(serviceId);
        ip2sp.setDate(new Date());
        ip2sp.setServiceComponent(getServiceComponent(componentId));
        ip2sp.setSpInfo(getSpInfo(spId));
        ip2sp.setIpInfo(getIpInfo(ipId));
        ip2sp.setServiceTime(serviceTime);
        ip2sp.setServiceRisk(serviceRisk);
        ip2sp.setSercurityAssessment(security);
        ip2sp.setPerformance(performance);
        ip2sp.setLegalOpeness(legal);
        ip2sp.setServiceTrust(serviceTrust);
        ip2sp.setServiceReliability(reliability);
        session.save(ip2sp);
        tx.commit();
        if (tx.wasCommitted()) {
            logger.info("Transaction commited");
        }
    } catch (Exception e) {
        logger.info("ERROR " + e.getMessage());
        throw e;
    } finally {
        if (session != null) {
            try {
                session.close();
            } catch (HibernateException e) {
            }
        }
    }
    sf.close();
    return true;
}

From source file:eu.optimis.common.trec.db.ip.TrecIP2SPDAO.java

License:Apache License

public boolean deleteIP2SPTrust(String serviceId) throws Exception {
    Session session = null;/*from  ww w . j  av a 2 s  .com*/
    SessionFactory sf = HibernateUtil.getSessionFactory();
    int result = 0;
    try {
        session = sf.openSession();
        Transaction tx = session.beginTransaction();
        tx.begin();
        Query query = session.createSQLQuery("DELETE FROM ip_to_sp WHERE service_id =:serviceId");
        query.setParameter("serviceId", serviceId);
        result = query.executeUpdate();
        tx.commit();
        sf.close();

    } catch (Exception e) {
        logger.error("ERROR " + e.getMessage());
        throw new Exception(e.toString());
    }

    if (result == 0)
        return false;

    return true;

}