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.lt.clienttracker.testmain.java

public static void main(String[] args) {
    SessionFactory sessFact = HibernateManager.getSessionFactory();
    Session sess = sessFact.openSession();
    List<client> events = sess.createQuery("from client").list();
    System.out.println(events.size());

    try {//from  ww  w .  ja va 2  s.  com
        //            System.out.println(events.get(events.size()-1).toString());
        sess.beginTransaction();
        com.lt.clienttracker.client obj = new client();
        obj.setId("Kevin2");
        obj.setAdmissionAge(10, 2);
        obj.setAdmissionYear(2014);
        obj.setAdmissionMonth(11);
        obj.setLGBTQ("T");

        //            client cur=events.get(0);
        sess.save(obj);
        diagnosis test = new diagnosis();
        test.setDescription("This is a code!");
        test.setCode("123.40");
        test.setCodeType("DSM-IV");
        test.setMonth(12);
        test.setYear(2014);
        test.setClient(obj);
        sess.save(test);

        /*            SASSI sassi=new SASSI();
                    sassi.setRiskLow();
                    sassi.setClient(cur);
                    sassi.setMonth(12);
                    sassi.setYear(2014);
                    sess.save(sassi);
                
                    ACE ace=new ACE();
                    ace.setScore(1);
                    ace.setClient(cur);
                    ace.setMonth(12);
                    ace.setYear(2014);
                    sess.save(ace);
                            
        //            sess.save(events.get(0));
        */
        //            sess.save(cur);
        sess.getTransaction().commit();
        sess.close();

    } catch (Exception e) {
        System.out.println(e.toString());
        sess.getTransaction().rollback();
        sess.close();
    }
    System.out.println("Hello World!");
    sessFact.close();
}

From source file:com.mangium.Moharto_Login_Check.java

public String valid() {
    System.out.println("-------------------Login Check ---------");
    String logincheck = "";

    Configuration cfg = new Configuration();
    cfg.configure("hibernate.cfg.xml");
    SessionFactory sf = cfg.buildSessionFactory();
    Session session = sf.openSession();/*  www.ja v a  2s  .  c  om*/
    Query q = session.createQuery("from com.mangium.Moharto_Login_Bean ");

    ArrayList list = (ArrayList) q.list();

    Iterator it = list.iterator();

    Moharto_Student_Register_Basic_Bean u = null;

    while (it.hasNext()) {

        u = (Moharto_Student_Register_Basic_Bean) it.next();

        if (u.equals(null)) {
            logincheck = "login";
            System.out.println("-------------------Login Failed---------");

            break;
        }

        if (user.getEmail().equals(u.getEmail())) {

            if (user.getPassword().equals(u.getPassword())) {

                System.out.println("-------------------Login Successfull---------");
                logincheck = "success";

            } else {
                logincheck = "login";
                System.out.println("-------------------Login Failed---------");
            }
        }

    }

    session.close();
    sf.close();
    return null;

}

From source file:com.manydesigns.portofino.persistence.Persistence.java

License:Open Source License

public synchronized void initModel() {
    if (setups != null) {
        logger.info("Cleaning up old setups");
        for (Map.Entry<String, HibernateDatabaseSetup> current : setups.entrySet()) {
            String databaseName = current.getKey();
            logger.info("Cleaning up old setup for: {}", databaseName);
            HibernateDatabaseSetup hibernateDatabaseSetup = current.getValue();
            try {
                SessionFactory sessionFactory = hibernateDatabaseSetup.getSessionFactory();
                sessionFactory.close();
            } catch (Throwable t) {
                logger.warn("Cannot close session factory for: " + databaseName, t);
            }//from   w w w .  j  av a  2  s  .com
        }
    }

    setups.clear();
    model.init();
    for (Database database : model.getDatabases()) {
        ConnectionProvider connectionProvider = database.getConnectionProvider();
        connectionProvider.init(databasePlatformsRegistry);
        if (connectionProvider.getStatus().equals(ConnectionProvider.STATUS_CONNECTED)) {
            HibernateConfig builder = new HibernateConfig(connectionProvider, configuration);
            String trueString = database.getTrueString();
            if (trueString != null) {
                builder.setTrueString("null".equalsIgnoreCase(trueString) ? null : trueString);
            }
            String falseString = database.getFalseString();
            if (falseString != null) {
                builder.setFalseString("null".equalsIgnoreCase(falseString) ? null : falseString);
            }
            Configuration configuration = builder.buildSessionFactory(database);
            SessionFactory sessionFactory = configuration.buildSessionFactory();

            HibernateDatabaseSetup setup = new HibernateDatabaseSetup(configuration, sessionFactory);
            String databaseName = database.getDatabaseName();
            setups.put(databaseName, setup);
        }
    }

    cacheResetListenerRegistry.fireReset(new CacheResetEvent(this));
}

From source file:com.mesut.elementcollection.Main.java

public static void main(String[] args) {
    Uye uye1 = new Uye();
    uye1.setAdi("Mehmet");
    uye1.setSoyadi("Kahraman");
    Adres adres1 = new Adres();
    adres1.setSehir("Diyarbakr");
    adres1.setIlce("Kayapnar");
    adres1.setMahalle("Diclekent");
    adres1.setCadde("Mahabad Bulvar");

    Adres adres2 = new Adres();
    adres2.setSehir("Ankara");
    adres2.setIlce("ankaya");
    adres2.setMahalle("Bahelievler");
    adres2.setCadde("7. Cadde");
    uye1.getAdresListesi().add(adres1);/*from www.  j  a v a2 s  .c  o m*/
    uye1.getAdresListesi().add(adres2);
    //-----------------------------------------------

    Uye uye2 = new Uye();
    uye2.setAdi("Mert");
    uye2.setSoyadi("Kalkan");
    Adres adres3 = new Adres();
    adres3.setSehir("ehir3");
    adres3.setIlce("le 3");
    adres3.setMahalle("Mahalle 3");
    adres3.setCadde("Cadde 3");
    Adres adres4 = new Adres();
    adres4.setSehir("ehir 4");
    adres4.setIlce("le 4");
    adres4.setMahalle("Mahalle 4");
    adres4.setCadde("Cadde 4");
    uye2.getAdresListesi().add(adres3);
    uye2.getAdresListesi().add(adres4);
    //--------------------------------------------------

    SessionFactory sf = new Configuration().configure().buildSessionFactory();
    Session session = sf.openSession();
    session.beginTransaction();
    session.save(uye1);
    session.save(uye2);
    session.getTransaction().commit();
    session.close();
    sf.close();
}

From source file:com.mesut.embeddedembedable.Main.java

public static void main(String[] args) {
    Uye uye = new Uye();
    uye.setAdi("Ahmet");
    uye.setSoyadi("Demir");
    Adres adres = new Adres();
    adres.setSehir("Ankara");
    adres.setIlce("ankaya");
    adres.setMahalle("mahalle");
    adres.setCadde("cadde");
    uye.setAdres(adres);//  ww w. ja  va2s .co  m
    //------------------------------------

    SessionFactory sf = new Configuration().configure().buildSessionFactory();
    Session session = sf.openSession();
    session.beginTransaction();
    session.save(uye);
    session.getTransaction().commit();
    session.close();
    sf.close();
}

From source file:com.mesut.onetoone.Main.java

public static void main(String[] args) {
    Personel personel = new Personel();
    personel.setAdi("Mehmet");
    personel.setSoyadi("Yldz");
    //-------------------------------------

    Meslek meslek = new Meslek();
    meslek.setMeslekAdi("renci");
    //-------------------------------------

    personel.setMeslek(meslek);//from   w  w w . j  a va 2 s. c  o m
    meslek.setPersonel(personel);

    SessionFactory sf = new Configuration().configure().buildSessionFactory();
    Session session = sf.openSession();
    session.beginTransaction();
    session.save(personel);
    session.save(meslek);
    session.getTransaction().commit();
    session.close();
    sf.close();
}

From source file:com.mesut.secondarytable.Main.java

public static void main(String[] args) {
    Personel personel = new Personel();
    personel.setName("Mesut");
    personel.setSurname("Yakut");
    personel.setAddress("Diyarbakr");
    personel.setPhone("1234567");
    personel.setHireDate(Calendar.getInstance());
    //-----------------------------------------------

    SessionFactory sf = new Configuration().configure().buildSessionFactory();
    Session session = sf.openSession();//  w w  w  .  j  a v  a2s.  c o  m
    session.beginTransaction();
    session.save(personel);
    session.getTransaction().commit();
    //---------------------------------------------------
    session.beginTransaction();
    personel = null;
    personel = (Personel) session.get(Personel.class, 1);
    System.out.println("Id: " + personel.getId());
    System.out.println("Name: " + personel.getName());
    System.out.println("Surname: " + personel.getSurname());
    System.out.println("Address: " + personel.getAddress());
    System.out.println("Phone: " + personel.getPhone());
    System.out.println("Hire Date: " + personel.getHireDate());
    session.getTransaction().commit();
    session.close();
    sf.close();
}

From source file:com.model.database.GtfsDatabaseLoaderMain.java

License:Apache License

private void run(String[] args) throws IOException {
    CommandLine cli = parseCommandLineOptions(args);

    args = cli.getArgs();// www. j  av a  2  s . com
    if (args.length != 1) {
        printUsage();
        System.exit(-1);
    }

    Configuration config = new Configuration();
    config.setProperty("hibernate.connection.driver_class", cli.getOptionValue(ARG_DRIVER_CLASS));
    config.setProperty("hibernate.connection.url", cli.getOptionValue(ARG_URL));
    if (cli.hasOption(ARG_USERNAME)) {
        config.setProperty("hibernate.connection.username", cli.getOptionValue(ARG_USERNAME));
    }
    if (cli.hasOption(ARG_PASSWORD)) {
        config.setProperty("hibernate.connection.password", cli.getOptionValue(ARG_PASSWORD));
    }
    config.setProperty("hibernate.connection.pool_size", "1");
    config.setProperty("hibernate.cache.provider_class", "org.hibernate.cache.NoCacheProvider");
    config.setProperty("hibernate.hbm2ddl.auto", "update");
    config.addResource("com/model/database/connections/transit/mapping/GtfsMapping.hibernate.xml");
    config.addResource(
            "com/model/database/connections/transit/mapping/HibernateGtfsRelationalDaoImpl.hibernate.xml");

    SessionFactory sessionFactory = config.buildSessionFactory();
    HibernateGtfsFactory factory = new HibernateGtfsFactory(sessionFactory);

    GtfsReader reader = new GtfsReader();
    reader.setInputLocation(new File(args[0]));

    GtfsMutableRelationalDao dao = factory.getDao();
    reader.setEntityStore(dao);
    try {
        reader.run();
    } catch (Exception e) {
        DbUpdate.gtfsMessage = e.getMessage();
        DbUpdate.gtfsUpload = true;
    } finally {
        reader.close();
        sessionFactory.close();
    }
}

From source file:com.model.database.onebusaway.gtfs.hibernate.ext.GtfsHibernateReaderExampleMain.java

License:Apache License

public static void updateSessions() {
    for (SessionFactory s : sessions) {
        s.close();
    }//w  w w.  j  a va2  s. co m
    factory = new HibernateGtfsFactory[Databases.dbsize];
    sessions = new SessionFactory[Databases.dbsize];
    for (int k = 0; k < Databases.dbsize; k++) {
        factory[k] = createHibernateGtfsFactory(Databases.ConfigPaths[k], k);
    }
}

From source file:com.model.database.queries.util.Hutil.java

License:Open Source License

public static void updateSessions() {
    for (SessionFactory s : sessionFactory) {
        s.close();
    }/*from   w w  w.j  a  v  a  2 s  .  co  m*/
    sessionFactory = new SessionFactory[Databases.dbsize];
    for (int k = 0; k < Databases.dbsize; k++) {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            sessionFactory[k] = new Configuration().configure(Databases.spatialConfigPaths[k])
                    .buildSessionFactory();

        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }
}