正在嘗試從ResultSet中的字符串值時,我得到異常。發生這種情況後,我加了兩個新列IMAGE_URL和icon_url表(默認爲空),並試圖檢索新添加的列。 表創建語句如下:結果集的getString拋出ArrayIndexOutOfBoundsException異常
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)
這可能是一些非常愚蠢的錯誤,我做了,因爲我已經離開行業工作多年,後,任何幫助表示讚賞!
什麼是你的MySQL版本? – user7294900