0

I'm trying to configure spring framework, JPA & Jboss. So i've written an end to end chain for testing which simply finds an entity based on entity. Please find the below code.

try {
            /**
             * intentionally breaking code into two for testing
             * */
            AbstractJpaEntity entity = null;
            /**
             * below line of code throws exception
             * 1) Type, id & em, none of them found null at run-time
             * */
            if(em.find(type, id)!=null){
                entity = em.find(type, id);
            }
            return entity;
        } catch (NoResultException nre) {
            throw new WalletException("no entity found", nre);
        }

So whenever the above piece of code is executed em.find(type, id) throws null pointer exception. I tried to debug,i found id and type with defined values as expected and even em with following values :-

enter image description here

which says persistence unit defined in xml is properly loaded and same says the server boot logs. What is annoying is that even though nothing is null but still Nullpointer Exception is thrown. The stacktrace is as follows :-

[org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/AltSurvey].[rest]] (http--0.0.0.0-34567-2) Servlet.service() for servlet rest threw exception: java.lang.NullPointerException at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.getStatus(JtaStatusHelper.java:73) [hibernate-core-4.0.1.Final.jar:4.0.1.Final] at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.isActive(JtaStatusHelper.java:115) [hibernate-core-4.0.1.Final.jar:4.0.1.Final] at org.hibernate.engine.transaction.internal.jta.CMTTransaction.join(CMTTransaction.java:149) [hibernate-core-4.0.1.Final.jar:4.0.1.Final] at org.hibernate.ejb.AbstractEntityManagerImpl.joinTransaction(AbstractEntityManagerImpl.java:1207) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final] at org.hibernate.ejb.AbstractEntityManagerImpl.postInit(AbstractEntityManagerImpl.java:176) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final] at org.hibernate.ejb.EntityManagerImpl.(EntityManagerImpl.java:89) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final] at org.hibernate.ejb.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:125) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final] at org.hibernate.ejb.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:120) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_67] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [rt.jar:1.7.0_67] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0_67] at java.lang.reflect.Method.invoke(Method.java:606) [rt.jar:1.7.0_67] at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.invokeProxyMethod(AbstractEntityManagerFactoryBean.java:376) [spring-orm-3.1.1.RELEASE.jar:3.1.1.RELEASE] at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean$ManagedEntityManagerFactoryInvocationHandler.invoke(AbstractEntityManagerFactoryBean.java:517) [spring-orm-3.1.1.RELEASE.jar:3.1.1.RELEASE] at com.sun.proxy.$Proxy37.createEntityManager(Unknown Source) at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:234) [spring-orm-3.1.1.RELEASE.jar:3.1.1.RELEASE] at com.sun.proxy.$Proxy47.find(Unknown Source) at com.alt.survey.common.db.dao.AbstractJpaDao.findById(AbstractJpaDao.java:84) [classes:]

What looks fishy is persistence unit coming null in last :- enter image description here

I've already reviewed all of the XMLs associated with the configuration. But couldn't suspect any reason for the exception. I'm attacking XMLs below:-

persistence.xml

<persistence-unit name="TalentPactFormEngine_New"
        transaction-type="JTA">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>java:/jdbc/TalentPactFormEngine_New</jta-data-source>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
        <properties>
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect" />
            <property name="hibernate.default_schema" value="dbo" />
            <property name="hibernate.generate_statistics" value="true" />
            <property name="hibernate.cache.use_second_level_cache"
                value="true" />
            <property name="hibernate.cache.use_query_cache" value="true" />
            <property name="hibernate.cache.infinispan.cachemanager"
                value="java:jboss/infinispan/hibernate" />
        </properties>
    </persistence-unit>

hibernateJpaConfig.xml

<bean id="surveyDataSource" class="org.apache.commons.dbcp.BasicDataSource"
        scope="singleton" destroy-method="close">
        <property name="driverClassName" value="${driver}" />
        <property name="url" value="${url}" />
        <property name="username" value="${user}" />
        <property name="password" value="${password}" />
    </bean>

    <bean id="entityManagerFactory" depends-on="surveyDataSource"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="surveyDataSource" />
        <property name="persistenceXmlLocation" value="classpath:persistence.xml" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
    </bean>
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager" />

ApplicationContext.xml

<bean id="dbDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <!-- <property name="jndiName" value="java:/jdbc/TalentPactFormEngine_New" 
            /> -->
        <property name="environment" ref="remoteEnv" />
    </bean>

    <jee:jndi-lookup id="wcDataSource"
        jndi-name="java:/jdbc/TalentPactFormEngine_New" resource-ref="false"
        environment-ref="remoteEnv" expected-type="javax.sql.DataSource" />

    <util:properties id="remoteEnv">
        <prop key="java.naming.provider.url">http://survey.peoplestrong.com:34567
        </prop>
        <prop key="java.naming.factory.url.pkgs">org.jboss.naming:org.jnp.interfaces</prop>
        <prop key="java.naming.factory.initial">org.jnp.interfaces.NamingContextFactory</prop>
        <prop key="jnp.disableDiscovery">true</prop>
    </util:properties>

and finally the standalone of JBoss Server

<datasource jta="true" jndi-name="java:/jdbc/TalentPactFormEngine_New" pool-name="TalentPactFormEngine_New" enabled="true" use-java-context="true" use-ccm="true">
                    <connection-url>jdbc:jtds:sqlserver://192.168.0.226:1433/talentpactformengine_new</connection-url>
                    <driver>net.sourceforge.jtds</driver>
                    <security>
                        <user-name>bla</user-name>
                        <password>blabla</password>
                    </security>
                </datasource>

Couldn't find the reason for NullPointer Exception, Any Sort of help will be highly appreciable.

  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – ΦXocę 웃 Пepeúpa ツ Feb 03 '16 at 09:53
  • @Xoce root cause of the problem i've stated and link which you've shared , are totally different. Anyways thanks for taking interest. Lemme know i find any solution. – codechefvaibhavkashyap Feb 04 '16 at 06:39

1 Answers1

0

As you can see in the src code of org.hibernate.engine.transaction.internal.jta.JtaStatusHelper

the transaction Manager must be null

final int status = transactionManager.getStatus();

Check the configuration of the transaction manager.

Jens
  • 67,715
  • 15
  • 98
  • 113