Java项目中超时问题解决方案汇总--解决cxf 时间 等待问题 bug~~~~

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

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

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.

1 <tx:attributes>
2         <tx:method name="…" timeout="3"/>
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头文件增加:

2        xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
5 <http-conf:conduit name="*.http-conduit">
6    <http-conf:client ConnectionTimeout="2000" ReceiveTimeout="5000"/>单位为毫秒
7 </http-conf:conduit>

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

02        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
03        xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
04        xsi:schemaLocation="http://cxf.apache.org/transports/http/configuration
05  
06 http://cxf.apache.org/schemas/configuration/http-conf.xsd
07  
08  
09 http://www.springframework.org/schema/beans
10  
11  
12 http://www.springframework.org/schema/beans/spring-beans.xsd">
13  
14       <http-conf:conduit name="*.http-conduit">
15         <http-conf:client ConnectionTimeout="2000" ReceiveTimeout="5000"/>单位为毫秒
16        </http-conf:conduit>
17 </beans>

版权声明:本文采用BY-NC-SA协议进行授权,转载张衡Henry的文章请注明出处
原文地址:http://www.izhangheng.com/java-jndi-dbcp-ice-cxf-timeout/
喜欢我的文章?赶快订阅RSS吧,及时阅读最新文章,或者关注我的微博,谢谢!

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值