shardingsphere.core.exception.ShardingException: Unsupported Date type:class java.lang.String

最近使用sharding做分表, 其中做一个简单的查询时出现以下报错.

Cause: org.apache.shardingsphere.core.exception.ShardingException: Unsupported Date type:class java.lang.String
	at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:150)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433)
	... 59 common frames omitted

经跟踪源码发现在执行到org.apache.ibatis.logging.jdbc.ResultSetLogger.invoke(Object proxy, Method method, Object[] params)时

@Override
  public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {
    try {
      if (Object.class.equals(method.getDeclaringClass())) {
        return method.invoke(this, params);
      }    
      Object o = method.invoke(rs, params);
      if ("next".equals(method.getName())) {
        if (((Boolean) o)) {
          rows++;
          if (isTraceEnabled()) {
            ResultSetMetaData rsmd = rs.getMetaData();
            final int columnCount = rsmd.getColumnCount();
            if (first) {
              first = false;
              printColumnHeaders(rsmd, columnCount);
            }
            //注意此处
            printColumnValues(columnCount);
          }
        } else {
          debug("     Total: " + rows, false);
        }
      }
      clearColumnInfo();
      return o;
    } catch (Throwable t) {
      throw ExceptionUtil.unwrapThrowable(t);
    }
  }

printColumnValues(columnCount);处报错.继续查看该方法发现其中colname = rs.getString(i);此处值类型为其实是Date类型.

继续跟踪此处源码到

org.apache.shardingsphere.shardingjdbc.jdbc.core.resultset.ResultSetUtil中执行convertDateValue时报错.
private static Object convertDateValue(final Object value, final Class<?> convertType) {
        Date date = (Date) value;
        switch (convertType.getName()) {
            case "java.sql.Date":
                return new java.sql.Date(date.getTime());
            case "java.sql.Time":
                return new Time(date.getTime());
            case "java.sql.Timestamp":
                return new Timestamp(date.getTime());
            default:
                throw new ShardingException("Unsupported Date type:%s", convertType);
        }
    }

解决方法:

此问题应该是一个打印sql查询结果的bug,只需要设置不打印sql结果即可.

其中isTraceEnabled()具体子类判断是否打印sql查询结果日志.

我的日志设置的是

mybatis.configuration.log-impl= org.apache.ibatis.logging.stdout.StdOutImpl

该类默认isTraceEnabled()返回true.所以将该配置去掉使用默认的配置即可.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值