Documente Academic
Documente Profesional
Documente Cultură
About
Downloads
Documenta on
Dev Zone
Community
Blog
Tweet
An applica on with a proper JDBC meout can cut down the failure me. In this ar cle we would like to talk about dierent kinds of meout values and recommended meout applica on methods when you import values from DBMS.
The en re service did not work normally a er a DDos a ack. The network was disconnected because L4 was not working, which caused WAS to be inoperable as well. Shortly a erwards, the security team blocked all DDos a acks, and restored the network back to normal. Yet, WAS was s ll not working. Through the ThreadDump of WAS, the service team was able to conrm that WAS had stopped during API call from JDBC. A er 20 minutes, WAS was s ll in WAITING status and the service was s ll not working. About 30 minutes had passed when an excep on suddenly occurred, and the service was restored.
Why was WAS in WAITING status for 30 minutes when QueryTimeout value was set to 3 seconds, and why did WAS start working again a er 30 minutes?
You can nd the answer if you understand how the JDBC Timeout works.
Figure 1: JDBC Type 4. Type 4 drivers process byte stream via sockets, and have the same basic opera ons as a network library like H pClient. This uses up a lot of CPU resources and loses response meout, while sharing the same error points with other network libraries. If you have used H pClient before, then you must have encountered errors from not se ng the meout value. Type 4 driver may have the same error (a hang occurs) if the socket meout value is not set properly. Let's learn about how to congure the socket meout value for JDBC driver, and what needs to be considered.
converted by Web2PDFConvert.com
Figure 2: Timeout Class. Figure 2 above shows a simplied version of the meout class when WAS and DBMS are communica ng. The higher level meout is dependent on the lower level meout. The higher level meout will operate normally only if the lower level meout operates normally as well. If the JDBC driver socket meout does not work properly, then higher level meouts such as statement meout and transac on meout will not work properly either. We have received a lot of comments that said:
Even a er the s ta tement meout wa s congured, the a pplica on s ll did not recover from the error beca us e the s ta tement meout did not work a t the me of network fa ilure.
The statement meout does not handle the meouts at the me of network failure. Statement meout does only one thing: restricts the opera on me of 1 statement. Handling meout to prevent network failure must be done by JDBC Driver. The JDBC driver's socket meout is aected by the OS's socket meout congura on. This would explain why JDBC connec on hang recovers 30 minutes a er the network connec on failure, even when the JDBC driver's socket meout is not congured. DBCP Connec on Pool is located on the le side of Figure 2. You can see that the meout classes and DBCP are separated. DBCP is in charge of crea ng and managing connec ons, and is not involved in processing meouts. When a connec on is created within DBCP or a valida on query is sent to check the validity of the connec on, the socket meout does aect these processes but does not aect the applica on directly. However, when getConnection() is called to DBCP from the applica on logic, then you can specify the meout un l the applica on acquires the connec on. However, this has nothing to do with the JDBC's connect meout.
converted by Web2PDFConvert.com
Transac on meout is used to limit the total statement processing me to the maximum amount allowed. For example, if it takes 0.1 second to process 1 statement, processing a few statements would not be a problem, but processing 100,000 statements would take 10,000 seconds (approx. 7 hours). Statement meout can be used here. EJB CMT (Container Managed Transac on) would be a typical example of actual implementa ons. EJB CMT varies in its implementa on methods and opera ng process depending on developers. NHN does not use EJB Container, so transac on meout of Spring Framework would be the most common example. In Spring, you may use XML as shown below or use @Transactional from Java source codes, for congura on.
1 2 3 < tx:attributes > < tx:method name="" timeout="3"/> </ tx:attributes >
Statement meout provided by Spring is very simple. It records the star ng me and the elapsed me for each transac on, and checks the elapsed me when an event occurs. If the meout is abnormal, it generates an excep on. In Spring, the connec on is stored in, and used from ThreadLocal. This is called Transac on Synchroniza on . When a connec on is saved in
ThreadLocal
, the star ng me and the meout me of the transac on is also recorded. When a statement is being created by using the proxy connec on, the elapsed me is checked to generate an excep on. The EJB CMT implementa on is done in a similar way. The structure itself is very simple. If the transac on meout is very important but the container or the framework you are using does not provide this feature, you could implement it yourself without major problems. There is no standard API for transac on meout. Lucy 1.5 and 1.6 Framework does not have a transac on meout feature, but you can get the same result by using Transac on Manager from Spring. If the processing me of the statement (5 or less) is 200 ms and the processing me of other business logics or framework opera on is 100 ms, the transac on meout me should be set to 1,100 ms ((200 * 5) + 100) or more.
converted by Web2PDFConvert.com
To use iBa s as an example, the default value can be congured by using @defaultStatementTimeout value in sqlMapCong/se ngs of sqlmap-cong.xml. By using @timeout value, you can congure statement, select, insert and update syntax of sql-map.xml separately. When MangedDatasource of Lucy 1.5 and 1.6 is used, the queryTimeout op on can be used to get a statement of which meout is congured at the datasource level. The statement meout me is congured based on the features of each applica on, so there is no recommended congura on value.
7. Sends a cancel message through the Connec onJDBC and cancels the query being executed.
converted by Web2PDFConvert.com
converted by Web2PDFConvert.com
Example:
jdbc:mysql://xxx.xx.xxx.xxx:3306/database?connectTimeout=60000&socketTimeout=60000
sec
sec Example:
jdbc:jtds:sqlserver://server:port/database;loginTimeout=60;socketTimeout=60
oracle.net.CONNECT_TIMEOUT 0
ms Not possible with the driverURL. Must be delivered to the proper es object via
converted by Web2PDFConvert.com
Not possible with the driverURL. Timeout occurs in 5 seconds. CUBRID Thin No separate congura on Driver 5,000 ms Note 1: When meout occurs with althost op on specied in the URL, it can be connected to the designated host. Note 2: C API can be used to state the login_ me op on in ms in the URL.
Note 1: The default value for connectTimeout and socketTimeout is "0," which means that the meout does not occur. Note 2: You can also congure through proper es without directly using the separate API of DBCP. When you congure proper es, pass on the character string where the key value is connec onProper es, and the format value is [propertyName=property;]*. The following example shows conguring proper es through xml in iBa s.
1 2 3 4 5 6 < transactionManager type="JDBC"> < dataSource type="com.nhncorp.lucy.db.DbcpDSFactory"> .... < property name="connectionProperties" value="oracle.net.CONNECT_TIMEOUT=6000;oracle.jdbc.ReadTimeout=6000"/> </ dataSource > </ transactionManager >
FAQ
Q1. I congured the query meout by using Statement.setQueryTimeout(), but it does not work as expected when there is a network error.
Query Timeout only works when it is connected to the s ocket correctly. Therefore, it ca nnot be us ed to s olve a n excep ona l s itua on with a network error. To be
prepa red for network errors , s ocket meout in JDBC driver mus t be congured.
Q2. How are transac on meout, statement meout and JDBC driver socket meout related to the DBCP congura on values?
When the connec on is a cquired from DBCP to JDBC, nothing but waitTimeout is a ected.
Q3. If JDBC SocketTimeout is congured, wouldn't the connec ons that stayed in idle status for a long me in DBCP be closed?
No. The s ocket op on is a pplied when the a ctua l da ta is being wri en or rea d, s o it does not a ect the connec ons in idle s ta tus in DBCP. The s ocket op on ca n ha ve
certa in eect when new connec ons tha t la ck in ins ide of DBCP a re crea ted, old idle connec ons a re removed, or the va lida on is checked, but this does not ca us e a ny s ignica nt is s ues unles s the network ha s a n error.
converted by Web2PDFConvert.com
JDBC driver becomes eec ve a er a network error occurs . A ca reful congura on for the va lue ca nnot prevent s uch the errors from ha ppening, but s ome mes s hortens the me tha t the network is dis a bled (if the network is res tored right a wa y).
By Woon Duk Kang, So ware Engineer at Web Pla orm Development Lab, NHN Corpora on.
See also
How to Analyze Java Thread Dumps
Dev Pla orm When there is an obstacle, or when a Java based Web applica on is running much slower than expected, we need to use ...
last year by Tae Jin Gu 10 67063
converted by Web2PDFConvert.com
1 comment
Leave a message...
Discussion Community Share
klyk
2 months ago
One can also set the socket properties with: DriverManager.getConnection(String url, Properties info) using the property names as in the above table. Note though that for Oracle the value should be in fact in milliseconds and not seconds as stated in table.
0
Reply
Share
Comment feed
converted by Web2PDFConvert.com