仅使用JDBC的Socket读取超时

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了仅使用JDBC的Socket读取超时相关的知识,希望对你有一定的参考价值。

我在一个Oracle WITHSELECT结构上得到一个异常,如果我把它粘贴到SQL Developer中,它可以正常工作,并在9.655s中返回。

[DEBUG] 2020-05-30 23:17:29.848 [http-nio-8090-exec-6] JdbcTransaction - [] Opening JDBC Connection
[DEBUG] 2020-05-30 23:17:30.336 [http-nio-8090-exec-6] JdbcTransaction - [] Setting autocommit to false on JDBC Connection [oracle.jdbc.driver.T4CConnection@c1d7134]
[DEBUG] 2020-05-30 23:17:30.336 [http-nio-8090-exec-6] getTotalUniqueUsersDetailsByKeyByTime - [] ==>  Preparing: with main_data as ( select /*+ parallel(SCHUC) parallel(SCHU) parallel(SCHUT) parallel(SCHCM) parallel(SCHET) parallel(SCHES) */ distinct schutm.user_type_meta_name as KEY ,schuc.time_unit as TIME_UNIT ,schuc.user_key as VALUE from ( /* BP1 */ /* BP4 */ select /*+ parallel(SCHUCD) parallel(SCHFQL) */ YEAR_MONTH as TIME_UNIT ,schucd.TRACE_ID_KEY ,schucd.platform_id ,schucd.user_key ,schucd.event_type_id ,schucd.event_source_id ,REFERRER_ID ,PARENT_ID ,sum(USAGE_COUNT) as usage_count from sc_report_new.sc_hub_usage_counts_day schucd WHERE FISCAL_QUARTER_ID IN ( ? ) group by YEAR_MONTH ,schucd.TRACE_ID_KEY ,schucd.platform_id ,schucd.user_key ,schucd.event_type_id ,schucd.event_source_id ,REFERRER_ID ,PARENT_ID ) schuc inner join sc_hub_users schu on schuc.user_key=schu.user_key inner join sc_hub_user_types schut on schut.user_type_id=schu.user_type_id inner join sc_hub_user_types_meta_map schutmm on schutmm.user_type_id=schut.user_type_id inner join sc_hub_user_types_meta schutm ON schutm.user_type_meta_id = schutmm.user_type_meta_id inner join sc_hub_content_master schcm on schcm.trace_id_key=schuc.trace_id_key inner join sc_hub_event_types schet on schet.event_type_id = schuc.event_type_id inner join sc_hub_event_sources sches on sches.event_source_id = schuc.event_source_id WHERE ( (schcm.PAGETYPE='Page' AND sches.event_source_name='wem' AND schet.event_code='V') OR (schcm.PAGETYPE='Page' AND sches.event_source_name='purchaseshubs' AND schet.event_code='S') OR (schcm.PAGETYPE='Hub' AND sches.event_source_name='saleshubs' AND schet.event_code='S') ) ) select /*+parallel(AUTO) */ KEY,time_unit,count(VALUE) as VALUE from main_data group by KEY,TIME_UNIT order by case when KEY = 'Other' then 1 else 0 end, KEY, TIME_UNIT 
[DEBUG] 2020-05-30 23:17:30.336 [http-nio-8090-exec-6] getTotalUniqueUsersDetailsByKeyByTime - [] ==> Parameters: 2020Q4(String)
org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.sql.SQLRecoverableException: IO Error: Socket read timed out
### The error may exist in com/mycompany/myapp/screport/mapper/hubportfolio/TotalUniqueUsers.xml
### The error may involve com.mycompany.myapp.screport.mapper.hubportfolio.TotalUniqueUsers.getTotalUniqueUsersDetailsByKeyByTime-Inline
### The error occurred while setting parameters
### SQL: with main_data as (                       select /*+ parallel(SCHUC)  parallel(SCHU)  parallel(SCHUT)  parallel(SCHCM) parallel(SCHET) parallel(SCHES) */              distinct                                           schutm.user_type_meta_name as KEY                                      ,schuc.time_unit as TIME_UNIT                ,schuc.user_key as VALUE         from                     (                                   /* BP1 */                                                          /* BP4 */                                       select /*+ parallel(SCHUCD) parallel(SCHFQL) */                             YEAR_MONTH as TIME_UNIT                            ,schucd.TRACE_ID_KEY                            ,schucd.platform_id                            ,schucd.user_key                            ,schucd.event_type_id                            ,schucd.event_source_id                            ,REFERRER_ID                            ,PARENT_ID                            ,sum(USAGE_COUNT)  as usage_count                                   from sc_report_new.sc_hub_usage_counts_day schucd                      WHERE  FISCAL_QUARTER_ID IN                                           (                                  ?                             )                      group by YEAR_MONTH                        ,schucd.TRACE_ID_KEY                        ,schucd.platform_id                        ,schucd.user_key                        ,schucd.event_type_id                        ,schucd.event_source_id                        ,REFERRER_ID                        ,PARENT_ID                                                                                                  ) schuc               inner join sc_hub_users schu         on schuc.user_key=schu.user_key                           inner join sc_hub_user_types schut                 on schut.user_type_id=schu.user_type_id                                       inner join sc_hub_user_types_meta_map schutmm                     on schutmm.user_type_id=schut.user_type_id                     inner join sc_hub_user_types_meta schutm                     ON schutm.user_type_meta_id = schutmm.user_type_meta_id                                         inner join sc_hub_content_master schcm         on schcm.trace_id_key=schuc.trace_id_key                   inner join sc_hub_event_types schet         on schet.event_type_id = schuc.event_type_id         inner join sc_hub_event_sources sches         on sches.event_source_id = schuc.event_source_id                                                                                                                                                                                                                           WHERE  (             (schcm.PAGETYPE='Page' AND sches.event_source_name='wem' AND schet.event_code='V')              OR              (schcm.PAGETYPE='Page' AND sches.event_source_name='purchaseshubs' AND schet.event_code='S')              OR              (schcm.PAGETYPE='Hub' AND sches.event_source_name='saleshubs' AND schet.event_code='S')         )                )          select /*+parallel(AUTO) */              KEY,time_unit,count(VALUE) as VALUE from main_data         group by KEY,TIME_UNIT         order by case when KEY = 'Other' then 1 else 0 end, KEY, TIME_UNIT
### Cause: java.sql.SQLRecoverableException: IO Error: Socket read timed out
    at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:149)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:140)
    at com.mycompany.myapp.screport.dao.SCHubPortfolioserviceHandler.getTotalUniqueUserDetailsByTime(SCHubPortfolioServiceHandler.java:296)
    at com.mycompany.myapp.screport.services.SCHubPortfolioService.getTotalUniqueUserDetailsByTime(SCHubPortfolioService.java:269)
    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.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:144)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:161)
    at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:160)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
    at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:326)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
    at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305)
    at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154)
    at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:473)
    at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:427)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:388)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:341)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:228)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at com.thetransactioncompany.cors.CORSFilter.doFilter(CORSFilter.java:174)
    at com.thetransactioncompany.cors.CORSFilter.doFilter(CORSFilter.java:237)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:200)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:834)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.sql.SQLRecoverableException: IO Error: Socket read timed out
    at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:897)

我在Java 1.8 JRE上使用Tomcat 9,MyBatis和Oracle 11g(我想)。 Oracle数据库不在我的机器上,但在同一个时区,延迟不是问题。 如果我采用上面的查询,将 "2020Q4 "的?替换为 "2020Q4",根据SQL Developer的说法,我可以在10秒内得到答案。

一些关键的Maven依赖包括。

        <dependency>
            <groupId>com.cisco.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.3</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.2</version>
        </dependency>

当我得到DataSource来建立连接时,我是这样做的。

        OracleDataSource result = new OracleDataSource();
        Class.forName("oracle.jdbc.driver.OracleDriver");
        String connectionString = lCiscoLifeJdbcUrl;
        String username = lCiscoLifeJdbcUserName; 
        String password = lCiscoLifeJdbcPassword;
        OracleDataSource oracleDataSource = (OracleDataSource)result; 
        oracleDataSource.setURL(connectionString);
        oracleDataSource.setPassword(password);
        oracleDataSource.setUser(username);
        String timeoutKey = "oracle.jdbc.ReadTimeout";
        Properties connectionProperties;
        try {
            connectionProperties = oracleDataSource.getConnectionProperties();
            if(connectionProperties==null) {
                connectionProperties = new Properties();
            }
            connectionProperties.put(timeoutKey, 60 /* minutes */ * 60 /* seconds per minutes */ * 1000 /* ms per seconds */);
        } catch (SQLException e) {
            e.printStackTrace();
        }

从之前的调试来看,connectionProperties默认为null,但我想允许比我更聪明的人设置这些属性。

另外,请注意,这个错误发生在64个测试中的8个左右,所以不是每个选择都会触发这个问题。 我没有做数据库更新或DML类型的语句。 在这一点上,一切都被假定为完全提交。

我使用的是MyBatis XML映射器文件,但这是 一个Spring驱动的项目。 我通过Java对象手动构建DataSource、Configuration、Environment。

我缺少了什么?

答案

能否设置连接属性,如图所示 DataSourceSample 另外,请查看 OracleConnection 的属性列表。

以上是关于仅使用JDBC的Socket读取超时的主要内容,如果未能解决你的问题,请参考以下文章

使用 jdbc 驱动程序读取大表时超时和内存不足错误

spring-boot jdbc 到 db2 的连接获取读取超时

socket读取超时报错

java - 如何超时读取Java Socket?

com.microsoft.sqlserver.jdbc.SQLServerException:连接超时(读取失败)

Spark JDBC 读取仅在一个分区中结束