org.springframework.dao.EmptyResultDataAccessException
今天在做定时任务获取数据的时候遇到下面的错误:
org.springframework.dao.EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0 at org.springframework.dao.support.DataAccessUtils.requiredSingleResult(DataAccessUtils.java:71) at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:472) at org.springframework.jdbc.core.JdbcTemplate.queryForMap(JdbcTemplate.java:467) at zd.record.utils.AbstractRecordDBUtils.getRecordByUniqueProperty(AbstractRecordDBUtils.java) at zd.record.utils.RecordDBUtils.getRecordByUniqueProperty(RecordDBUtils.java) at zd.dms.service.ebuy.BaseInfoServiceImpl.saveOrUpdateData(BaseInfoServiceImpl.java:283) at zd.dms.service.ebuy.BaseInfoServiceImpl.batchFetchAndDisposeMerchantInfos(BaseInfoServiceImpl.java:242) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307) 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:107) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) at com.sun.proxy.$Proxy171.batchFetchAndDisposeMerchantInfos(Unknown Source) at zd.dms.job.ebuy.FetchBaseInfosJob.work(FetchBaseInfosJob.java:30) at zd.dms.utils.AbstractHibernateQuartzJob.execute(AbstractHibernateQuartzJob.java) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:273) at org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob.executeInternal(MethodInvokingJobDetailFactoryBean.java:264) at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:86) at org.quartz.core.JobRunShell.run(JobRunShell.java:223) at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:549)
解释:上面的错误是Spring中使用JdbcTemplate的queryForObject方法,当查不到数据时会抛出异常。如下源码:
public static <T> T requiredSingleResult(Collection<T> results) throws IncorrectResultSizeDataAccessException { int size = (results != null ? results.size() : 0); if (size == 0) { throw new EmptyResultDataAccessException(1); } if (results.size() > 1) { throw new IncorrectResultSizeDataAccessException(1, size); } return results.iterator().next();
可以看出,当results为空时,就会抛出EmptyResultDataAccessException异常,Spring这样做的目的是为了防止程序员不对空值进行判断,保证了程序的健壮性。另外,当results的size大于1时,还会抛出IncorrectResultSizeDataAccessException异常,以保证返回的记录只有一条。如果我们想查询结果为空时,返回null而不是抛出异常,该怎么办呢?
很简单,只需要在捕获EmptyResultDataAccessException,然后返回null,代码如下:
Object object = null; try { object = jdbcTemplate.queryForObject(); } catch (EmptyResultDataAccessException e) { return null; } return object;
【当你用心写完每一篇博客之后,你会发现它比你用代码实现功能更有成就感!】