-->
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.  [ 12 posts ] 
Author Message
 Post subject: Lost session on second HQL query.
PostPosted: Wed Feb 08, 2006 12:18 pm 
Newbie

Joined: Wed Feb 08, 2006 11:04 am
Posts: 7
Location: Vail, Colorado
Greetings all my first post.

I have successfully executed a simple HQL query returning a cartline given a cartlineid. My issue is that when doing a refresh or executing a query with a different cartlineid during the session I receive a null pointer exception.


First successfull query:

From the Log:
Hibernate: select cartlinedo0_.cartid as col_0_0_ from cartline cartlinedo0_ where cartlinedo0_.cartlineid=?


Unsuccessfull query executed after first:

Hibernate: select cartlinedo0_.cartid as col_0_0_ from cartline cartlinedo0_ where cartlinedo0_.cartlineid=?
08:39:20,429 INFO [STDOUT] java.lang.NullPointerException




When I restart JBoss and refresh the page the query is successfully executed but after the first query the same problem persists. Sometimes I can refresh the page and the query will work and obtain a session.


Does my session cache have objects that are preventing hibernate from executing the query?


I have turned off the second level cache and I have put hooks in my code to see if the session is open on the second query and to my suprise the session is closed during the second query:



HOOKS:

System.out.println( "CartlineHAO LINE 63" + session.isOpen());
System.out.println( "CartlineHAO LINE 64" + session.isConnected());

RESULTS:

First Query:

08:39:17,912 INFO [STDOUT] CartlineHAO LINE 63true
08:39:17,913 INFO [STDOUT] CartlineHAO LINE 64true

Second Unsuccessfull: Lost My session ?


08:39:20,429 INFO [STDOUT] CartlineHAO LINE 63false
08:39:20,429 INFO [STDOUT] CartlineHAO LINE 64false




My Code:

public List getCartLineByCartLineId(Integer cartLineId) {

List cartLines = new ArrayList();
Session session = HibernateUtil.getSession();

try {
CartLineDO cartLine = new CartLineDO();
cartLine.setCartLineId(cartLineId);
Query results = session.createQuery("select cartId from com.rock.common.domain.dto.CartLineDO where cartLineId = :cartlineid")
.setEntity("cartlineid", cartLineId)
.setParameter("cartlineid", cartLineId, Hibernate.INTEGER);
//results.setCacheable(true);
cartLines = results.list();

} catch (Exception e) {
System.out.println(e);
}

return cartLines;
}

MBEAN:

<server>
<mbean code="org.jboss.hibernate.jmx.Hibernate"
name="rock.har:service=Hibernate">
<attribute name="DatasourceName">java:/ROCKPostgresDS</attribute>
<attribute name="Dialect">org.hibernate.dialect.PostgreSQLDialect</attribute>
<attribute name="SessionFactoryName">java:/hibernate/SessionFactory</attribute>
<attribute name="CacheProviderClass">org.hibernate.cache.HashtableCacheProvider</attribute>
<attribute name="ShowSqlEnabled">true</attribute>
</mbean>
</server>

I have also use session.close() and session.clear() to clear the session after the query with no success.

I am using JBoss-4.0.2 and Hibernate-3.1 connecting successfully with postgresql8.0. database.


Thank you to anyone who has any insight. ...Many Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 08, 2006 12:31 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
for clarification, by 2nd query do you mean the method you have above is called twice?

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject: second query
PostPosted: Wed Feb 08, 2006 1:00 pm 
Newbie

Joined: Wed Feb 08, 2006 11:04 am
Posts: 7
Location: Vail, Colorado
Yes the second query is this method called twice. Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 08, 2006 2:25 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
what is the full stack trace of the error, and what line is it pointing to

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject: Stack Trace
PostPosted: Wed Feb 08, 2006 3:24 pm 
Newbie

Joined: Wed Feb 08, 2006 11:04 am
Posts: 7
Location: Vail, Colorado
The null pointer exception is the only error that I get in my stack trace.

If I continue to refresh the page it eventually does return a value.
Sometimes it returns a value and sometimes a null pointer.

The only consistency is after I restart JBOSS a call to this method will return a value.

So strange. From the posted log it appears that the HQL query is executed the second time the method is called but simply returns a null pointer.

Thanks again


Top
 Profile  
 
 Post subject: Server Start Up Log
PostPosted: Wed Feb 08, 2006 4:27 pm 
Newbie

Joined: Wed Feb 08, 2006 11:04 am
Posts: 7
Location: Vail, Colorado
I have included the startup logs that may have pertinent information with regards to my Hibernate configuration.


Jboss startup log.

13:21:58,997 INFO [Configuration] Searching for mapping documents in jar: rock.har
13:21:58,998 INFO [Configuration] Found mapping document in jar: com/rock/common/domain/dto/AccountDO.hbm.xml
13:21:59,125 INFO [HbmBinder] Mapping class: com.rock.common.domain.dto.AccountDO -> account
13:21:59,150 INFO [Configuration] Found mapping document in jar: com/rock/common/domain/dto/BuildDO.hbm.xml
13:21:59,187 INFO [HbmBinder] Mapping class: com.rock.common.domain.dto.BuildDO -> build
13:21:59,188 INFO [Configuration] Found mapping document in jar: com/rock/common/domain/dto/CarDTO.hbm.xml
13:21:59,225 INFO [HbmBinder] Mapping class: com.rock.common.domain.dto.CarDTO -> CAR
13:21:59,225 INFO [Configuration] Found mapping document in jar: com/rock/common/domain/dto/CartDO.hbm.xml
13:21:59,265 INFO [HbmBinder] Mapping class: com.rock.common.domain.dto.CartDO -> cart
13:21:59,276 INFO [Configuration] Found mapping document in jar: com/rock/common/domain/dto/CartLineDO.hbm.xml
13:21:59,315 INFO [HbmBinder] Mapping class: com.rock.common.domain.dto.CartLineDO -> cartline
13:21:59,408 INFO [Configuration] Found mapping document in jar: com/rock/common/domain/dto/CartLineOptionDO.hbm.xml
13:21:59,431 INFO [HbmBinder] Mapping class: com.rock.common.domain.dto.CartLineOptionDO -> cartlineoption
13:21:59,433 INFO [Configuration] Found mapping document in jar: com/rock/common/domain/dto/InventoryDO.hbm.xml
13:21:59,457 INFO [HbmBinder] Mapping class: com.rock.common.domain.dto.InventoryDO -> INVENTORY
13:21:59,458 INFO [Configuration] Found mapping document in jar: com/rock/common/domain/dto/OptionDO.hbm.xml
13:21:59,483 INFO [HbmBinder] Mapping class: com.rock.common.domain.dto.OptionDO -> OPTION
13:21:59,485 INFO [Configuration] Found mapping document in jar: com/rock/common/domain/dto/OptionValueDO.hbm.xml
13:21:59,508 INFO [HbmBinder] Mapping class: com.rock.common.domain.dto.OptionValueDO -> optionvalue
13:21:59,509 INFO [Configuration] Found mapping document in jar: com/rock/common/domain/dto/ProductDO.hbm.xml
13:21:59,536 INFO [HbmBinder] Mapping class: com.rock.common.domain.dto.ProductDO -> product
13:21:59,538 INFO [Configuration] Found mapping document in jar: com/rock/common/domain/dto/SpecValueDO.hbm.xml
13:21:59,560 INFO [HbmBinder] Mapping class: com.rock.common.domain.dto.SpecValueDO -> specvalue
13:21:59,561 INFO [Configuration] Found mapping document in jar: com/rock/common/domain/dto/SpecificationDO.hbm.xml
13:21:59,582 INFO [HbmBinder] Mapping class: com.rock.common.domain.dto.SpecificationDO -> specification
13:21:59,583 INFO [Configuration] Found mapping document in jar: com/rock/common/domain/dto/StateDO.hbm.xml
13:21:59,603 INFO [HbmBinder] Mapping class: com.rock.common.domain.dto.StateDO -> state
13:21:59,604 INFO [Configuration] Found mapping document in jar: com/rock/common/domain/dto/StateDTO.hbm.xml
13:21:59,623 INFO [HbmBinder] Mapping class: com.rock.common.domain.dto.StateDTO -> state
13:21:59,624 INFO [Configuration] Found mapping document in jar: com/rock/common/domain/dto/WorkDO.hbm.xml
13:21:59,649 INFO [HbmBinder] Mapping class: com.rock.common.domain.dto.WorkDO -> work
13:21:59,652 INFO [Configuration] processing extends queue
13:21:59,652 INFO [Configuration] processing collection mappings
13:21:59,652 INFO [HbmBinder] Mapping collection: com.rock.common.domain.dto.CartDO.cartLines -> cartline
13:21:59,653 INFO [HbmBinder] Mapping collection: com.rock.common.domain.dto.CartLineDO.cartLineOptions -> cartlineoption
13:21:59,653 INFO [HbmBinder] Mapping collection: com.rock.common.domain.dto.OptionDO.optionValues -> optionvalue
13:21:59,654 INFO [HbmBinder] Mapping collection: com.rock.common.domain.dto.ProductDO.options -> OPTION
13:21:59,654 INFO [HbmBinder] Mapping collection: com.rock.common.domain.dto.ProductDO.specifications -> specification
13:21:59,654 INFO [HbmBinder] Mapping collection: com.rock.common.domain.dto.SpecificationDO.specValues -> specvalue
13:21:59,654 INFO [Configuration] processing association property references
13:21:59,654 INFO [Configuration] processing foreign key constraints
13:21:59,669 INFO [NamingHelper] JNDI InitialContext properties:{}
13:21:59,671 INFO [DatasourceConnectionProvider] Using datasource: java:/ROCKPostgresDS
13:21:59,834 INFO [SettingsFactory] RDBMS: PostgreSQL, version: 8.1.0
13:21:59,836 INFO [SettingsFactory] JDBC driver: PostgreSQL Native Driver, version: PostgreSQL 8.0 JDBC3 with SSL (build 311)
13:21:59,873 INFO [Dialect] Using dialect: org.hibernate.dialect.PostgreSQLDialect
13:21:59,882 INFO [TransactionFactoryFactory] Transaction strategy: org.hibernate.transaction.JTATransactionFactory
13:21:59,883 INFO [NamingHelper] JNDI InitialContext properties:{}
13:21:59,884 INFO [TransactionManagerLookupFactory] instantiating TransactionManagerLookup: org.hibernate.transaction.JBossTransactionManagerLookup
13:21:59,885 INFO [TransactionManagerLookupFactory] instantiated TransactionManagerLookup
13:21:59,885 INFO [TransactionManagerLookupFactory] instantiating TransactionManagerLookup: org.hibernate.transaction.JBossTransactionManagerLookup
13:21:59,885 INFO [TransactionManagerLookupFactory] instantiated TransactionManagerLookup
13:21:59,885 INFO [SettingsFactory] Automatic flush during beforeCompletion(): enabled
13:21:59,885 INFO [SettingsFactory] Automatic session close at end of transaction: enabled
13:21:59,885 INFO [SettingsFactory] JDBC batch size: 15
13:21:59,885 INFO [SettingsFactory] JDBC batch updates for versioned data: disabled
13:21:59,886 INFO [SettingsFactory] Scrollable result sets: enabled
13:21:59,887 INFO [SettingsFactory] JDBC3 getGeneratedKeys(): disabled
13:21:59,887 INFO [SettingsFactory] Connection release mode: auto
13:21:59,888 INFO [SettingsFactory] Default batch fetch size: 1
13:21:59,888 INFO [SettingsFactory] Generate SQL with comments: disabled
13:21:59,888 INFO [SettingsFactory] Order SQL updates by primary key: disabled
13:21:59,888 INFO [SettingsFactory] Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
13:21:59,893 INFO [ASTQueryTranslatorFactory] Using ASTQueryTranslatorFactory
13:21:59,893 INFO [SettingsFactory] Query language substitutions: {}
13:21:59,893 INFO [SettingsFactory] Second-level cache: enabled
13:21:59,893 INFO [SettingsFactory] Query cache: disabled
13:21:59,893 INFO [SettingsFactory] Cache provider: org.hibernate.cache.HashtableCacheProvider
13:21:59,895 INFO [SettingsFactory] Optimize cache for minimal puts: disabled
13:21:59,895 INFO [SettingsFactory] Structured second-level cache entries: disabled
13:21:59,904 INFO [SettingsFactory] Echoing all SQL to stdout
13:21:59,904 INFO [SettingsFactory] Statistics: disabled
13:21:59,904 INFO [SettingsFactory] Deleted entity synthetic identifier rollback: disabled
13:21:59,904 INFO [SettingsFactory] Default entity-mode: pojo
13:21:59,946 INFO [SessionFactoryImpl] building session factory
13:22:01,213 INFO [SessionFactoryObjectFactory] Not binding factory to JNDI, no JNDI name configured
13:22:01,213 INFO [NamingHelper] JNDI InitialContext properties:{}
13:22:01,215 INFO [SessionFactoryImpl] Checking 0 named HQL queries
13:22:01,215 INFO [SessionFactoryImpl] Checking 0 named SQL queries
13:22:01,216 INFO [Hibernate] SessionFactory successfully built and bound into JNDI [java:/hibernate/SessionFactory]
13:22:01,232 INFO [TomcatDeployer] deploy, ctxPath=/, warUrl=file:/usr/local/jboss-4.0.2/server/default/tmp/deploy/tmp51381rock.ear-contents/satcomweb.war/
13:22:01,415 INFO [PropertyMessageResources] Initializing, config='org.apache.struts.util.LocalStrings', returnNull=true
13:22:01,415 INFO [PropertyMessageResources] Initializing, config='org.apache.struts.action.ActionResources', returnNull=true
13:22:01,708 INFO [PropertyMessageResources] Initializing, config='com.rock.satcomweb.app.ApplicationResources', returnNull=true
13:22:01,722 INFO [ValidatorPlugIn] Loading validation rules file from '/WEB-INF/validator-rules.xml'
13:22:01,722 ERROR [ValidatorPlugIn] Skipping validation rules file from '/WEB-INF/validator-rules.xml'. No stream could be opened.
13:22:01,722 INFO [ValidatorPlugIn] Loading validation rules file from '/WEB-INF/validation.xml'
13:22:09,428 INFO [TilesPlugin] Tiles definition factory loaded for module ''.
13:22:09,469 INFO [EARDeployer] Started J2EE application: file:/usr/local/jboss-4.0.2/server/default/deploy/rock.ear
13:22:09,470 INFO [EARDeployer] Init J2EE application: file:/usr/local/jboss-4.0.2/server/default/deploy/storage.ear/
13:22:10,021 INFO [TomcatDeployer] deploy, ctxPath=/storage, warUrl=file:/usr/local/jboss-4.0.2/server/default/deploy/storage.ear/storage.war/
13:22:10,140 INFO [EARDeployer] Started J2EE application: file:/usr/local/jboss-4.0.2/server/default/deploy/storage.ear/
13:22:10,262 INFO [Http11Protocol] Starting Coyote HTTP/1.1 on http-0.0.0.0-80
13:22:10,377 INFO [ChannelSocket] JK: ajp13 listening on /0.0.0.0:8009
13:22:10,386 INFO [JkMain] Jk running ID=0 time=0/26 config=null
13:22:10,398 INFO [Http11Protocol] Starting Coyote HTTP/1.1 on http-0.0.0.0-443
13:22:10,407 INFO [Server] JBoss (MX MicroKernel) [4.0.2 (build: CVSTag=JBoss_4_0_2 date=200505022023)] Started in 37s:696ms

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 08, 2006 5:21 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
I can't believe that there's no more to that error stack trace.

HibernateUtil.getSession() is probably just returning the session object that it has inside it. Because it says your session is closed on the 2nd query I am inclined to believe that once you run your first query, clear the session and close it, that when you run your 2nd query HibernateUtil.getSession() just returns that same session which has already been closed.

you probably need to make a new open session in that method, or don't close the one inside your HibernateUtil object

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject: Lost session
PostPosted: Thu Feb 09, 2006 11:08 am 
Newbie

Joined: Wed Feb 08, 2006 11:04 am
Posts: 7
Location: Vail, Colorado
I agree with your disbelief that the error stack is simply a null pointer and no other error report. My next step is to check my error erporting to increase the logging level in hopes I can get back a better error.

Side Note: Even If I start a new session (log into the site again through a new browser) the error persists.

The only way to get a connected session is to do multiple refreshes of the page (calling the method over again) or restarting JBOSS.

I have removed any session.close() lines and instantiated a new session within the method and I run into the same problem.

My assumptions where that the session.clear() call would clear any objects that are in the session cache.

I appreciate your comments thanks

Kyle


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 09, 2006 11:26 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
what class is HibernateUtil an instance of

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject: Structure
PostPosted: Thu Feb 09, 2006 12:34 pm 
Newbie

Joined: Wed Feb 08, 2006 11:04 am
Posts: 7
Location: Vail, Colorado
I hope this helps to clarify. The comment that Hibernate is returning an empty object is very interesting.


I am including the code for my hibernate access object that I call from a business object along with the sevice locator code.



Hibernate Access Object


CartLineHAO.java


import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Order;

import java.util.List;
import java.util.ArrayList;
import com.rock.common.domain.util.HibernateUtil;
import com.rock.common.domain.util.ServiceLocator;
import com.rock.common.domain.dto.CartLineDO;
import com.rock.common.domain.dto.WorkDO;

public class CartLineHAO {

private static final String HIBERNATE_SESSION_FACTORY = "java:/hibernate/SessionFactory";

public List getCartLineByCartLineId(Integer cartLineId) {

List cartLines = new ArrayList();
Session session = HibernateUtil.getSession();

try {
CartLineDO cartLine = new CartLineDO();
cartLine.setCartLineId(cartLineId);
Query results = session.createQuery("select cartId from com.rock.common.domain.dto.CartLineDO where cartLineId = :cartlineid")
.setEntity("cartlineid", cartLineId)
.setParameter("cartlineid", cartLineId, Hibernate.INTEGER);
//results.setCacheable(true);
cartLines = results.list();

} catch (Exception e) {
System.out.println(e);
}


return cartLines;
}

Service Locator

ServiceLocator.java

package com.rock.common.domain.util;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
* <code>ServiceLocator</code> encapsulates JNDI lookups to make it
* easier to access JNDI-based resources (EJBs, DataSources,
* JMS Destinations, and so on).
*
*/
public class ServiceLocator {

/**
* Making the default (no arg) constructor private
* ensures that this class cannnot be instantiated.
*/
private ServiceLocator() {
}

/**
* Gets a <code>DataSource</code> using the given JNDI name.
*
* @param dataSourceJndiName The <code>DataSource</code>'s JNDI name.
*
* @return DataSource The <code>DataSource</code>.
*
* @throws ServiceLocatorException If there are JNDI lookup problems.
*
* @see javax.sql.DataSource
*/
public static DataSource getDataSource(String dataSourceJndiName)
throws ServiceLocatorException {
DataSource dataSource = null;

try {
Context ctx = new InitialContext();
dataSource = (DataSource) ctx.lookup(dataSourceJndiName);

} catch (ClassCastException cce) {
throw new ServiceLocatorException(cce);
} catch (NamingException ne) {
throw new ServiceLocatorException(ne);
}

return dataSource;
}

public static SessionFactory getHibernateSessionFactory(
String jndiSessionFactoryName) throws ServiceLocatorException {
SessionFactory sessionFactory = null;

try {
Context ctx = new InitialContext();
sessionFactory = (SessionFactory) ctx
.lookup(jndiSessionFactoryName);

} catch (ClassCastException cce) {
throw new ServiceLocatorException(cce);
} catch (NamingException ne) {
throw new ServiceLocatorException(ne);
}

return sessionFactory;
}

public static Session getHibernateSession(String jndiSessionFactoryName)
throws ServiceLocatorException {
Session session = null;

try {
session = getHibernateSessionFactory(jndiSessionFactoryName)
.openSession();
} catch (Exception e) {
throw new ServiceLocatorException(e);
}
return session;

}

}

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 09, 2006 12:47 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
I'm not too familiar with jndi connection stuff... but I think only instantiating one SessionFactory per application is the way to go, instead of creating a new one every time you want to open a new session.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject: Solution
PostPosted: Thu Feb 09, 2006 2:15 pm 
Newbie

Joined: Wed Feb 08, 2006 11:04 am
Posts: 7
Location: Vail, Colorado
Chris thank you for your insight.

I was trying to get the hibernate session factory that had been closed.
When I removed the threadSession.set(session); from my getSession() method in my HibernateUtil class everything works fine.


Thanks again for your help


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