Spring 3 NPE与LazyConnectionDataSourceProxy autoCommit

前端之家收集整理的这篇文章主要介绍了Spring 3 NPE与LazyConnectionDataSourceProxy autoCommit前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在使用带有JPA的Spring 3,我在Web应用程序中看到了一个间歇性的问题.
我有JPA EntityManager的包装器,它调用底层的EntityManager crud方法.当我调用entityManager.persist(object)时,我有时会看到NPE;它看起来数据库连接丢失但我不是100%是什么原因.任何人都有关于可能导致以下异常的详细信息?

春季版:3.0.6.RELEASE

Spring 3 LazyConnectionDataSourceProxy.java第416行:

if (this.autoCommit != null && this.autoCommit != this.target.getAutoCommit()) {
    this.target.setAutoCommit(this.autoCommit);
}

例外:

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

弹簧配置:

 

AOP错误处理程序:

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 && 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");
    }
}
最佳答案
似乎NPE被抛出,因为连接为null(变量this.target).

之前的几行(LazyConnectionDataSourceProxy.java)从DataSource获得连接:

// Fetch physical Connection from DataSource.
this.target = (this.username != null) ? getTargetDataSource().getConnection(this.username,this.password) : getTargetDataSource().getConnection();

谷歌搜索我发现Oracle JDBC驱动程序可能会在以下情况下返回空连接(link):

ConnectionWaitTimeout

Specifies cache behavior when a connection is requested and there are
already MaxLimit connections active. If ConnectionWaitTimeout is
greater than zero,then each connection request waits for the
specified number of seconds or until a connection is returned to the
cache. If no connection is returned to the cache before the timeout
elapses,then the connection request returns null.

Default: 0 (no timeout)

所以,我猜,连接超时可以解释你的不一致问题.

原文链接:https://www.f2er.com/spring/431489.html

猜你在找的Spring相关文章