views:

22

answers:

1

I have a web app (using Hibernate, and I cannot avoid that because is one of the point of the exam test ) for a university project in wich I need a login functionality.

After I run some test with Jmeter (basicaly http get e post in the login) I found that after 20 test the webapp stop working returning this message : bean userList not found within scope Basicaly I need to restart Mysql to make the app work again.

This is the code of the login functionality:

<jsp:root version="2.0" xmlns:jsp="http://java.sun.com/JSP/Page"
 xmlns:c="urn:jsptld:http://java.sun.com/jsp/jstl/core"&gt;
 <jsp:directive.page contentType="text/html; charset=UTF-8" />
<jsp:scriptlet>
    session.setAttribute( "userList", com.ggm.hibernateConnection.DAO.getData());
</jsp:scriptlet>
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
 <head>
 <title>Login Page</title>
 </head>
 <body background="sfondo_jsp2.jpg" text="white" link="white" vlink="yellow">
 <div align =" center">
 <br></br>
 <br></br>
 <h1> Benvenuto in Ggm Web Application! </h1>
 <br></br>
 <br></br>
 <br></br>
<jsp:useBean id="actualUser" class ="com.ggm.javaBean.LoginBean" scope="session"> </jsp:useBean>
 <form name="form1" method="POST">
 Username<input type="text" name ="loginUser"></input>
 <br></br>
 Password<input type="password" name ="loginPassword"></input>
 <br></br>
 <input type = "submit" value = "Login"></input>
 <jsp:setProperty name="actualUser" property="loginUser"></jsp:setProperty> 
 <jsp:setProperty name="actualUser" property="loginPassword"></jsp:setProperty> 
 </form>
 Non sei Registrato? Clicca <a href ="registeruser.jsp">QUI</a> per registrarti! 
 <jsp:useBean id="userList" scope="session" type="java.util.List"> </jsp:useBean>
 <c:forEach items="${userList}" var="UserTable">
 <jsp:setProperty name="actualUser" property="databaseUser" value="${UserTable.userName}"></jsp:setProperty>
 <jsp:setProperty name="actualUser" property="databasePassword" value="${UserTable.password}"></jsp:setProperty>
 <c:if test="${actualUser.login}">
  <jsp:directive.page import="javax.servlet.http.Cookie"></jsp:directive.page>
 <jsp:setProperty name ="actualUser" property ="loginUser" value ="null"></jsp:setProperty>
 <c:set var="authorization" value="${true}" scope="session" ></c:set>
 <a href = "poi.jsp">Ciao! sei loggato ora! Clicca qui per andare alla pagina di Inserimento POI</a>
 <c:redirect url="poi.jsp"></c:redirect>
 </c:if>
 </c:forEach>
 </div>
 </body>    
 </html>
 </jsp:root>

And this is the DAO clas for the access to the database (via Hibernate)

package com.ggm.hibernateConnection;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.*;
import java.util.*;

public class DAO
{
    public static List<?> getData ()
    {
        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        Session session = sessionFactory.getCurrentSession();
        List<?> result = null;
        try
        {
            session.beginTransaction();
            Query query = session.createQuery("from Users");
            result = query.list();
            session.getTransaction().commit();
            query.setReadOnly(true);
            query.setMaxResults(50);
            session.flush();
            session.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return result;      
    }

}

PS: sorry for my bad scholastic English.

+1  A: 

Building a session factory shouldn't happen on each request. The session-factory is per-application, so build (configure) it only once, and reuse it. see here

Bozho
Thanks, I will take a look at the code and paste the soulution to my problem.Probably tomorrow! I need some sleep XD
lordfuoco