Resultset getString throw ArrayIndexOutOfBoundsException

时间:2017-08-14 19:32:50

标签: java exception jdbctemplate hikaricp

尝试从ResultSet中检索String值时出现异常。在我向表中添加两个新列image_url和icon_url(默认为null)并尝试检索新添加的列之后,会发生这种情况。 表create语句如下:

CREATE TABLE `examples` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`description` text COLLATE utf8_unicode_ci,
`event_id` int(11) DEFAULT NULL,
`created_at` datetime NOT NULL,
`updated_at` datetime NOT NULL,
`session_id` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`location` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`start_date` datetime DEFAULT NULL,
`end_date` datetime DEFAULT NULL,
`url` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`button_label` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`hidden` tinyint(1) DEFAULT NULL,
`image_url` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`icon_url` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=427 DEFAULT CHARSET=utf8 
COLLATE=utf8_unicode_ci;

查询非常简单,只需查询示例对象中的所有字段:

@Override
public List<Example> getExamples(Integer eventId) {
    return jdbcTemplate.query(
            "SELECT * FROM examples WHERE event_id= ?",
            new Object[]{eventId},
            (rs, rowNum) -> {
                 Example ex = new Example();
                 ex.setId(rs.getInt("id"));
                 ex.setName(rs.getString("name"));
                 ex.setDescription(rs.getString("description"));
                 ex.setExpoId(rs.getInt("expo_id"));
                 ex.setSessionId(rs.getString("session_id"));
                 ex.setLocation(rs.getString("location"));
                 ex.setStartDate(rs.getString("start_date"));
                 ex.setEndDate(rs.getString("end_date"));
                 ex.setUrl(rs.getString("url"));
                 ex.setButtonLabel(rs.getString("button_label"));
                 ex.setHidden(rs.getBoolean("hidden"));
                 ex.setImageUrl(rs.getString("image_url")); //throw ArrayIndexOutOfBoundsException
                 ex.setIconUrl(rs.getString("icon_url")); // throw ArrayIndexOutOfBoundsException
            });
}

如果我将所有image_url和icon_url更新为空字符串而不是空值,则异常消失。

我使用HikariCP来配置和管理Datasouce:

   @Bean(name = "dataSouce")
   public DataSource dataSouce() {
    HikariConfig config = new HikariConfig();
    config.setJdbcUrl(env.getProperty("ds.url"));
    config.setUsername(env.getProperty("ds.username"));
    config.setPassword(env.getProperty("ds.password"));
    config.setDriverClassName(env.getProperty("ds.driver.classname"));
    config.addDataSourceProperty("cachePrepStmts", "true");
    config.addDataSourceProperty("prepStmtCacheSize", "250");
    config.addDataSourceProperty("useServerPrepStmts", "true");
    config.addDataSourceProperty("useLocalSessionState", "true");
    config.addDataSourceProperty("useLocalTransactionState", "true");
    config.addDataSourceProperty("rewriteBatchedStatements", "true");
    config.addDataSourceProperty("cacheResultSetMetadata", "false");
    config.addDataSourceProperty("cacheServerConfiguration", "true");
    config.addDataSourceProperty("elideSetAutoCommits", "true");
    config.addDataSourceProperty("maintainTimeStats", "false");
    return new HikariDataSource(config);
}

我已经调试了好几个小时,但没有找到正在发生的事情的运气,对HikariCP也很新,所以不确定它是否对缓存查询结果有一些影响。完整的异常跟踪如下:

2017-08-14 12:09:53.020 [http-apr-8080-exec-2] WARN  example.MvcLogger - Handler execution resulted in exception
java.lang.ArrayIndexOutOfBoundsException: 939
at com.mysql.cj.mysqla.io.Buffer.readInteger(Buffer.java:284)
at com.mysql.cj.mysqla.result.BinaryBufferRow.getValue(BinaryBufferRow.java:231)
at com.mysql.cj.jdbc.result.ResultSetImpl.getString(ResultSetImpl.java:880)
at com.mysql.cj.jdbc.result.ResultSetImpl.getString(ResultSetImpl.java:892)
at com.zaxxer.hikari.pool.HikariProxyResultSet.getString(HikariProxyResultSet.java)
at com.playstation.sony.otg.service.impl.EventsServiceImpl.lambda$getExamples$2(EventsServiceImpl.java:162)
at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:93)
at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:60)
at org.springframework.jdbc.core.JdbcTemplate$1.doInPreparedStatement(JdbcTemplate.java:708)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:644)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:695)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:727)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:737)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:787)
at com.playstation.sony.otg.service.impl.EventsServiceImpl.getExamples(EventsServiceImpl.java:158)
at com.playstation.sony.otg.controller.AdminController.eventIdExamplesGet(AdminController.java:48)
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.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:775)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:858)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at com.playstation.sony.otg.logging.logging.LoggerFilter.doFilter(LoggerFilter.java:46)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at com.playstation.sony.otg.config.SimpleCORSFilter.doFilter(SimpleCORSFilter.java:22)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:436)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2517)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2506)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)

自从我离开行业多年以来,我可能会犯一些非常愚蠢的错误,任何帮助都会受到赞赏!

1 个答案:

答案 0 :(得分:3)

MySQL Connector / J 6.0.5上有一个bug,其中有相同的例外情况已被关闭

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
    at com.mysql.cj.mysqla.io.Buffer.readInteger(Buffer.java:284)
    at com.mysql.cj.mysqla.result.BinaryBufferRow.getValue(BinaryBufferRow.java:227)

评论:

  

服务器端抛出了ArrayIndexOutOfBoundsException   使用了准备好的语句,BLOB,TEXT或者中有一个NULL   ResultSet中的JSON类型列

这已在Connector / J 8.0.7

中修复
  

服务器端抛出了ArrayIndexOutOfBoundsException   使用了准备好的语句,BLOB,TEXT或者中有一个NULL   ResultSet中的JSON类型列。 (Bug#25215008,Bug#84084)