当前使用版本(必须填写清楚,否则不予处理)
V3.3.1.tmp
该问题是怎么引起的?(最新版上已修复的会直接close掉)
PaginationInterceptor.java:223中报空指针异常 DbType dbType = (DbType)Optional.ofNullable(this.dbType).orElse(JdbcUtils.getDbType(connection.getMetaData().getURL())); connection.getMetaData().getURL()为null,抛出空指针异常 我已在分页插件中设置 paginationInterceptor.setDbType(DbType.MYSQL);
重现步骤
我是用的mysql fabric,AtomikosNonXADataSourceBean 数据源,使用分页插件分页查询的时候报错
报错信息
n### The error may involve defaultParameterMap\r\n### The error occurred while setting parameters\r\n### Cause: com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: Error: The jdbcUrl is Null, Cannot read database type","detail":"Stack trace:\r\norg.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: \r\n### Error querying database. Cause: com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: Error: The jdbcUrl is Null, Cannot read database type\r\n### The error may exist in com/test/api/dao/mapper/global/TenantConfigMapper.java (best guess)\r\n### The error may involve defaultParameterMap\r\n### The error occurred while setting parameters\r\n### Cause: com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: Error: The jdbcUrl is Null, Cannot read database type\r\n\tat org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:92)\r\n\tat org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:440)\r\n\tat com.sun.proxy.$Proxy150.selectList(Unknown Source)\r\n\tat org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:223)\r\n\tat com.baomidou.mybatisplus.core.override.MybatisMapperMethod.executeForIPage(MybatisMapperMethod.java:134)\r\n\tat com.baomidou.mybatisplus.core.override.MybatisMapperMethod.execute(MybatisMapperMethod.java:96)\r\n\tat com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:96)\r\n\tat com.sun.proxy.$Proxy159.selectPage(Unknown Source)\r\n\tat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\r\n\tat sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)\r\n\tat sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\r\n\tat java.lang.reflect.Method.invoke(Method.java:498)\r\n\tat org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333)\r\n\tat org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)\r\n\tat org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)\r\n\tat org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:85)\r\n\tat com.nd.gaea.client.feign.aop.FeignContextAspect.doAround(FeignContextAspect.java:82)\r\n\tat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\r\n\tat sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)\r\n\tat sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\r\n\tat java.lang.reflect.Method.invoke(Method.java:498)\r\n\tat org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:627)\r\n\tat org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:616)\r\n\tat org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70)\r\n\tat org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)\r\n\tat org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)\r\n\tat org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)\r\n\tat org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)\r\n\tat
Comment From: linyuliang
目前重载该类,不使用Optional.ofNullable().orElse()方式,先赋值,判断null,再取orelse的值来规避 DbType dbType=this.dbType; if(dbType==null){ dbType=JdbcUtils.getDbType(connection.getMetaData().getURL()); }
Comment From: nieqiurong
用3.3.2.1-SNAPSHOT试试,使用方式参考https://mp.baomidou.com/guide/install.html#snapshot
Comment From: chenhaipeng
用3.3.2.1-SNAPSHOT试试,使用方式参考https://mp.baomidou.com/guide/install.html#snapshot
1、遇到同样的问题,类似一些云分布式数据库jdbcUrl 是空的,例如阿里TDDL,配置是从diamond获取,希望作者保留这个配置mybatis-plus.global-config.db-config.db-type=mysql 2、Optional.ofNullable().orElse(),感觉作者是理解错误,这个orElse()里面是必然执行一次,无论s是否ofNullable,应该使用的是orElseGet
Comment From: guanpingtang
我用的是阿里的TDDL,也遇到了这个问题,版本是:3.4.0. 请问有临时解决方案吗?
Comment From: yizi-xfl
我用的是阿里的TDDL,也遇到了这个问题,版本是:3.4.0. 请问有临时解决方案吗?
你好,你的问题解决了么?我和你一样,3.4.0的版本,也遇到了这个问题
Comment From: redfrogotr
用3.3.2.1-SNAPSHOT试试,使用方式参考https://mp.baomidou.com/guide/install.html#snapshot
1、遇到同样的问题,类似一些云分布式数据库jdbcUrl 是空的,例如阿里TDDL,配置是从diamond获取,希望作者保留这个配置mybatis-plus.global-config.db-config.db-type=mysql 2、Optional.ofNullable().orElse(),感觉作者是理解错误,这个orElse()里面是必然执行一次,无论s是否ofNullable,应该使用的是orElseGet
我看了很多遍大佬的回答,终于理解了第二句话的意思🤣。
// Optional.orElse
public T orElse(T other) {
return value != null ? value : other;
}
因为other需要先计算出来,JdbcUtils.getDbType(connection.getMetaData().getURL())就需要执行,此时可能出现 NPE。正常逻辑应该是:如果dbType非空,直接短路返回,而不应该再执行后面的逻辑。使用orElseGet因为参数为Supplier,就不会出现这样的问题了。