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:com.uva.jobportal.dao.ProfileDAOImpl.java

@Override
public Profile viewProfile(int id) {
    Profile profile = null;/*  ww w . ja  v a  2s  .  co  m*/
    try {
        SessionFactory sf = new Configuration().configure().buildSessionFactory();
        Session s = sf.openSession();
        profile = (Profile) s.get(Profile.class, id);
        s.getTransaction().commit();
        s.close();
        sf.close();
    } catch (HibernateException e) {
    }
    return profile;
}

From source file:com.uva.jobportal.dao.ProfileDAOImpl.java

@Override
public Profile editProfile(Profile profile) {
    try {/*from  ww w  . jav a 2 s .co  m*/
        SessionFactory sf = new Configuration().configure().buildSessionFactory();
        Session s = sf.openSession();
        s.beginTransaction();
        s.update(profile);
        s.getTransaction().commit();
        s.close();
        sf.close();
    } catch (HibernateException e) {
    }
    return profile;
}

From source file:com.vmware.photon.controller.apife.db.MigrationTest.java

License:Open Source License

@Test
public void testMigrations() throws SQLException, LiquibaseException {
    try (Connection connection = DriverManager.getConnection("jdbc:h2:mem:migrations", "sa", "")) {
        Liquibase liquibase = new Liquibase("migrations.xml", new ClassLoaderResourceAccessor(),
                new JdbcConnection(connection));
        liquibase.update("");

        Configuration configuration = new Configuration();

        Reflections reflections = new Reflections("com.vmware.photon.controller.apife.entities");
        Set<Class<?>> classes = reflections.getTypesAnnotatedWith(Entity.class);

        Reflections commonReflections = new Reflections("com.vmware.photon.controller.api.common");
        classes.addAll(commonReflections.getTypesAnnotatedWith(Entity.class));
        for (final Class<?> clazz : classes) {
            configuration.addAnnotatedClass(clazz);
        }/*ww w  .ja  va  2  s  .  c  o m*/

        configuration.setProperty(AvailableSettings.CURRENT_SESSION_CONTEXT_CLASS, "thread");
        configuration.setProperty(AvailableSettings.DIALECT, "org.hibernate.dialect.H2Dialect");
        configuration.setProperty(AvailableSettings.DRIVER, "org.h2.Driver");
        configuration.setProperty(AvailableSettings.URL, "jdbc:h2:mem:migrations");
        configuration.setProperty(AvailableSettings.USER, "sa");
        configuration.setProperty(AvailableSettings.PASS, "");
        configuration.setProperty(AvailableSettings.HBM2DDL_AUTO, "validate");
        configuration.setNamingStrategy(ImprovedNamingStrategy.INSTANCE);

        ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
                .applySettings(configuration.getProperties()).build();

        SessionFactory factory = configuration.buildSessionFactory(serviceRegistry);
        factory.close();
    }
}

From source file:com.wideplay.warp.persist.hibernate.ManualLocalReadOnlyTransactionsTest.java

License:Apache License

@AfterClass
void post() {/*  ww  w .j a v  a  2 s. c  o  m*/
    final SessionFactory sessionFactory = injector.getInstance(SessionFactory.class);
    sessionFactory.close();

    ManagedSessionContext.unbind(sessionFactory);
}

From source file:com.zanvork.guildhub.model.dao.HibernateMySQLDAO.java

public static void closeSession(String dbconnect) {
    if (sessionFactory != null && sessionFactory.containsKey(dbconnect)) {
        SessionFactory sf = sessionFactory.get(dbconnect);
        sf.close();
        sessionFactory.remove(dbconnect);

        try {//w ww .  ja v a2  s .  c  o m

        } catch (HibernateException ex) {
            throw new ExceptionInInitializerError(ex);
        }
    }
}

From source file:control.Demo_HibernateFull.java

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

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

    //open the session
    session.beginTransaction();

    //insert
    //Date date = new SimpleDateFormat("yyyy-MM-dd").parse("2018-05-21");
    //Livraison livraison = new Livraison(new BigDecimal(106), date);
    Client client = new Client(new BigDecimal(10), "Luc Sansom", "(999)999-9999");

    Date date2 = new SimpleDateFormat("yyyy-MM-dd").parse("2000-06-01");
    Commande commande = new Commande(new BigDecimal(1), client, date2);

    Article article = new Article(new BigDecimal(10), new BigDecimal(5), new BigDecimal(10));

    LignecommandeId lignecommandeId = new LignecommandeId(new BigDecimal(1), new BigDecimal(10));

    Lignecommande lignecommande = new Lignecommande(lignecommandeId, commande, article, new BigDecimal(32));

    Date date = new SimpleDateFormat("yyyy-MM-dd").parse("2018-05-21");
    Livraison livraison = new Livraison(new BigDecimal(107), date);

    DetaillivraisonId detaillivraisonId = new DetaillivraisonId(new BigDecimal(107), new BigDecimal(1),
            new BigDecimal(10));
    DetaillivraisonId detaillivraisonId2 = new DetaillivraisonId(new BigDecimal(109), new BigDecimal(1),
            new BigDecimal(10));

    Detaillivraison detaillivraison = new Detaillivraison(detaillivraisonId, livraison, lignecommande,
            new BigDecimal(3));
    Detaillivraison detaillivraison2 = new Detaillivraison(detaillivraisonId2, livraison, lignecommande,
            new BigDecimal(10));

    Set livraisonset = new HashSet();

    livraisonset.add(detaillivraison);
    livraisonset.add(detaillivraison2);

    Date date3 = new SimpleDateFormat("yyyy-MM-dd").parse("2018-02-22");
    Livraison mylivraison = new Livraison(new BigDecimal(109), date3, livraisonset);

    Detaillivraison detaillivraison3 = new Detaillivraison(detaillivraisonId2, mylivraison, lignecommande,
            new BigDecimal(11));

    mylivraison.setDetaillivraisons(livraisonset);

    //session.save(livraison);
    //session.save(detaillivraison);
    //session.save(mylivraison);
    //session.update(mylivraison);
    //session.merge(mylivraison);
    session.save(detaillivraison3);
    //livraison.setDetaillivraisons(livraisonset);

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

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

From source file:controller.InsertAttendance.java

public static void main(String[] args) {
    //configure cfg xml file
    Configuration cf = new Configuration();
    cf.configure("xmlFiles/hibernate.cfg.xml");

    //build session factory
    SessionFactory sf = cf.buildSessionFactory();
    //get session object
    Session session = sf.openSession();//from  w w w.  j  a v  a2s  . c  om
    //get Transaction object
    Transaction tr = session.beginTransaction();

    //STUDENT ATTANDENCE
    StudentAttendance studentAttendance = new StudentAttendance();

    studentAttendance.setAttendance("P");
    studentAttendance.setLectureDate("20/7/2013");
    studentAttendance.setRollNum("13_CS_19");
    studentAttendance.setTheoryOrPractical("practical");
    studentAttendance.setSubject("BEE");
    studentAttendance.setSemester("2nd");
    studentAttendance.setBatch("13");
    studentAttendance.setDepart("Computer System");
    studentAttendance.setSemesterState("no");

    //INSERT ATTENDANCE OBJECT
    session.save(studentAttendance);
    tr.commit();
    session.evict(studentAttendance);

    //close session and session factory
    session.close();
    sf.close();

}

From source file:controller.InsertSubjects.java

public static void main(String[] args) {
    //configure cfg xml file
    Configuration cf = new Configuration();
    cf.configure("xmlFiles/hibernate.cfg.xml");

    //build session factory
    SessionFactory sf = cf.buildSessionFactory();
    //get session object
    Session session = sf.openSession();//  ww w  .  ja va2 s  .co  m
    //get Transaction object
    Transaction tr = session.beginTransaction();

    //SUBJECTS OF CS
    //          Subjects subjects = new Subjects();
    //          subjects.setDepartment("comptuer system");
    //          subjects.setSemester("1st");
    //          subjects.setSubjectName("Calculus");
    //        //INSERT ATTENDANCE OBJECT
    //        session.save(subjects);
    //        tr.commit();
    //        session.evict(subjects);
    //close session and session factory
    session.close();
    sf.close();

}

From source file:controllers.personTypes.PersonTypeController.java

public List<PersonType> ConsultarPorNombre(String pNombre) {
    pNombre = pNombre + '%';
    //Prep work/*  w w w .  j  ava 2  s  .c  o  m*/
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    Session session = sessionFactory.getCurrentSession();

    Transaction tx = session.beginTransaction();
    Query query = session.createQuery("from PersonType where personTypeName like :nombre ");
    query.setParameter("nombre", pNombre);
    List<PersonType> personTypeList = query.list();
    tx.commit();
    sessionFactory.close();
    return personTypeList;
}

From source file:crud.Person_crud.java

/**
* Retrieves all persons// w  ww.  j a va2  s .c om
*
* @return a list of persons 
*/
public List<Person> ShowAll() {
    //Create session factory object  
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    //getting session object from session factory  
    Session session = sessionFactory.openSession();
    //getting transaction object from session object  
    session.beginTransaction();
    Query query = session.createQuery("from Person");
    List<Person> persons = query.list();
    /*for(Person person : persons)  
    {  
    System.out.println("Person Id: "+person.getId()+", Name: "+person.getName()+", Surname: "+person.getSurname()+
            ",Computer: "+person.getComputer().getId());  
    } */
    session.getTransaction().commit();
    sessionFactory.close();

    return persons;
}