post

Java项目中超时问题解决方案汇总

很久没写过技术类文章,懒了,今天同事分享了一份超时相关问题的解决办法,我记录到博客上,以备日后所需。多谢龚同学的分享。

在做项目中,遇到比较多的超时问题跟大家分享下,具体超时时间设置多少可以根据监控数据、应用容忍和并发线程执行回收的最大等待时间设定。

1、数据库连接超时设置方法

连接池中不能设置超时,如:jndi、dbcp,对于maxwait的设置是表示在连接池满的时候,取新连接时的等待时间,不是jdbc执行的超时时间
1.1  事务级别超时(可以针对某个事务单独设置)
Transaction timeout is a timeout valid in frameworks (Spring, EJB container) or at the application level.
In Spring, you may use XML as shown below or use @Transactional from Java source codes, for configuration.

<tx:attributes>
        <tx:method name="…" timeout="3"/>
</tx:attributes>

1.2  statment 超时(ibatis的config文件设置定义范围为全局,如果应用中可以统一设置超时,推荐此方法)
To use iBatis as an example, the default value can be configured by using @defaultStatementTimeout (秒) value insqlMapConfig/settings of sql-map-config.xml. By using @timeout value, you can configure statement, select, insert and update syntax of sql-map.xml separately.

1.3  jdbc driver socket 超时(可以针对单个jdbc driver单独设定,请注意超时时间的单位,如果不能统一设置超时,推荐单个连接设置,推荐此方法)
How to configure SocketTimeout is as explained below.

JDBC Driver connectTimeout Default Unit
socketTimeout Default Unit
Application Method
MySQL Driver connectTimeout 0 ms
socketTimeout 0 ms
Specify the option in the DriverURL.
Format:
jdbc:mysql://[host:port],[host:port]…/[database]
[?propertyName1][=propertyValue1][&propertyName2][=propertyValue2]…
Example:
jdbc:mysql://xxx.xx.xxx.xxx:3306/database?connectTimeout=60000&socketTimeout=60000
MS-SQL Driver
jTDS Driver
loginTimeout 0 sec
socketTimeout 0 sec
Specify the option in the DriverURL.
Format:
jdbc:jtds:://[:][/][;=[;…]]
Example:
jdbc:jtds:sqlserver://server:port/database;loginTimeout=60;socketTimeout=60
Oracle Thin Driver oracle.net.CONNECT_TIMEOUT 0 ms
oracle.jdbc.ReadTimeout 0 ms
Not possible with the driverURL. Must be delivered to the properties object via OracleDatasource.setConnectionProperties() API. When DBCP is used, use the following APIs:
BasicDatasource.setConnectionProperties()
BasicDatasource.addConnectionProperties()
CUBRID Thin Driver No separate configuration 5,000 ms
Not possible with the driverURL. Timeout occurs in 5 seconds.
Note 1: When timeout occurs with althost option specified in the URL, it can be connected to the designated host.
Note 2: C API can be used to state the login_time option in ms in the URL.
  • Note 1: The default value for connectTimeout and socketTimeout is “0,” which means that the timeout does not occur.
  • Note 2: You can also configure through properties without directly using the separate API of DBCP.

摘自:http://www.cubrid.org/blog/dev-platform/understanding-jdbc-internals-and-timeout-configuration/

2、ice

默认超时时间为-1,代表是无限期,就是没有超时时间,这就代表server挂了,会一直占用当前client的线程。
建议:在链接时加上 tcp -h xx -p xxx -t 2000 或者objectPrx.ice_timeout(2000) 单位为毫秒

3、cxf

默认连接时间超时为30秒,等待响应时间为60秒。这就代表server挂了,会一直占用当前client的线程最长30秒以上。
建议:
3.1.cxf spring xml beans头文件增加:

xmlns:context="http://www.springframework.org/schema/context"
       xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
      http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd  http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd"
<http-conf:conduit name="*.http-conduit">
   <http-conf:client ConnectionTimeout="2000" ReceiveTimeout="5000"/>单位为毫秒
</http-conf:conduit>

3.2.在资源文件目录下增加cxf.xml,cxf会自动读取(推荐):

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
       xsi:schemaLocation="http://cxf.apache.org/transports/http/configuration
           http://cxf.apache.org/schemas/configuration/http-conf.xsd
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd">
      <http-conf:conduit name="*.http-conduit">
        <http-conf:client ConnectionTimeout="2000" ReceiveTimeout="5000"/>单位为毫秒
       </http-conf:conduit>
</beans>

Comments

  1. 你猜我看得懂不》??

  2. 你猜我看得懂不》??

  3. 呃 java

  4. 呃 java

  5. 博客做的不错,来踩踩,求回访

  6. 博客做的不错,来踩踩,求回访

  7. java还真的不懂额

  8. java还真的不懂额

  9. 看不懂的飘过

  10. 看不懂的飘过

  11. 讨厌JAVA

  12. 讨厌JAVA

  13. 看成了:中超,哈哈,不是足球。

  14. 看成了:中超,哈哈,不是足球。

  15. 博客做的不错,来踩踩,求回访

  16. 博客做的不错,来踩踩,求回访

  17. 衡哥牛逼,我在Google上搜索第四个就是你这篇博客!https://www.google.com.hk/search?newwindow=1&safe=strict&es_sm=93&q=java+spring+%E6%95%B0%E6%8D%AE%E5%BA%93%E8%BF%9E%E6%8E%A5%E8%B6%85%E6%97%B6&spell=1&sa=X&ei=x4xrU6zgFMrg8AWFw4CAAQ&ved=0CCYQvwUoAA&biw=1920&bih=932

  18. 衡哥牛逼,我在Google上搜索第四个就是你这篇博客!https://www.google.com.hk/search?newwindow=1&safe=strict&es_sm=93&q=java+spring+%E6%95%B0%E6%8D%AE%E5%BA%93%E8%BF%9E%E6%8E%A5%E8%B6%85%E6%97%B6&spell=1&sa=X&ei=x4xrU6zgFMrg8AWFw4CAAQ&ved=0CCYQvwUoAA&biw=1920&bih=932

Trackbacks

  1. […] Java项目中超时问题解决方案汇总 […]

鬼娃娃进行回复 取消回复

*

· 3,340 次浏览