连接池中不能设置超时,如: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.