Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring 3 NPE with LazyConnectionDataSourceProxy autoCommit
    primarykey
    data
    text
    <p>I'm using Spring 3 with JPA and I'm seeing an intermittent issue in my web application. I have wrapper for the JPA EntityManager that calls the underlying EntityManager crud methods. I see a NPE sometimes when I call <code>entityManager.persist(object);</code> It looks the the database connection is lost but I'm not 100% what is the cause. Anyone have any details on what may have caused the below exception? </p> <p>Spring Version: 3.0.6.RELEASE</p> <p><strong>Spring 3 LazyConnectionDataSourceProxy.java line 416:</strong></p> <pre><code>if (this.autoCommit != null &amp;&amp; this.autoCommit != this.target.getAutoCommit()) { this.target.setAutoCommit(this.autoCommit); } </code></pre> <p><strong>Exception:</strong></p> <pre><code>Caused by: java.lang.NullPointerException at org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy$LazyConnectionInvocationHandler.getTargetConnection(LazyConnectionDataSourceProxy.java:416) at org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy$LazyConnectionInvocationHandler.invoke(LazyConnectionDataSourceProxy.java:376) at $Proxy64.prepareStatement(Unknown Source) at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:534) at org.hibernate.jdbc.AbstractBatcher.prepareSelectStatement(AbstractBatcher.java:145) at org.hibernate.id.SequenceGenerator.generate(SequenceGenerator.java:96) at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:122) at org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGeneratedId(EJB3PersistEventListener.java:49) at org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:154) at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:110) at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:61) at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:646) at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:620) at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:624) at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:220) at sun.reflect.GeneratedMethodAccessor101.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:592) at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240) at $Proxy79.persist(Unknown Source) at myapp.api.dao.impl.GenericDAOImpl.save(GenericDAOImpl.java:50) at sun.reflect.GeneratedMethodAccessor100.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:592) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:155) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) at $Proxy119.save(Unknown Source) at myapp.api.service.impl.backoffice.StoringServiceImpl.store(StoringServiceImpl.java:89) at myapp.api.service.impl.backoffice.StoringServiceImpl.storeIncludedFeatureMessage(StoringServiceImpl.java:68) at sun.reflect.GeneratedMethodAccessor124.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:592) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) at $Proxy168.storeIncludedFeatureMessage(Unknown Source) at myapp.api.listener.backoffice.StorableMessageListener.processNew(StorableMessageListener.java:136) at myapp.api.listener.backoffice.StorableMessageListener.onMessage(StorableMessageListener.java:187) ... 34 more </code></pre> <p><strong>Spring Configuration:</strong></p> <pre><code> &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:hz="http://www.hazelcast.com/schema/spring" xsi:schemaLocation=" http://www.hazelcast.com/schema/spring http://www.hazelcast.com/schema/spring/hazelcast-spring-2.5.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"&gt; &lt;!-- Generic --&gt; &lt;context:annotation-config /&gt; &lt;context:component-scan base-package="myapp.api" /&gt; &lt;aop:aspectj-autoproxy/&gt; &lt;!-- JPA --&gt; &lt;bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /&gt; &lt;tx:annotation-driven /&gt; &lt;bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /&gt; &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt; &lt;property name="dataSource" ref="dataSource" /&gt; &lt;property name="jpaVendorAdapter" ref="jpaVendorAdapter" /&gt; &lt;property name="persistenceUnitName" value="MyApp" /&gt; &lt;property name="jpaProperties"&gt; &lt;props&gt; &lt;prop key="hibernate.use_sql_comments"&gt;true&lt;/prop&gt; &lt;prop key="hibernate.generate_statistics"&gt;true&lt;/prop&gt; &lt;prop key="hibernate.archive.autodetection"&gt;class&lt;/prop&gt; &lt;prop key="hibernate.cache.use_second_level_cache"&gt;true&lt;/prop&gt; &lt;prop key="hibernate.cache.provider_class"&gt;com.hazelcast.hibernate.provider.HazelcastCacheProvider&lt;/prop&gt; &lt;prop key="hibernate.cache.use_query_cache"&gt;true&lt;/prop&gt; &lt;prop key="hibernate.cache.use_minimal_puts"&gt;true&lt;/prop&gt; &lt;/props&gt; &lt;/property&gt; &lt;/bean&gt; &lt;hz:hazelcast id="instance"&gt; &lt;hz:config&gt; //rest of Hazelast config here &lt;/hz:config&gt; &lt;/hz:hazelcast&gt; &lt;hz:hibernate-region-factory id="regionFactory" instance-ref="instance"/&gt; &lt;!-- Define JPA Provider Adapter --&gt; &lt;bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt; &lt;property name="showSql" value="true" /&gt; &lt;property name="generateDdl" value="true" /&gt; &lt;property name="databasePlatform" value="org.hibernate.dialect.OracleDialect" /&gt; &lt;/bean&gt; &lt;bean id="dataSourceTarget" class="oracle.jdbc.pool.OracleDataSource" destroy-method="close"&gt; &lt;property name="URL" value="jdbc:oracle:thin:@server:1525:name" /&gt; &lt;property name="user" value="test" /&gt; &lt;property name="password" value="123" /&gt; &lt;property name="connectionCachingEnabled" value="true" /&gt; &lt;property name="connectionCacheProperties"&gt; &lt;props merge="default"&gt; &lt;prop key="MinLimit"&gt;5&lt;/prop&gt; &lt;prop key="MaxLimit"&gt;50&lt;/prop&gt; &lt;/props&gt; &lt;/property&gt; &lt;/bean&gt; &lt;bean id="dataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy"&gt; &lt;property name="targetDataSource" ref="dataSourceTarget"/&gt; &lt;/bean&gt; &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt; &lt;property name="entityManagerFactory" ref="entityManagerFactory"/&gt; &lt;property name="dataSource" ref="dataSource"/&gt; &lt;/bean&gt; &lt;tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="false"/&gt; &lt;bean id="genericDAO" class="myapp.api.dao.impl.GenericDAOImpl"&gt; &lt;constructor-arg&gt; &lt;value&gt;java.io.Serializable&lt;/value&gt; &lt;/constructor-arg&gt; &lt;/bean&gt; &lt;bean id="springContextHolder" class="myapp.api.util.SpringContextHolder" factory-method="getInstance" /&gt; &lt;bean id="executionInterceptor" class="myapp.api.listener.backoffice.ExecutionInterceptor" /&gt; &lt;/beans&gt; </code></pre> <p><strong>AOP Error Handler:</strong></p> <pre><code>package myapp.api.listener.backoffice; import javax.jms.Message; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.jms.core.JmsTemplate; import org.springframework.util.StopWatch; @Aspect public class ExecutionInterceptor extends BaseListener{ //protected Log log = LogFactory.getLog(this.getClass()); private String errorDestination="ErrorQ"; @Autowired @Qualifier("jmsTemplate") JmsTemplate jmsTemplate; @Around("execution(* onMessage(javax.jms.Message))") public Object aroundOnMessage(ProceedingJoinPoint pjp) throws Throwable{ StopWatch stopWatch = new StopWatch(); stopWatch.start(); Object retVal = pjp.proceed(); stopWatch.stop(); log.trace( pjp.getSignature().getName() + " Execution Time: " + stopWatch.getTotalTimeMillis()+" ms" ); return retVal; } @AfterReturning("execution(* onMessage(javax.jms.Message))") public void afterOnMessage(){ // logic to capture time log.debug("*****************************EXIT ONMESSAGE*******************************"); } @Around("execution(* commit(..))") public Object aroundCommit(ProceedingJoinPoint pjp) throws Throwable{ try{ return pjp.proceed(); }catch(Throwable ex){ log.error( "Unexpected Error occured during database commit routing message to ErrorQ", ex); jmsTemplate.convertAndSend(errorDestination, ex); throw ex; }finally{ log.trace( "Commiting Transaction..."); } } @AfterThrowing(value="execution(* onMessage(javax.jms.Message) throws java.lang.RuntimeException)",throwing="ex") public void afterThrowingOnMessage(JoinPoint jp,RuntimeException ex) throws RuntimeException{ log.trace("Unexpected Error occured during onMessage processing routing to ErrorQ"); log.error("{{ERROR}}", ex); Object[] args = jp.getArgs(); if (args!=null &amp;&amp; args[0] instanceof javax.jms.Message){ Message msg = (Message)args[0]; jmsTemplate.convertAndSend(errorDestination, msg); log.info("Unexpected Error occured successfully routed to ErrorQ"); }else log.info("Unexpected Error occured failed to route to ErrorQ"); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload