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:ctd.services.getTicket.java

License:Apache License

public String getTicket() throws SerializerException {

    //init parameters
    ResourceBundle res = ResourceBundle.getBundle("settings");

    String webservice_password = res.getString("ws.password");

    String ftp_folder = res.getString("ws.ftp_folder");
    String prefix_ctd_reference = res.getString("ws.prefix_ticket_reference");
    String prefix_ftp_subfolder = res.getString("ws.prefix_ftp_subfolders");

    TicketClient ticket_client = new TicketClient();
    String new_folder = "";
    String message = "";

    String password_client = getPassword();

    if (webservice_password.equals(password_client)) {
        //open hibernate connection
        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction tr = session.beginTransaction();

        Integer id = 1;/*from   w  ww  . j  ava2  s  . com*/
        SQLQuery q1 = session.createSQLQuery("Select MAX(id) FROM ticket");
        Iterator it1 = q1.list().iterator();
        while (it1.hasNext()) {
            id = (Integer) it1.next();
            if (id != null) {
                id++;
            }
            if (id == null) {
                id = 1;
            }
        }

        ///Get unique password for processing the submitted celfiles
        UUID uuid = null;

        ///check if this uuid is not allready used in this database
        Boolean check = true;
        while (check) {
            uuid = UUID.randomUUID();
            Query q2 = session.createQuery("from Ticket where password='" + uuid.toString() + "'");
            if (q2.list().isEmpty()) {
                check = false;
            }
        }

        //String db_password = password_client+"_"+webservice_password;//uuid.toString();
        String db_password = uuid.toString();
        //ctd reference id
        String ctd_ref = prefix_ctd_reference + String.valueOf(id);
        //ctd ftp subfolder
        String ctd_ftp_folder = prefix_ftp_subfolder + String.valueOf(id);

        //create subdir
        new_folder = ftp_folder + ctd_ftp_folder;
        boolean success = (new File(new_folder)).mkdir();
        //change owner from root to cleandata for this new directory

        //Adjust permissions
        String command1 = "chown cleandata " + new_folder;
        String command2 = "chgrp cleandata " + new_folder;
        String command3 = "chmod 777 " + new_folder;
        String command4 = "chmod +t " + new_folder;
        Process child;
        try {
            child = Runtime.getRuntime().exec(command1);
            child = Runtime.getRuntime().exec(command2);
            child = Runtime.getRuntime().exec(command3);
            child = Runtime.getRuntime().exec(command4);
        } catch (IOException ex) {
            Logger.getLogger(getTicket.class.getName()).log(Level.SEVERE, null, ex);
        }

        Ticket ticket = new Ticket();
        ticket.setFolder(ctd_ftp_folder);
        ticket.setPassword(db_password);
        ticket.setCtdRef(ctd_ref);
        ticket.setClosed("no");

        ticket_client.setCtdRef(ctd_ref);
        ticket_client.setFolder(ctd_ftp_folder);
        ticket_client.setPassword(db_password);

        session.save(ticket);
        session.persist(ticket);
        tr.commit();

        session.close();
        sessionFactory.close();
        session = null;
        sessionFactory = null;
    }

    ////////////////////
    //SKARINGA
    ObjectTransformer trans = null;
    try {
        trans = ObjectTransformerFactory.getInstance().getImplementation();
    } catch (NoImplementationException ex) {
        Logger.getLogger(getTicket.class.getName()).log(Level.SEVERE, null, ex);
    }
    message = trans.serializeToJsonString(ticket_client);

    return message;
}

From source file:ctd.services.internal.Query.java

License:Apache License

/**
 * @return the downloadData/*from   w w  w . ja  v  a2  s . c  om*/
 */
public String getDownloadData() {
    //init parameters
    ResourceBundle res = ResourceBundle.getBundle("settings");
    String ftp_username = res.getString("ws.ftp_username");
    String ftp_folder = res.getString("ws.ftp_folder");
    String hostname = res.getString("ws.hostname");

    //open hibernate connection
    SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
    Session session = sessionFactory.openSession();

    Ticket ticket = null;

    SQLQuery q1 = session.createSQLQuery("Select ctd_REF,title,folder,id FROM ticket Order By id ASC");
    Iterator it1 = q1.list().iterator();

    String table = "";

    while (it1.hasNext()) {
        Object[] data = (Object[]) it1.next();
        String ctd_REF = (String) data[0];
        String title = (String) data[1];
        String folder = (String) data[2];

        if (title != null && title.equals("") == false) {
            String link2 = "sftp://" + ftp_username + "@" + hostname + ":" + ftp_folder + folder + "/";
            String link1 = "<a href=\"" + link2 + "\">link</a>";
            table = table + "<tr><td>" + ctd_REF + "</td><td>" + title + "</td><td>" + link1 + "</td></tr>";
        }

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

    return table;
}

From source file:cz.cuni.mff.ufal.lindat.utilities.HibernateFunctionalityManager.java

License:Open Source License

public static void main(String args[]) {
    try {/*w ww  .  ja  va  2  s  .co  m*/
        //expecting INSTALL_DIR in args[0]
        String dspace_path = args[0] + "/config/" + Variables.default_config_file;
        HibernateFunctionalityManager functionalityManager = new HibernateFunctionalityManager(dspace_path);
        System.out.println(String.format("\nUsing dspace configuration from %s.\nTrying to connect to %s",
                dspace_path, Variables.databaseURL));
        SessionFactory sessionFactory = functionalityManager.hibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        Query query = session.createSQLQuery("select current_date");
        List result = query.list();
        System.out.println(String.format("Current date selected from database is: %s", result.get(0)));
        session.close();
        sessionFactory.close();
    } catch (Exception e) {
        System.err.println("\nCould NOT connect to utilities database!\n");
        throw new RuntimeException("Could not connect to database - " + e.toString());
    }

    System.out.println("\nConnection to utilities database successful!\n");
}

From source file:DataLayer.CtrlCasellaDB.java

@Override
public Casella get(Integer idPartida, Integer numeroFila, Integer numeroColumna) 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();/* www. j  av  a  2  s.  co m*/
    List<Casella> l = session
            .createQuery("from Casella where idPartida = :idP and numeroFila = :nF and numeroColumna = :nC")
            .setParameter("idP", idPartida).setParameter("nF", numeroFila).setParameter("nC", numeroColumna)
            .list();
    session.getTransaction().commit();
    factory.close();
    if (!l.isEmpty()) {
        return l.get(0);
    }
    throw new Exception("casellaNoExisteix");
}

From source file:DataLayer.CtrlCasellaDB.java

@Override
public boolean exists(Integer idPartida, Integer numeroFila, Integer numeroColumna) {
    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.  java 2 s.c o  m
    List<Casella> l = session
            .createQuery("from Casella where idPartida = :idP and numeroFila = :nF and numeroColumna = :nC")
            .setParameter("idP", idPartida).setParameter("nF", numeroFila).setParameter("nC", numeroColumna)
            .list();
    session.getTransaction().commit();
    factory.close();
    return !l.isEmpty();
}

From source file:DataLayer.CtrlCasellaDB.java

@Override
public Set<Casella> 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 . ja  v a  2  s .c  o m*/
    List<Casella> l = session.createQuery("from Casella").list();
    session.getTransaction().commit();
    factory.close();
    Set<Casella> r = new HashSet();
    for (Casella c : l) {
        r.add(c);
    }
    return r;
}

From source file:DataLayer.CtrlJoc2048DB.java

@Override
public Joc2048 get() {
    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. jav  a2 s .  co  m*/
    List<Joc2048> l = session.createQuery("from Joc2048").list();
    session.getTransaction().commit();
    factory.close();
    if (!l.isEmpty())
        return l.get(0);
    else {
        Joc2048 j = Joc2048.joc2048();
        return j;
    }
}

From source file:DataLayer.CtrlJugadorDB.java

@Override
public Jugador getU(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.ja v a2 s.  c  o  m
    List<Jugador> l = session.createQuery("from Jugador where username = :usr").setParameter("usr", username)
            .list();
    session.getTransaction().commit();
    factory.close();
    if (!l.isEmpty())
        return l.get(0);
    throw new Exception("jugadorNoExisteix");
}

From source file:DataLayer.CtrlJugadorDB.java

@Override
public Jugador getE(String email) 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();//  w  ww .  j  a v  a 2s .co  m
    List<Jugador> l = session.createQuery("from Jugador where email = :em").setParameter("em", email).list();
    session.getTransaction().commit();
    factory.close();
    if (!l.isEmpty())
        return l.get(0);
    throw new Exception("jugadorNoExisteix");
}

From source file:DataLayer.CtrlJugadorDB.java

@Override
public Boolean existsU(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();//w w  w. ja v  a  2  s .c o  m
    List<Jugador> l = session.createQuery("from Jugador where username = :usr").setParameter("usr", username)
            .list();
    session.getTransaction().commit();
    factory.close();
    return !l.isEmpty();
}