-->
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.  [ 6 posts ] 
Author Message
 Post subject: Struts , hibernate and frames Session Exception
PostPosted: Tue Mar 02, 2004 8:34 am 
Newbie

Joined: Tue Mar 02, 2004 8:23 am
Posts: 2
Hi all,

I am using hibernate with struts.
I keep the hibernate session in the HttpSession.

I use the Session.connect() and disconnect() methods .

I call to different struts action on the same frameset (lets say with 2 frames called 'left' and 'right').

the 'left' frame call the Action 'getJOB' and the 'right' one 'GetEmploye'.


My problem is that i get the 'session already connected' exception.
Since both of the frames try to connect to the same Hibernate Session at the same time.

What would be the best practice to handle such a problem???

Should I synchronize the Action method that calls the Hibernate-session-connect() method?



I 've tried tried the method while (!session.isConnected()) but it is of course a dirty way to solve this problem !!!


Thanx for your help


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 02, 2004 8:43 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
better use thread local strategy than storing session in httpsession...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 02, 2004 8:46 am 
Hibernate Team
Hibernate Team

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


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 02, 2004 3:55 pm 
Newbie

Joined: Tue Mar 02, 2004 8:23 am
Posts: 2
thankx for answering so promptly.

Well i deliberately use the HttpSession to store the Hibernate session.

Cause i want to limit the database access and give top priority to the hibernate cache.

I've read the "open session in view" but i am not familiarised with this filter stuff.

And the Persistance class that implement the TreadLocal stuff you are talking about is not obvious to me since there is no example provided with it (http://www.hibernate.org/43.html)


Sorry me, but i 've never used EJB and j2ee application server.

I am using hibernate with tomcat and struts, and i am not developping a critical banking application (no transactions in my application).

Need a full example to understand (Filter + view and use)!!
;-)

Thanx anyway


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 02, 2004 4:09 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
lol i use tomcat and struts too.
ok i'll explain it
first, create this call:
package org.infohazard.pig.servlet;

import java.io.*;
import javax.servlet.*;
import javax.naming.*;

import net.sf.hibernate.*;

/**
* Filter which manages a ThreadLocal hibernate session. Obtain the session
* by calling Persistance.getSession(). Define the JNDI name of the
* hibernate session factory as an init param to the filter in your web.xml.
*
* @author <a href="mailto:jeff@infohazard.org">Jeff Schnitzer</a>
*/
public class Persistance implements Filter
{
/**
* Filter init param which defines the JNDI name for the hibernate factory
*/
public static final String HIBERNATE_FACTORY_JNDI_PARAM = "hibernateFactory";

/**
* Default value if no init param is set.
*/
public static final String HIBERNATE_FACTORY_JNDI_DEFAULT = "java:/HibernateSessionFactory";

/**
* Holds the current hibernate session, if one has been created.
*/
protected static ThreadLocal hibernateHolder = new ThreadLocal();

/**
*/
protected static SessionFactory factory;

/**
*/
public void init(FilterConfig filterConfig) throws ServletException
{
// Initialize hibernate
try
{
new Configuration().configure();
}
catch (HibernateException ex) { throw new ServletException(ex); }

// As good a place as any to initialize the factory
String factoryJndiName = filterConfig.getInitParameter(HIBERNATE_FACTORY_JNDI_PARAM);
if (factoryJndiName == null)
factoryJndiName = HIBERNATE_FACTORY_JNDI_DEFAULT;

try
{
Context ctx = new InitialContext();
factory = (SessionFactory)ctx.lookup(factoryJndiName);
}
catch (NamingException ex) { throw new ServletException(ex); }
}

/**
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException
{
if (hibernateHolder.get() != null)
throw new IllegalStateException(
"A session is already associated with this thread! "
+ "Someone must have called getSession() outside of the context "
+ "of a servlet request.");

try
{
chain.doFilter(request, response);
}
finally
{
Session sess = (Session)hibernateHolder.get();
if (sess != null)
{
hibernateHolder.set(null);

try
{
sess.close();
}
catch (HibernateException ex) { throw new ServletException(ex); }
}
}
}

/**
* ONLY ever call this method from within the context of a servlet request
* (specifically, one that has been associated with this filter). If you
* want a Hibernate session at some other time, call getSessionFactory()
* and open/close the session yourself.
*
* @return an appropriate Session object
*/
public static Session getSession() throws HibernateException
{
Session sess = (Session)hibernateHolder.get();

if (sess == null)
{
sess = factory.openSession();
hibernateHolder.set(sess);
}

return sess;
}

/**
* @return the hibernate session factory
*/
public static SessionFactory getSessionFactory()
{
return factory;
}

/**
* This is a simple method to reduce the amount of code that needs
* to be written every time hibernate is used.
*/
public static void rollback(Transaction tx)
{
if (tx != null)
{
try
{
tx.rollback();
}
catch (HibernateException ex)
{
// Probably don't need to do anything - this is likely being
// called because of another exception, and we don't want to
// mask it with yet another exception.
}
}
}

/**
*/
public void destroy()
{
// Nothing necessary
}
}




secondly declare the filter into web.xml

<!-- The hibernate filter configuration -->
<context-param>
<param-name>HIBERNATE_FILTER_SESSION_FACTORY_JNDI</param-name>
<param-value>java:comp/env/hibernate/SessionFactory</param-value>
</context-param>
<context-param>
<param-name>HIBERNATE_FILTER_REQUEST_ATTRIBUTE_ID</param-name>
<param-value>HibernateSession</param-value>
</context-param>
<context-param>
<param-name>HIBERNATE_FILTER_USE_PROXY</param-name>
<param-value>true</param-value>
</context-param>

<filter>
<filter-name>HibernateFilter</filter-name>
<filter-class>com.xe.hibernate.HibernateFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>HibernateFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>



As you can see, you have static method you can call.
Instead of doing HttpSession.getAttribute....
import the class above on your classes
and only call getSession(); to retrieve a "pure" hibernate session.

You have to understand that the session is not a cache, it's a business process cache.
If you want higher level cache, use EHCache for example, it's not complicated to use.

Don't forget, disconnect / close / commit ... the session depending what you're doing.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 02, 2004 4:20 pm 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
It is possible to make it work sometimes, but it is better not to try it.
You can detect this kind of conflicts in filter and to send redirect basck (retry).It is possible, but I do not think it is good way (it has memory problems too).

I think a good way is to use some well tested, soft JVM level cache and Transaction/connection per thread, but you must decide and to find a good way.


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