Sunteți pe pagina 1din 9

| Login | Register

About

Downloads

Documenta on

Dev Zone

Community

Blog

Understanding JDBC Internals & Timeout Congura on


posted last year in Dev Pla orm category by Woon Duk Kang
Like 6 people like this. Sign Up to see what your friends like.

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.

Web Applica on Server became unresponsive a er a DDos a ack one day


(This is a close recons tu on of an actual event.)

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.

Why Do We Need to Know about the JDBC Driver?


When there is a performance issue or an error, WAS and DBMS are the two important ers we pay a en on to. In NHN, WAS and DBMS are generally handled by dierent departments, so each department tries to gure out this situa on by focusing on their own area of exper se. When this happens, you get a blind spot between WAS and DBMS, that does not receive much a en on. For Java applica ons, the blind spot would be between DBCP and JDBC. In this ar cle we will focus on JDBC.

What is a JDBC Driver?


JDBC is a standard API that you use to access the DBMS in Java applica ons. There are 4 types of JDBC drivers (Wikipedia) dened by Sun. NHN mainly uses the type 4. JDBC type 4 driver is wri en en rely in Java (pure Java) and communicates with a DBMS using sockets in Java applica ons.

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.

Timeout Class at WAS - DBMS Communica on

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

Figure 3: Timeout for Each Levels.

What is Transac on Timeout?


Transac on meout is a meout valid in frameworks (Spring, EJB container) or at the applica on level. Transac on meout can be an unfamiliar concept. Simply put, transac on meout is "
Statement Timeout * N (number of statements being processed) + @ (garbage collection, etc.)."

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.

What is Statement Timeout?


It is a limita on on how long a statement should run. It sets the meout value for the statement, which is a JDBC API. The JDBC driver processes the statement meout based on this value. Statement meout is congured via java.sql.Statement.setQueryTimeout(int timeout), which is a JDBC API. In recent developing environments, the developers rarely congure the statement meout value directly through Java source codes, but o en congure it by using the framework.

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.

Statement Timeout Execu on Process for JDBC Driver


Statement meout works dierently per DBMS and driver. The way it works is similar between Oracle and MS SQLServer. It is also similar between MySQL and CUBRID.

QueryTimeout for Oracle JDBC Statement


1. 2. 3. 4. 5. 6. 7. Creates a statement by calling Connection.createStatement(). Calls Statement.executeQuery(). The statement transmits the Query to Oracle DBMS by using its own connec on. The statement registers a statement to OracleTimeoutPollingThread (1 for each classloader) for meout process. Timeout occurs. OracleTimeoutPollingThread calls OracleStatement.cancel(). Sends a cancel message through the connec on and cancels the query being executed.

Figure 4: Query Timeout Execu on Process for Oracle JDBC Statement.

QueryTimeout for JTDS (MS SQLServer) Statement


1. Creates a statement by calling Connection.createStatement(). 2. Calls Statement.executeQuery(). 3. 4. 5. 6. The statement transmits the Q uery to M S SqlServer by using the internal connec on. The statement registers a statement in TimerThread for meout process. Timeout occurs. TimerThread calls up TsdCore.cancel() inside the JtdsStatement object.

7. Sends a cancel message through the Connec onJDBC and cancels the query being executed.

converted by Web2PDFConvert.com

Figure 5: QueryTimeout Execu on Process for JTDS (MS SQLServer) Statement.

QueryTimeout for MySQL JDBC Statement (5.0.8)


1. Creates a statement by calling Connection.createStatement(). 2. Calls Statement.executeQuery(). 3. 4. 5. 6. 7. 8. 9. The statement transmits the Q uery to MySqlServer by using the internal connec on. The statement creates a new meout-execu on thread for meout process. For version 5.1.x, it changes to assign 1 thread for each connec on. Registers the meout execu on to the thread. Timeout occurs. The meout-execu on thread creates a connec on that has the same congura ons as the statement. Transmits the cancel Q uery (K I LL Q UERY "connec onI d) by using the connec on.

Figure 6: QueryTimeout Execu on Process for MySQL JDBC Statement (5.0.8).

QueryTimeout for CUBRID JDBC Statement


1. Creates a statement by calling Connection.createStatement(). 2. Calls Statement.executeQuery(). 3. 4. 5. 6. 7. 8. The statement transmits the Q uery to CUBRI D D BM S by using the internal connec on. The statement creates a new meout-execu on thread for meout process. Registers the meout execu on to the thread. Timeout occurs. The meout-execu on thread creates a connec on that has the same congura ons as the statement. Transmits the cancel message using the connec on.

converted by Web2PDFConvert.com

Figure 7: QueryTimeout Execu on Process for CUBRID JDBC Statement.

What is Socket Timeout for JDBC Driver?


JDBC driver type 4 uses the socket to connect to the DBMS, and the connec on meout process between the applica on and the DBMS is not carried out by the DBMS. Socket meout value for JDBC driver is necessary when the DBMS is terminated abruptly or an network error has occured (equipment malfunc on, etc.). Because of the structure of TCP/IP, there are no means for the socket to detect network errors. Therefore, the applica on cannot detect any disconnec on with the DBMS. If the socket meout is not congured, then the applica on may wait for the results from the DBMS indenitely. (This connec on is also called a "dead connec on .") To prevent dead connec ons, a meout must be congured for the socket. Socket meout can be congured via JDBC driver. By se ng up the socket meout, you can prevent the innite wai ng situa on when there is a network error and shorten the failure me. It is not recommended to use the socket meout value to limit the statement execu on me. So the socket meout value must be higher than the statement meout value. If the socket meout value is smaller than the statement meout value, as the socket meout will be executed rst, and the statement meout value becomes meaningless and will not be executed. Socket meout has 2 op ons listed below, and their congura ons vary by driver. Timeout at socket connec on: Time limit for Socket.connect(SocketAddress endpoint, int timeout) Timeout at socket reading/wri ng: Time limit for Socket.setSoTimeout(int timeout) By checking the source for CUBRID, MySQL, MS SQL Server (JTDS) and Oracle JDBC, we conrmed that all the drivers we checked use the 2 APIs above. How to congure SocketTimeout is as explained below. JDBC Driver connectTimeout socketTimeout connectTimeout Default Unit Applica on Method Default Unit 0 ms Specify the op on in the DriverURL. Format:
jdbc:mysql://[host:port],[host:port].../[database] [? ms propertyName1][=propertyValue1][&propertyName2][=propertyValue2]...

MySQL Driver socketTimeout

Example:
jdbc:mysql://xxx.xx.xxx.xxx:3306/database?connectTimeout=60000&socketTimeout=60000

loginTimeout MS-SQL Driver jTDS socketTimeout Driver

sec

Specify the op on in the DriverURL. Format:


jdbc:jtds:<server_type>://<server>[:<port>][/<database>] [;<property>=<value>[;...]]

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

OracleDatasource.setConnectionProperties() API. When DBCP is used, use the

Oracle Thin Driver

following APIs: oracle.jdbc.ReadTimeout 0 ms


BasicDatasource.setConnectionProperties() BasicDatasource.addConnectionProperties()

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 >

OS Level SocketTimeout Congura on


If the socket meout or the connect meout is not congured, most of the me, applica ons cannot detect network errors. So, un l the applica ons are connected or are able to read data, they will wait indenitely. However, if you look at the actual issues NHN services encountered, the problems were o en resolved a er the applica ons (WAS) tried to reconnect to the network 30 minutes a er. This is because the OS can also congure socket meout me. Linux servers used by NHN have set the socket meout to 30 minutes. This checks the network connec on at the OS level. Because the KeepAlive checking cycle for NHN's Linux servers is 30 minutes, even when socket meout is set to 0, the DBMS network connec on problems caused by network issues do not surpass 30 minutes. Generally, the applica on hangs from network issues when the applica on is calling Socket.read(). However, depending on the network composi on or the error type, it can rarely be in wai ng status while running Socket.write(). When the applica on calls Socket.write(), the data is recorded to the OS kernel buer and then the right to control is returned to the applica on immediately. Thus, as long as a valid value is recorded to the kernel buer, Socket.write() is always successful. However, if the OS kernel buer is full due to a special network error, even Socket.write() can be put into wai ng status. In this case, the OS tries to resend the packet for a certain amount of me, and generates an error when it reaches the limit. In NHN's Linux server environment, the meout for this situa on is set to 15 minutes. I have explained the internal opera ons of JDBC so far. I hope that this will help you with the correct meout congura on and reducing errors. If you have more ques ons or any good informa on related to JDBC, please leave your comments below. Lastly, I have listed some of the frequently asked ques ons below.

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

Q4. How long should SocketTimeout be set to?


As I ha ve men oned in the ma in a r cle a bove, it mus t be much bigger tha n the s ta tement meout, a nd there is no recommended va lue. Socket meout va lue for the

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

MaxClients in Apache and its eect on Tomcat during Full GC


Dev Pla orm This is the fourth ar cle in the series of "Become a Java GC Expert". In the rst issue Understanding Java Garbage Collect...
3 months ago by Dongsoon Choi 0 5289

Spring Roo: Fast Java Applica on Development Tool


Dev Pla orm Spring Roo is a development tool that allows easy and fast Java Applica on development. Lets develop a Java applica on by ...
7 months ago by Kee Seon Baek 0 5590

How Statement Pooling in JDBC aects the Garbage Collec on


Dev Pla orm There are various techniques to improve the performance of your Java applica on. In this ar cle I will talk about Statement ...
10 months ago by Dongsun Choi 2 5212

jOOQ: Java Object Oriented Querying now supports CUBRID Database


CUBRID Apps&Tools It was great couple of weeks for CUBRID project! We opened this month with par cipa ng at the Russian Internet ...
11 months ago by Esen Sagynov 0 3915

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

Subscribe via email

About CUBRID | Contact us | 2012 CUBRID.org. All rights reserved.

converted by Web2PDFConvert.com

S-ar putea să vă placă și