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.  [ 4 posts ] 
Author Message
 Post subject: NPE in AbstractBatcher during query (null connection)
PostPosted: Thu Jan 12, 2006 5:05 pm 
Regular
Regular

Joined: Wed Dec 21, 2005 6:57 pm
Posts: 70
I am migrating to Hibernate 3.1 and getting a NPE when doing a simple query (a Find All, with no where clause). I've traced the problem in the source and see that the issue is a null connection, which in turn comes from a connectionManager.getConnection() call. My question is why would this ever throw a NPE, and how can I debug the situation (hopefully without becoming a hibernate developer myself ;-> ).

The problem seems specific to our "User" object -- I can query other objects.


Hibernate version: 3.1

Full stack trace of any exception that occurs:
Code:
    [exec] 15:36:18,661 ERROR [UserHelper] java.lang.NullPointerException
    [exec] java.lang.NullPointerException
    [exec]      at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:442)
    [exec]      at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:368)
    [exec]      at org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:105)
    [exec]      at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1561)
    [exec]      at org.hibernate.loader.Loader.doQuery(Loader.java:661)
    [exec]      at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
    [exec]      at org.hibernate.loader.Loader.doList(Loader.java:2150)
    [exec]      at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029)
    [exec]      at org.hibernate.loader.Loader.list(Loader.java:2024)
    [exec]      at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:369)
    [exec]      at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:300)
    [exec]      at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:146)
    [exec]      at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1093)
    [exec]      at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)


Name and version of the database you are using: MySQL 4

The generated SQL (show_sql=true):
Code:
    [exec] 15:36:16,927 INFO  [STDOUT] LoginBean.ejbCreate()
    [exec] 15:36:17,896 INFO  [STDOUT] Hibernate: select user0_.user_guid as user1_94_, user0_.UserID as UserI
D94_, user0_.Password as Password94_, user0_.UserName as UserName94_, user0_.isActive as isActive94_, user0_.D
ataEntryDate as DataEntr6_94_, user0_.DataEnterer as DataEnte7_94_ from user user0_ where user0_.UserID='imsma
' and user0_.Password='5f4dcc3b5aa765d61d8327deb882cf99'
    [exec] 15:36:17,989 INFO  [STDOUT] Hibernate: select roles0_.user_guid as user2_1_, roles0_.role_guid as r
ole1_1_, role1_.role_guid as role1_86_0_, role1_.Role as Role86_0_, role1_.DataEntryDate as DataEntr3_86_0_, r
ole1_.DataEnterer as DataEnte4_86_0_ from user_has_role roles0_ left outer join role role1_ on roles0_.role_gu
id=role1_.role_guid where roles0_.user_guid=?
    [exec] 15:36:18,099 INFO  [STDOUT] com.fgm.imsma.ejb.UserBean.ejbCreate()
    [exec] 15:36:18,646 INFO  [STDOUT] Hibernate: select user0_.user_guid as user1_94_, user0_.UserID as UserI
D94_, user0_.Password as Password94_, user0_.UserName as UserName94_, user0_.isActive as isActive94_, user0_.D
ataEntryDate as DataEntr6_94_, user0_.DataEnterer as DataEnte7_94_ from user user0_


I browsed the source (UTSL) and found that the null pointer exception is thrown here (I've removed a lot of irrelevant lines for brevity):
Code:
class AbstractBatcher {
[...]
       private PreparedStatement getPreparedStatement(
         final Connection conn,
         [... args removed ...] )
   throws SQLException {

      sql = getSQL( sql );
      
      log(sql);

      log.trace("preparing statement");
      PreparedStatement result;
      if (scrollable) {
         [... code removed ...]
      }
      else {
         if (callable) {
            result = conn.prepareCall(sql);
         }
         else {
            result = conn.prepareStatement(sql);      <<<---- LINE 442
         }
      }


In turn, the offending null connection originates four stack frames up, in this method of AbstractBatcher:
Code:
   public PreparedStatement prepareQueryStatement(String sql, boolean scrollable, ScrollMode scrollMode)
   throws SQLException, HibernateException {
      logOpenPreparedStatement();
      PreparedStatement ps = getPreparedStatement( connectionManager.getConnection(), sql, scrollable, scrollMode ); <<<--- LINE 105


So why would connectionManager.getConnection() not return a connection?

In our dev environment someone may have a bad mapping, but I'd expect that to show up as a SQL or Hibernate error rather than a NPE. Please help with any insights on the root cause or how to debug this.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 5:43 pm 
Regular
Regular

Joined: Wed Dec 21, 2005 6:57 pm
Posts: 70
Additional Information about this: This same query runs standalone from a test. So something before the query is putting Hibernate into a state where connectionManager.getConnection() returns null.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 5:51 pm 
Regular
Regular

Joined: Wed Dec 21, 2005 6:57 pm
Posts: 70
It appears that a null connection is only returned if the ConnectionManager is closed (there is a guard that attempts to open the connection otherwise).

How would the ConnectionManager be in a closed state, and still be used?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 13, 2006 10:25 am 
Regular
Regular

Joined: Wed Dec 21, 2005 6:57 pm
Posts: 70
More info for the next person who has this problem: we are calling Session.close() in the login process, but not clearing out the ThreadLocal variable where we store the session, so a stale Session object is left on that thread. When our J2EE container next uses that thread it finds the old, closed session, which shows up in Hibernate as a NPE.


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