-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Session handling in multiple web context
PostPosted: Wed Mar 24, 2010 4:02 am 
Newbie

Joined: Wed Mar 24, 2010 3:16 am
Posts: 1
Hi,

I'm relatively new to Hibernate and have been experimenting with whether I could have web applications from different web context accessing the same database via hibernate. I seem to be having problems with seeing changes which are made via one web app on another web app.

I have spent a long time trying to figure out what is wrong with my application so I created two small webapp just to test out how I am using Hibernate instead. Turns out that it didnt work as well.
Basically, I'm trying to do update to the database on one web app, and viewing the update from another. I create 2 web projects "Inserter" and "Viewer". Inserter is used to add new entries to the db. When I try viewing the result through Viewer web app, it doesnt show. I suppose its to do with the persistence objects which are maintained separately between the two apps?

I'm hoping that someone could advise on what I need to do in order to be able to access the same hibernate persistent objects from two different web apps. Below are my test codes.

/Inserter/index.jsp:
Code:
<%@page
    import="java.util.Iterator"
    import="org.hibernate.Session"
    import="org.hibernate.Transaction"
    import="org.hibernate.Query"
    import="entity.HibernateUtil"
    import="entity.User"
    %>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form action="process/doInsert.jsp">
            Name: <input type="text" name="name"/>
            Surname: <input type="text" name="surname"/>
            <input type="submit" value="Insert"/>
        </form>
        <table border="1">
            <tr><td>Id</td><td>Name</td><td>Surname</td></tr>
<%
    Session sess = HibernateUtil.getSessionFactory().getCurrentSession();
    Transaction tx = null;

    try {
        tx = sess.beginTransaction();
        String SQL_QUERY = "from User s";
        Query query = sess.createQuery(SQL_QUERY);

        for (Iterator it = query.iterate(); it.hasNext();) {
            User newuser = (User) it.next();
            %>
            <tr><td><%=newuser.getId()%></td><td><%=newuser.getName()%></td><td><%=newuser.getSurname()%></td></tr>
            <%
        }
    } catch (Exception e) {
        System.out.println(e.getMessage());
    } finally {
        //sess.close();
    }
%>
        </table>
    </body>
</html>


/Inserter/process/doInsert.jsp:
Code:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<%@page
    import="org.hibernate.Session"
    import="org.hibernate.Transaction"
    import="org.hibernate.Query"
    import="entity.HibernateUtil"
    import="entity.User"
    %>
<%
    String name = request.getParameter("name");
    String surname = request.getParameter("surname");
    System.out.println("Name: " + name + ", Surname: " + surname);

    Session sess = HibernateUtil.getSessionFactory().getCurrentSession();
    Transaction tx = null;

    try {
        tx = sess.beginTransaction();
        User newuser = new User(name, surname);
        sess.saveOrUpdate(newuser);
        sess.persist(newuser);
        tx.commit();
    } catch (Exception e) {
        if (tx!=null)
            tx.rollback();
        System.out.println(e.getMessage());
    } finally {
        //sess.close();
        response.sendRedirect("../index.jsp");
    }
%>


/Viewer/index.jsp:
Code:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<%@page
    import="java.util.Iterator"
    import="org.hibernate.Session"
    import="org.hibernate.Transaction"
    import="org.hibernate.Query"
    import="entity.HibernateUtil"
    import="entity.User"
    %>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <table border="1">
            <tr><td>Id</td><td>Name</td><td>Surname</td></tr>
<%
//    Session sess = HibernateUtil.getSessionFactory().openSession();
    Session sess = HibernateUtil.getSessionFactory().getCurrentSession();
    Transaction tx = null;

    try {
        tx = sess.beginTransaction();

        String SQL_QUERY = "from User s";
        Query query = sess.createQuery(SQL_QUERY);

        for (Iterator it = query.iterate(); it.hasNext();) {
            User newuser = (User) it.next();
            %>
            <tr><td><%=newuser.getId()%></td><td><%=newuser.getName()%></td><td><%=newuser.getSurname()%></td></tr>
            <%
        }
    } catch (Exception e) {
        System.out.println(e.getMessage());
    } finally {
//        sess.close();
    }
%>
        </table>
    </body>
</html>


hibernate.cfg.xml (common for both):
Code:
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibtest</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">admin</property>
    <property name="hibernate.current_session_context_class">thread</property>
    <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
    <mapping resource="entity/User.hbm.xml"/>
  </session-factory>
</hibernate-configuration>


I used the Netbeans Hibernate wizard to automatically generate the POJOs, the hibernate mapping files, and the HibernateUtil file.


Top
 Profile  
 
 Post subject: Re: Session handling in multiple web context
PostPosted: Wed Mar 24, 2010 5:56 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
I'm hoping that someone could advise on what I need to do in order to be able to access the same hibernate persistent objects from two different web apps.


It should not be a problem at all to access the same data (records) out from two different web apps.
Obviously they are then held as separate objects in memory, as you can't share persistent objects
to different sessions (unless you detach then first from the session).

I would say, in first line the question to ask is:
Why does the query in Viewer/index.jsp not see the new inserted instances?

In order to investigate in this direction I suggest you to activate sql-logging with p6spy.
In this way you can:
1. check if the /Inserter/index.jsp properly inserts new records
2. check if the /Inserter/index.jsp does effectively also commit the transaction
3. check if/how the query from Viewer/index.jsp is propagated as sql-statement to the database
4. check which result-set is returned from database


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.