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:servs.AddRecord.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    System.out.println(request.getQueryString());
    String action = request.getParameter("qry");
    switch (action) {
    case "ajx": {
        try {/*ww  w. j a va 2  s.c  om*/
            String str = request.getParameter("q") + "%";
            PrintWriter out = response.getWriter();

            Configuration cfg = new Configuration().configure("hibernate.cfg.xml");
            SessionFactory factory = cfg.buildSessionFactory();
            Session s = factory.openSession();
            Transaction tx = s.beginTransaction();

            String sql = "From bean.Record where recname like :name";
            Query q = s.createQuery(sql);
            q.setParameter("name", str);
            List list = q.list();
            Iterator it = list.iterator();
            out.print("<datalist>");
            while (it.hasNext()) {
                bean.Record obj = (bean.Record) it.next();
                String b = "'" + obj.getRecname() + "'";
                out.print("<option value=" + b + ">" + obj.getRecname() + "</option>");
            }
            out.print("</datalist>");
            tx.commit();
            s.close();
            factory.close();

        } catch (Exception ex) {
            System.out.println("ERROR STARTS FROM HERE:" + ex);
        }

        break;
    }

    case "Submit": {
        int catid = Integer.parseInt(request.getParameter("categorySelect"));
        String recname = request.getParameter("recordname");
        String desc = request.getParameter("desc");
        String tags = request.getParameter("tags");

        bean.Record r = new Record();
        r.setCatid(catid);
        r.setDesc(desc);
        r.setRecname(recname);
        r.setTags(tags);

        Configuration cfg = new Configuration().configure("hibernate.cfg.xml");
        SessionFactory factory = cfg.buildSessionFactory();
        Session s = factory.openSession();
        Transaction tx = s.beginTransaction();
        s.save(r);
        tx.commit();
        factory.close();

        //==== if success then redirect to success page
        request.getSession(false).setAttribute("info", "Your Record is Added Successfully");
        response.sendRedirect("AdminPanel/AdminPanelSuccess.jsp");

        break;
    }
    }

}

From source file:servs.Feedback_Review.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String action = request.getParameter("action");

    switch (action) {
    case "Submit Feedback": {
        try {//from   ww w  .ja  v  a  2  s .c  o  m
            String name = request.getParameter("fname");
            String subject = request.getParameter("fsub");
            String ftext = request.getParameter("ftxt");

            bean.Feedback f = new Feedback();
            f.setFusr(name);
            f.setFsub(subject);
            f.setFtext(ftext);

            Configuration cfg = new Configuration();
            SessionFactory sessionFactory = cfg.configure("hibernate.cfg.xml").buildSessionFactory();
            Session s = sessionFactory.openSession();
            Transaction transaction = s.beginTransaction();
            s.save(f);
            transaction.commit();
            s.close();
            sessionFactory.close();

            response.getWriter().print("Thankyou for your feedback!");

        }

        catch (Exception ex) {
            System.out.println(ex);
        }
        break;
    }

    case "Submit Review": {
        try {
            String rtext = request.getParameter("rtext");
            int recid = Integer.parseInt(request.getParameter("recid"));
            String uname = request.getSession(false).getAttribute("wname").toString();
            int uid = Integer.parseInt(request.getSession(false).getAttribute("wid").toString());

            System.out.println(rtext);
            bean.Review r = new Review();
            r.setRevtext(rtext);
            r.setRevusrname(uname);
            r.setRevusr(uid);
            r.setRecid(recid);
            Date dateobj = new Date();
            r.setPostdate(dateobj);
            r.setVisible(false);

            Configuration cfg = new Configuration();
            SessionFactory sessionFactory = cfg.configure("hibernate.cfg.xml").buildSessionFactory();
            Session s = sessionFactory.openSession();
            Transaction transaction = s.beginTransaction();
            s.save(r);
            transaction.commit();
            s.close();
            sessionFactory.close();

            response.getWriter().print("Thankyou for your review!");

        }

        catch (Exception ex) {
            System.out.println(ex);
        }
        break;
    }
    }

}

From source file:servs.SearchBar.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    try {//w  ww . ja v a 2s .c  om

        String str = request.getParameter("q");
        String cat = request.getParameter("c");
        String area = request.getParameter("a");

        PrintWriter out = response.getWriter();

        Configuration cfg = new Configuration().configure("hibernate.cfg.xml");
        SessionFactory factory = cfg.buildSessionFactory();
        Session s = factory.openSession();
        Transaction tx = s.beginTransaction();

        String sql = "From bean.Record";
        Query q = s.createQuery(sql);

        if (!cat.isEmpty() && area.isEmpty()) {
            String strnew = "%" + str + "%";
            int catid = Integer.parseInt(cat);
            sql = "Select recname From bean.Record where recname like :name and catid=:catid";
            q = s.createQuery(sql);
            q.setParameter("name", strnew);
            q.setParameter("catid", catid);
        }

        else if (!area.isEmpty() && cat.isEmpty())

        {
            String areanew = "%" + area + "%";
            sql = "Select distinct r.recname" + " From bean.Record r,bean.Address a" + " where r.recid=a.recid"
                    + " and a.area like :name";
            q = s.createQuery(sql);
            q.setParameter("name", areanew);
        }

        else if (!area.isEmpty() && !cat.isEmpty()) {
            String areanew = "%" + area + "%";
            int catid = Integer.parseInt(cat);
            System.out.println(catid);
            sql = "Select distinct r.recname" + " From bean.Record r,bean.Address a" + " where r.recid=a.recid"
                    + " and r.catid =:catid" + " and a.area like :name";
            q = s.createQuery(sql);
            q.setParameter("catid", catid);
            q.setParameter("name", areanew);
        } else {
            sql = "Select recname From bean.Record";
            q = s.createQuery(sql);
        }

        List list = q.list();
        Iterator it = list.iterator();
        out.print("<datalist>");
        out.print("<option>---RECORDS---</option>");
        while (it.hasNext()) {
            Object rcname = it.next();
            out.print("<option value='" + rcname + "'>" + rcname + "</option>");
        }
        out.print("</datalist>");
        tx.commit();
        s.close();
        factory.close();

    } catch (Exception ex) {
        System.out.println("ERROR STARTS FROM HERE:" + ex);
    }

}

From source file:servs.ViewClassified.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    try {//  w w w  .  j  a  va 2 s  .c o  m
        PrintWriter out = response.getWriter();

        int catid = Integer.parseInt(request.getParameter("c"));
        boolean imgcheck = Boolean.valueOf(request.getParameter("i"));

        Configuration cfg = new Configuration().configure("hibernate.cfg.xml");
        SessionFactory factory = cfg.buildSessionFactory();
        Session s = factory.openSession();
        Transaction tx = s.beginTransaction();

        Query query;
        if (catid > 0 && imgcheck == false) {

            String sql1 = "Select c.title, c.postdate,c.cid" + " From bean.Classified c" + " where c.catid=:id"
                    + " order by c.postdate desc";

            query = s.createQuery(sql1);
            query.setParameter("id", catid);

        }

        else if (catid == 0 && imgcheck == false) {
            String sql1 = "Select c.title, c.postdate,c.cid" + " From bean.Classified c"
                    + " order by c.postdate desc";

            query = s.createQuery(sql1);
        }

        else if (catid > 0 && imgcheck == true) {
            String sql1 = "Select c.title, c.postdate,c.cid" + " From bean.Classified c"
                    + " where c.img1 is NOT NULL and " + " c.catid=:id" + " order by c.postdate desc";

            query = s.createQuery(sql1);
            query.setParameter("id", catid);
        } else {
            String sql1 = "Select c.title, c.postdate,c.cid" + " From bean.Classified c"
                    + " where c.img1 is NOT NULL" + " order by c.postdate desc";

            query = s.createQuery(sql1);
        }

        List<Object[]> list = query.list();
        Iterator itr = list.iterator();

        String temp = "";
        String temptday = "";

        if (itr.hasNext()) {
            out.print("<div>");
            while (itr.hasNext()) {
                Object[] obj = (Object[]) itr.next();

                Date todaydate = new Date();
                Date clsdate = (Date) obj[1];
                String cdate = clsdate.getDate() + clsdate.getMonth() + clsdate.getYear() + "";
                String rdate = todaydate.getDate() + todaydate.getMonth() + todaydate.getYear() + "";

                if (cdate.equals(rdate)) {
                    if (cdate.equals(temptday)) {

                    } else {
                        out.print(
                                "<table id='tbl' style='border: #ccc 1px solid;'><tr><td style='border-bottom: #ccc 1px solid;'>");
                        out.print("Posted Today");
                        out.print("</td></tr>");

                    }

                    out.print("<tr><td><a href='ViewClassified.jsp?q=" + obj[2].toString() + "'>"
                            + obj[0].toString() + "</a></td></tr>");
                    temptday = cdate;
                } else {

                    String[] parts = new String[1];

                    parts = obj[1].toString().split(" ");

                    if (parts[0].equals(temp)) {

                    } else {
                        out.print(
                                "</table><table id='tbl'  style='border: #ccc 1px solid;'><tr><td style='border-bottom: #ccc 1px solid;'>");
                        out.print("Posted on : " + parts[0]);
                        out.print("</td></tr>");
                    }

                    out.print("<tr><td ><a href='ViewClassified.jsp?q=" + obj[2].toString() + "'>"
                            + obj[0].toString() + "</a></td></tr>");

                    temp = parts[0];
                }
            }
            out.print("</table>");
        }
        out.print("</div>");

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

    }

    catch (Exception ex) {
        System.out.println(ex.getMessage());
    }

}

From source file:smile.weixin.jdquanyi.database.Database.java

public boolean FuXiuExists(String fromUserName) {
    boolean exist = false;

    SessionFactory sf = new AnnotationConfiguration().configure().buildSessionFactory();
    Session s = sf.getCurrentSession();//from  ww  w.ja  va2s.  c  om
    s.beginTransaction();
    //        System.out.println("begintransaction");
    Query q = s.createQuery("from FX_FuXiu");
    List<FX_FuXiu> fx_list = q.list();
    s.getTransaction().commit();
    sf.close();

    for (FX_FuXiu f : fx_list) {
        if (f.getFx_fromUserName().equals(fromUserName)) {
            exist = true;
            break;
        }
    }
    return exist;
}

From source file:smile.weixin.jdquanyi.database.Database.java

public int GongYiSanExists(String fromUsername) {
    int exits = 0;

    SessionFactory sf = new AnnotationConfiguration().configure().buildSessionFactory();
    Session s = sf.getCurrentSession();/*from  ww w.jav  a2s.  c o m*/
    s.beginTransaction();
    Query q = s.createQuery("from GYS_GongYiSan");
    List<GYS_GongYiSan> gys_list = q.list();
    s.getTransaction().commit();
    sf.close();

    for (GYS_GongYiSan gys : gys_list) {
        if (gys.getGYS_fromUserName().equals(fromUsername)) {
            exits = 1;
        }
        if (gys.getGYS_fromUserName().equals(fromUsername) && gys.getGYS_verification()) {
            exits = 2;
        }
    }
    return exits;
}

From source file:smile.weixin.jdquanyi.database.Database.java

public boolean AddFuxiu(FX_FuXiu fx_user) {
    SessionFactory sf = new AnnotationConfiguration().configure().buildSessionFactory();
    Session s = sf.getCurrentSession();// w w  w .  jav a 2s  .  co m
    s.beginTransaction();
    s.save(fx_user);
    s.getTransaction().commit();
    sf.close();
    return true;
}

From source file:smile.weixin.jdquanyi.database.Database.java

public boolean AddGongYiSan(GYS_GongYiSan gys_user) {
    SessionFactory sf = new AnnotationConfiguration().configure().buildSessionFactory();
    Session s = sf.getCurrentSession();/*from   w ww  .j a v  a2  s .  c o m*/
    s.beginTransaction();
    s.save(gys_user);
    s.getTransaction().commit();
    sf.close();
    return true;
}

From source file:smile.weixin.jdquanyi.database.Database.java

public boolean GongYiSanIsPure(String fromUserName) {
    boolean pure = true;
    SessionFactory sf = new AnnotationConfiguration().configure().buildSessionFactory();
    Session s = sf.getCurrentSession();/*from   w w w  .  ja va 2 s  . c  om*/
    s.beginTransaction();
    Query q = s.createQuery("from GYS_GongYiSan");
    List<GYS_GongYiSan> gys_list = q.list();
    s.getTransaction().commit();
    sf.close();

    for (GYS_GongYiSan gys : gys_list) {
        if (gys.getGYS_fromUserName().equals(fromUserName)) {
            pure = gys.getGYS_pure();
            break;
        }
    }
    return pure;
}

From source file:smile.weixin.jdquanyi.database.Database.java

public boolean GongYiSanIsVerified(String fromUserName) {
    boolean verified = false;
    SessionFactory sf = new AnnotationConfiguration().configure().buildSessionFactory();
    Session s = sf.getCurrentSession();/*from   w  ww.ja  va  2  s  .  c  om*/
    s.beginTransaction();
    Query q = s.createQuery("from GYS_GongYiSan");
    List<GYS_GongYiSan> gys_list = q.list();
    s.getTransaction().commit();
    sf.close();

    for (GYS_GongYiSan gys : gys_list) {
        if (gys.getGYS_fromUserName().equals(fromUserName)) {
            verified = gys.getGYS_verification();
            break;
        }
    }
    return verified;
}