-->
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.  [ 9 posts ] 
Author Message
 Post subject: Problems with hibernate and tomcat
PostPosted: Tue Aug 24, 2004 1:22 pm 
Newbie

Joined: Tue Aug 24, 2004 1:14 pm
Posts: 14
Sorry to bother everyone i have small problem. I am writing a small struts application, and am stuck on the login.

Wrote a java class that access the database, and retreives the data from the database, this works fine when i build it with a simple java application file. As soon as the code moves in to the struts action class and then called i get a session closed error. I am running MySQL 4, jdk 1.4.2_05, tomcat 5.0.27, any ideas of what could be the problem would be greatly appreciated.

Tom

ps here are all the bits the forum asks for.

Hibernate version:
The one with myeclipseide
Mapping documents:
<hibernate-mapping package="co.uk.fastest.foi.hibernate">

<class name="Users" table="Users">
<id name="usersUsername" column="users_username" type="java.lang.String">
<generator class="native"/>
</id>

<property name="usersPassword" column="users_password" type="java.lang.String" not-null="true" />
<property name="usersLevel" column="users_level" type="java.lang.String" not-null="true" />
<property name="usersEmail" column="users_email" type="java.lang.String" not-null="true" />
<property name="usersDepartment" column="users_department" type="java.lang.String" not-null="true" />
<property name="usersTitle" column="users_title" type="java.lang.String" not-null="true" />
<property name="usersJobTitle" column="users_job_title" type="java.lang.String" />
<property name="usersFirstName" column="users_first_name" type="java.lang.String" not-null="true" />
<property name="usersInitials" column="users_initials" type="java.lang.String" not-null="true" />
<property name="usersSurname" column="users_surname" type="java.lang.String" not-null="true" />
<property name="usersTelephone" column="users_telephone" type="java.lang.String" not-null="true" />
</class>

</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
public static Session currentSession ( ) throws HibernateException
{
Session session = ( Session ) threadLocal.get ( );

if ( session == null )
{
if ( sessionFactory == null )
{
try
{
cfg.configure ( CONFIG_FILE_LOCATION );
sessionFactory = cfg.buildSessionFactory ( );
}
catch ( Exception e )
{
System.err.println ( "%%%% Error Creating SessionFactory %%%%" );
e.printStackTrace ( );
}
}
session = sessionFactory.openSession ( );
threadLocal.set ( session );
}

return session;
}

/**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession ( ) throws HibernateException
{
Session session = ( Session ) threadLocal.get ( );
threadLocal.set ( null );

if ( session != null )
{
session.close ( );
}
}

Full stack trace of any exception that occurs:
HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: net.sf.hibernate.HibernateException: Session is closed
org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:545)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:486)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

root cause

java.lang.RuntimeException: net.sf.hibernate.HibernateException: Session is closed
co.uk.fastest.foi.hibernate.UserAccess.getUsers(UserAccess.java:74)
co.uk.fastest.foi.struts.action.LoginAction.execute(LoginAction.java:35)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

note The full stack trace of the root cause is available in the Apache Tomcat/5.0.27 logs.
Name and version of the database you are using:

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 24, 2004 1:51 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
do you use servletfilter to _manage_ session per request?

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject: Maybe?
PostPosted: Tue Aug 24, 2004 1:53 pm 
Newbie

Joined: Tue Aug 24, 2004 1:14 pm
Posts: 14
How do i check


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 24, 2004 1:57 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
take a look at
open session in view + threadlocal pattern

you just have to configue a servletFilter (web.xml)
this filter contains :
- the static session factory
- static method to set & get session storing it into threadLocal
- and the filter method that is called at each httprequest and manage session.close so you have a clean and new session for each request.

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 25, 2004 4:04 am 
Newbie

Joined: Tue Aug 24, 2004 1:14 pm
Posts: 14
could you be more specific as i am having problems finding out about filters.

Tom


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 25, 2004 4:09 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
http://www.hibernate.org/43.html

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 25, 2004 4:32 am 
Newbie

Joined: Tue Aug 24, 2004 1:14 pm
Posts: 14
Sorry to be a pain but do you have to use jndi and what is wrong with this SessionFactory class that i wrote.

package co.uk.fastest.foi.hibernate;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.cfg.Configuration;

public class SessionFactory
{

private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";

private static final ThreadLocal threadLocal = new ThreadLocal ( );

private static final Configuration cfg = new Configuration ( );


private static net.sf.hibernate.SessionFactory sessionFactory;


public static Session currentSession ( ) throws HibernateException
{
Session session = ( Session ) threadLocal.get ( );

if ( session == null )
{
if ( sessionFactory == null )
{
try
{
cfg.configure ( CONFIG_FILE_LOCATION );
sessionFactory = cfg.buildSessionFactory ( );
}
catch ( Exception e )
{
System.err.println ( "%%%% Error Creating SessionFactory %%%%" );
e.printStackTrace ( );
}
}
session = sessionFactory.openSession ( );
threadLocal.set ( session );
}

return session;
}


public static void closeSession ( ) throws HibernateException
{
Session session = ( Session ) threadLocal.get ( );
threadLocal.set ( null );

if ( session != null )
{
session.close ( );
}
}

}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 25, 2004 4:37 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
check:
1- hibernate in action
2- or search the forum
3- get somme support (see jboss) ;)
4- check tomcat spec, you'll see that for tomcat, it is easier not to use JDNI

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject: Tomcat and jndi
PostPosted: Wed Aug 25, 2004 11:20 am 
Newbie

Joined: Fri Mar 19, 2004 2:09 pm
Posts: 3
Location: Edwardsville, IL
tjmalone wrote:
Sorry to be a pain but do you have to use jndi


I had the same problem last week. What I did was follow the example in the "open session in view" page. In addition, I created a servlet context listener that was responsible for creating, initializing, and storing the the SessionFactory in the application context.

Then I used the "Open Session in View" filter to grab the session factory and create sessions as needed. Use the application context, and you can avoid JNDI altogether.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.