这篇也是转自BEA的官方文档,源地址在BEA被Oracle收购后就转到Oracle官网了,所以留为备份。
Problem Description A JDBC connection which is used by an application or by WebLogic Server itself will block one WebLogic Server execute thread for the complete duration of the calls that are made via this connection. The JVM will ensure that the CPU is given to runnable threads by its thread scheduling mechanism, while the thread that blocks on a SQL query needs to wait. However, the thread occupied by the JDBC call will be reserved and used for the application until the call returns from the SQL query.
Even a transaction timeout will not kill or timeout any action that is done by the resources that are enlisted in this transaction. The actions will run as long as they take, without interruption. A transaction timeout will set a flag on the transaction that will mark it as rollback only, so that any subsequent request to commit this transaction will fail with a TimedOutException or RollbackException. However, as mentioned above, the long running JDBC calls can lead to blocked WebLogic Server execute threads, which can finally lead to a hanging instance, if all threads are blocked and no execute thread remains available for handling incoming requests. More recent WebLogic Server versions have a health check functionality that regularly checks if a thread does not react for a certain period of time (the default is 600 seconds). If this happens, an error message is printed to your log file similar to following: |
#### <000337> |
This does not interrupt the thread, as this is just a notification for the administrator. The only way a stuck thread becomes unstuck again is when the request it is handling finishes. In this case, you will find a message similar to following in your WebLogic Server’s log file: |
#### |
The time interval for the health check functionality is configurable. Please check StuckThreadMaxTime property in the
Top of Page Problem Troubleshooting This pattern addresses JDBC calls causing a server hang and other well known JDBC-related causes for common problems leading to hanging WebLogic Server instance. Other Support Patterns referenced in this pattern are at the WebLogic Server Support Patterns Site. Quick Links
Why does the problem occur?
Top of Page Synchronized DriverManager.getConnection() In addition to that, the constructor for a SQLException makes a DriverManager call, and most drivers have DriverManager.println() calls for logging, so any of these can block all other threads that issue a DriverManager call. DriverManager.getConnection() can take a relatively long time until it returns with the physical connection created to the database. Even if no deadlock occurs, all other calls need to wait until that one thread gets its connection. This is not a best practice in a multi-threaded system like WebLogic Server. |
This information is taken from http://forums.bea.com/bea//thread.jspa?forumID=2022&threadID=200063365&messageID=202311284&start=-1#202311284. |
Also our documentation clearly states that DriverManager.getConnection() should not be used: http://e-docs.bea.com/wls/docs81/faq/jdbc.html#501044.
If you prefer to use JDBC connections in your JDBC code, you should use a WebLogic Server JDBC connection pool, define a DataSource for it, and get the connection from the DataSource. This will give you all advantages from a pool (resource sharing, connection reuse, connection refresh if a database was down, etc). It also will help you avoid the deadlocks that may happen with DriverManager calls. See detailed information on how to use JDBC connection pools, DataSources, and other JDBC objects in WebLogic Server at: http://e-docs.bea.com/wls/docs81/jdbc/intro.html#1036718 and http://e-docs.bea.com/wls/docs81/jdbc/programming.html#1054307. A typical thread blocked in a DriverManager.getConnection() call looks like: |
“ExecuteThread-39″ daemon prio=5 tid=0×401660 nid=0×33 waiting for monitor entry [0xd247f000..0xd247fc68] at java.sql.DriverManager.getConnection(DriverManager.java:188) at com.bla.updateDataInDatabase(MyClass.java:296) at javax.servlet.http.HttpServlet.service(HttpServlet.java:865) at weblogic.servlet.internal.ServletStubImpl.invokeServlet (ServletStubImpl.java:120) at weblogic.servlet.internal.ServletContextImpl.invokeServlet (ServletContextImpl.java:945) at weblogic.servlet.internal.ServletContextImpl.invokeServlet (ServletContextImpl.java:909) at weblogic.servlet.internal.ServletContextManager.invokeServlet (ServletContextManager.java:269) at weblogic.socket.MuxableSocketHTTP.invokeServlet(MuxableSocketHTTP.java:392) at weblogic.socket.MuxableSocketHTTP.execute(MuxableSocketHTTP.java:274) at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:130)
|
Top of Page
Long Running SQL Queries Long running SQL queries block execute threads for their duration and until they return their result to the calling application. This means that a WebLogic Server instance needs to be configured to be able to handle enough calls simultaneously as they are requested by the application load. Limiting factors here are the number of execute threads and the number of connections in the JDBC connection pools. A general rule of thumb is to set the number of connections in the pool equally to the number of execute threads to enable optimal resource utilization. If JTS is used, some more connections in the pools should be available because connections may be reserved for transactions that are actually not active.
A thread hanging in a long running SQL call will show a very similar stack in a thread dump as the one for a hanging database. Please compare the next section for details. Hanging Database |
“ExecuteThread: ‘4′ for queue: ‘weblogic.kernel.Default’” daemon prio=5 tid=0×8e93c8 nid=0×19 runnable [e137f000..e13819bc] at java.net.SocketInputStream.socketRead0(Native Method) at java.net.SocketInputStream.read(SocketInputStream.java:129) at oracle.net.ns.Packet.receive(Unknown Source) at oracle.net.ns.DataPacket.receive(Unknown Source) at oracle.net.ns.NetInputStream.getNextPacket(Unknown Source) at oracle.net.ns.NetInputStream.read(Unknown Source) at oracle.net.ns.NetInputStream.read(Unknown Source) at oracle.net.ns.NetInputStream.read(Unknown Source) at oracle.jdbc.ttc7.MAREngine.unmarshalUB1(MAREngine.java:931) at oracle.jdbc.ttc7.MAREngine.unmarshalSB1(MAREngine.java:893) at oracle.jdbc.ttc7.Oall7.receive(Oall7.java:375) at oracle.jdbc.ttc7.TTC7Protocol.doOall7(TTC7Protocol.java:1983) at oracle.jdbc.ttc7.TTC7Protocol.fetch(TTC7Protocol.java:1250) – locked at oracle.jdbc.driver.OracleStatement.doExecuteQuery(OracleStatement.java:2529) at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout (OracleStatement.java:2857) at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:608) – locked – locked at oracle.jdbc.driver.OraclePreparedStatement.executeQuery (OraclePreparedStatement.java:536) – locked – locked at weblogic.jdbc.wrapper.PreparedStatement.executeQuery(PreparedStatement.java:80) at myPackage.query.getAnalysis(MyClass.java:94) at jsp_servlet._jsp._jspService(__jspService.java:242) at weblogic.servlet.jsp.JspBase.service(JspBase.java:33) at weblogic.servlet.internal.ServletStubImpl$ ServletInvocationAction.run(ServletStubImpl.java:971) at weblogic.servlet.internal.ServletStubImpl.invokeServlet (ServletStubImpl.java:402) at weblogic.servlet.internal.ServletStubImpl.invokeServlet (ServletStubImpl.java:305) at weblogic.servlet.internal.RequestDispatcherImpl.include (RequestDispatcherImpl.java:607) at weblogic.servlet.internal.RequestDispatcherImpl.include (RequestDispatcherImpl.java:400) at weblogic.servlet.jsp.PageContextImpl.include(PageContextImpl.java:154) at jsp_servlet._jsp.__mf1924jq._jspService(__mf1924jq.java:563) at weblogic.servlet.jsp.JspBase.service(JspBase.java:33) at weblogic.servlet.internal.ServletStubImpl$ ServletInvocationAction.run(ServletStubImpl.java:971) at weblogic.servlet.internal.ServletStubImpl.invokeServlet (ServletStubImpl.java:402) at weblogic.servlet.internal.ServletStubImpl.invokeServlet (ServletStubImpl.java:305) at weblogic.servlet.internal.WebAppServletContext$ ServletInvocationAction.run(WebAppServletContext.java:6350) at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:317) at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:118) at weblogic.servlet.internal.WebAppServletContext.invokeServlet (WebAppServletContext.java:3635) at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2585) at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:197) at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:170) |
The thread will be in running state. You should compare the threads in your different thread dumps in order to see if they receive the return from the SQL call in a timely manner or if they hang in this same call for a longer period of time. If the thread dumps seem to imply long response times from SQL calls, the corresponding database logs should be checked to see if problems in the database cause this slow performance or hang situation.
Top of Page Slow Network Deadlock |
java.sql.SQLException: ORA-00060: deadlock detected while waiting for resource at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:170) at oracle.jdbc.oci8.OCIDBAccess.check_error(OCIDBAccess.java:1614) at oracle.jdbc.oci8.OCIDBAccess.executeFetch(OCIDBAccess.java:1225) at oracle.jdbc.oci8.OCIDBAccess.parseExecuteFetch(OCIDBAccess.java:1338) at oracle.jdbc.driver.OracleStatement.executeNonQuery(OracleStatement.java:1722) at oracle.jdbc.driver.OracleStatement.doExecuteOther(OracleStatement.java:1647) at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:2167) at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate (OraclePreparedStatement.java:404) |
As it generally can take some time until a database detects a deadlock and resolves it by rolling back one or more transactions that cause the deadlock, one or more execute threads will be blocked until the rollback has finished.
RefreshMinutes or TestFrequencySeconds Pool Shrinking As these activities can take some time, the related application requests may take an unexpectedly long time which can lead users to assume that the system hangs. Information on how to optimize JDBC connection pool configurations is provided in Investigating JDBC Problems Pattern. Top of Page Analysis of a hanging WebLogic Server instance Most times it will be helpful to start with taking thread dumps from the hanging system in order to find out what is going on, e.g., what the different threads are doing and why they hang. Generally, thread dumps can be taken on production systems, however caution is necessary for very old versions of the JVM (<1.3.1_09), as they may crash during thread dumps. Also if the WebLogic Server instance has a huge number of threads, it will mean that the thread dump will take awhile to complete, while the rest of the threads are blocked. Please take more than one thread dump (5 to 10) with a delay of some seconds in between. This gives you the possibility to check the progress of the different threads. Also it will show if the system actually hangs (no progress at all) or if the throughput is extremely slow, which can seem to be a hanging system. Information on how to take thread dumps is provided in “Generic Server Hang” support pattern or in our documentation: http://e-docs.bea.com/wls/docs81/cluster/trouble.html. Also please check if the complete WebLogic Server instance hangs or if it is the application that hangs. “Generic Server Hang” support pattern also includes this information. Analyzing the thread dumps can show if one of the reasons mentioned in the previous section Why does the problem occur? actually is responsible for your hanging instance. If for example all your threads are in a DriverManager method like getConnection() then you have identified the root cause and need to change your application to use a DataSource or Driver.connect() instead of DriverManager.getConnection(). A very useful tool, Samurai, can be used to analyze thread dumps and to monitor the progress of threads between different thread dumps. This can be downloaded from dev2dev at: http://dev2dev.bea.com/resourcelibrary/utilitiestools/adminmgmt.jsp. A whitepaper on analyzing thread dumps on dev2dev: http://dev2dev.bea.com/products/wlplatform81/articles/thread_dumps.jsp will also be helpful in going deeper into the thread dumps to find out more about the server hang. Top of Page Tips and Tricks to optimize your JDBC code and JDBC connection pool configuration JDBC Programming You can view comprehensive information on JDBC that will help to optimize your JDBC code and the utilization of your JDBC resources on dev2dev Java Database Connectivity page at: http://dev2dev.bea.com/technologies/jdbc/index.jsp. JDBC Connection Pool Configuration |
Top of Page
Known Issues You can periodically review the Release Notes for your version of WLS for more information on Known Issues or Resolved Issues in Service Packs and browse for JDBC server hang-related issues. For your convenience, see the following:
Please note that changes have been made in WLS 8.1 SP3 to resolve CR134921, where for certain JDBC connections, the call to roll back a transaction was not being handled immediately because the driver had to wait for any currently-executing statement to return. |
Need Further Help? If you have followed the pattern, but still require additional help, you can:
If this does not resolve your issue and you have a valid Support Contract, you can open a Support Case by logging in at: http://support.bea.com/ . |
FEEDBACK Please provide us input on whether or not this Support Diagnostic Pattern “JDBC Causes Server Hang” helped, any clarifications you needed, and any requests for new topics to Support Diagnostic Patterns. |
DISCLAIMER NOTICE: BEA Systems, Inc. provides the technical tips and patches on this Website for your use under the terms of BEA’s maintenance and support agreement with you. While you may use this information and code in connection with software you have licensed from BEA, BEA makes no warranty of any kind, express or implied, regarding the technical tips and patches. Any trademarks referenced in this document are the property of their respective owners. Consult your product manuals for complete trademark information. |
备注:
本文转载自:http://www.hashei.me/2009/08/jdbc_causes_server_hang.html