NullPointerException in CassQuery

24 views
Skip to first unread message

Nick

unread,
Oct 28, 2014, 5:54:36 PM10/28/14
to kundera...@googlegroups.com
Hi,
We are implementing a HealthCheck function for our Kundera app, built on a Cassandra datastore. The implementation using a simple native query against the local system keyspace to ensure datastore connectivity.

We have 4 applications built, each accessing a different set of column families defined as JPA entities in different keyspaces of the Cassandra cluster. 3 of 4 applications have a perfectly functional healthcheck (written exactly the same in each app), but the 4th is throwing a NullPointerException (NPE).
Our code is:
    @Override
   public boolean isDataStoreAvailable() {
       int size = 0;
       try {
           final Query query = emf.createEntityManager().createNativeQuery("select cluster_name from system.local");
           @SuppressWarnings("unchecked")
           final List<String> result = query.getResultList();
           LOGGER.debug("cluster_name="+result);
           size = result.size();
       } catch (Exception e) {
           LOGGER.error("Unable to retrieve the cluster name from the system.local columnfamily", e);
           return false;
       }
       return size > 0;
   }

where emf is an injected EntityManagerFactory.

The application that has the problem with this native query is the only one of our apps that use JPA lifecycle methods. In one of our entities, we define a postLoad method, using the javax.persistence.PostLoad annotation.

When our app fails, we see the following messages in our log file:

Oct-28 16:27:49-TRACE [net.dataforte.cassandra.pool.ConnectionPool] - [Cassandra Connection Pool[1-1062412937]] ConnectionPool: automaticHostDiscovery=false
Oct-28 16:27:49-INFO  [com.impetus.kundera.persistence.EntityManagerFactoryImpl] - EntityManagerFactory created for persistence unit : nick-unit
Oct-28 16:29:02-DEBUG [com.nick.web.resource.StatusHandler] - [method => getStatus, annotations => @javax.ws.rs.GET(); @javax.ws.rs.Path(value=status/); @javax.ws.rs.Produces(value=[application/xml])]
Oct-28 16:29:02-DEBUG [com.impetus.kundera.persistence.EntityManagerImpl] - Creating EntityManager for persistence unit : nick-unit
Oct-28 16:29:02-INFO  [com.impetus.kundera.persistence.EntityManagerImpl] - Returning client instance for persistence unit nick-unit.
Oct-28 16:29:02-DEBUG [com.impetus.kundera.loader.GenericClientFactory] - Returning fresh client instance for persistence unit : nick-unit
Oct-28 16:29:02-DEBUG [com.impetus.kundera.persistence.EntityManagerImpl] - Created EntityManager for persistence unit : nick-unit
Oct-28 16:29:02-INFO  [com.impetus.kundera.query.QueryImpl] - On getResultList() executing query: select cluster_name from system.local
Oct-28 16:29:02-DEBUG [javax.persistence.EntityManager] - Callback >> PostLoad on com.nick.model.ProductPricing
Oct-28 16:29:02-DEBUG [javax.persistence.EntityManager] - Firing >> com.nick.model.ProductPricing.postLoad
Oct-28 16:29:02-ERROR [com.nick.dao.impl.HealthCheckDaoImpl] - Unable to retrieve the cluster name from the system.local columnfamily
java.lang.NullPointerException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.impetus.kundera.persistence.event.InternalCallbackMethod.invoke(InternalCallbackMethod.java:67)
at com.impetus.kundera.persistence.event.EntityEventDispatcher.fireEventListeners(EntityEventDispatcher.java:62)
at com.impetus.kundera.query.QueryImpl.handlePostEvent(QueryImpl.java:973)
at com.impetus.kundera.query.QueryImpl.getResultList(QueryImpl.java:153)
at com.nick.dao.impl.HealthCheckDaoImpl.isDataStoreAvailable(HealthCheckDaoImpl.java:40)

On my simplistic first glance, I would say that  com.impetus.kundera.persistence.event.EntityEventDispatcher.fireEventListeners(EntityEventDispatcher.java:62) should be checking for a null entity being passed in, but equally you could make a case for com.impetus.kundera.query.QueryImpl.handlePostEvent(QueryImpl.java:973) not explicitly passing a null in the call.

We are using Kundera 2.13 and have not yet tried upgrading to 2.14 as there was no encouragement in the release notes or JIRA.

If anyone can throw light on how this can fixed, or would like a JIRA defect creating, please let me know ASAP.

Thanks,

Nick.

Vivek Mishra

unread,
Oct 29, 2014, 12:46:04 AM10/29/14
to kundera...@googlegroups.com
Can you please share your entity definition and what you got to execute on PostLoad?

-Vivek

--
You received this message because you are subscribed to the Google Groups "kundera-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kundera-discu...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Nick

unread,
Oct 29, 2014, 12:54:22 PM10/29/14
to kundera...@googlegroups.com
I don't think it really matters about the contents of the postLoad. The problem is that QueryImpl calls fireEventListeners with the second parameter being a literal null, but EntityEventDispatcher doesn't check that the second parameter is not null (and indeed is of the correct type) before invoking callback.invoke(entity).
Is there a suitable unit test for QueryImpl to which I could add an entity with a PostLoad method to reproduce the error?
I have no problem contributing a fix to a simple problem like this.

Thanks,

Nick.

Vivek Mishra

unread,
Oct 29, 2014, 1:16:15 PM10/29/14
to kundera...@googlegroups.com
@Nick: Please go ahead and provide a fix. 

Yes, it sounds like a bug to me but to find a proper place holder to fix it i asked for entity definition.

As per 

It seems entity itself is null in this scenario. Did you try with

em.createNative(query, Entity.class)?

Here, Entity can be any Cassandra entity 

HTH,
-Vivek

Chhavi Gangwal

unread,
Oct 31, 2014, 1:06:16 AM10/31/14
to kundera...@googlegroups.com
 @Nick, the null check has been added in Kundera's current code, however inline with Vivek's opinion, in order to find out the complete issue, can you please share the entity definition ?

Also, did you try Vivek's suggestion ?

Chhavi
Reply all
Reply to author
Forward
0 new messages