Hi I am trying to retrieve data from MySQL table using
session.createQuery(). All I am doing is a simple user authentication.
Here is my method
Code:
private static final String QUERY_USER_BY_USER_PASSWD =
"from Users as users" +
" where " +
" users.logonUser = :logonUser AND" +
" users.logonPassword = :logonPwd";
public boolean isValidUser( String logonUser
,String logonPwd)
{
Session session = null;
try
{
LOG.debug("isValidUser->");
session = DBStoreCreator.currentSession();
Query q = session.createQuery(QUERY_USER_BY_USER_PASSWD);
q.setParameter("logonUser", logonUser);
q.setParameter("logonPwd", logonPwd);
LOG.debug("query completed->");
return (q.list().size() == 0) ? false : true;
}
catch (HibernateException he)
{
LOG.debug("validateUser Error", he);
return false;
}
finally
{
if ( session != null)
{
try
{
DBStoreCreator.closeSession();
}
catch (HibernateException heclose)
{
LOG.debug("isValidUser session close", heclose);
}
}
}
}
My hibernate helper class which is using the ThreadLocal pattern is given below too. (I just copied this from hibernate website I think).
Code:
package com.cexp.icuissues.db.utils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class DBStoreCreator {
private static final Log LOG = LogFactory.getLog(DBStoreCreator.class);
public static SessionFactory sessionFactory = null;
public static final ThreadLocal session = new ThreadLocal();
static {
try {
sessionFactory = new Configuration().configure()
.buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial sessionFactory creation failed" + ex);
}
}
public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();
// Open a new Session, if this thread has none yet
if (s == null) {
LOG.debug("->creating a new session");
s = sessionFactory.openSession();
// Store it in the ThreadLocal variable
session.set(s);
}
return s;
}
public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
if (s != null)
s.close();
session.set(null);
}
}
I am calling this method isValidUser from my struts action class.
When I give an username/password which exists in the table, query fetches the record and everything works fine.
But when I enter a username/password combination which does not exists in the table,
isValidUser method is called many times and I am getting a stack overflow error.
I have tested this method using JUNIT. Both the success and failure scernarios work fine there.
I am getting this problem only when I am deploying it in a container (TOMCAT)
Please help me
thanks