One-time Java Performance Tuning summary

Source: Internet
Author: User
Tags bulk insert throwable

Our system has newly developed a function of data extraction, after something is done, a look at the execution time that is called an annoyance. Refer to the execution time of the same function for similar systems, target: Compress local data processing time to less than 5 seconds.

The first step:

To know which areas need to be optimized, just by feeling or not, I use Btrace to find the reason for slow speed. The Btrace code for this use is posted below:

Import StaticCom.sun.btrace.BTraceUtils.name; Import StaticCom.sun.btrace.BTraceUtils.print; Import Staticcom.sun.btrace.BTraceUtils.println; Import StaticCom.sun.btrace.BTraceUtils.probeClass; Import StaticCom.sun.btrace.BTraceUtils.probeMethod; Import StaticCom.sun.btrace.BTraceUtils.str; Import StaticCom.sun.btrace.BTraceUtils.strcat; Import StaticCom.sun.btrace.BTraceUtils.timeMillis; ImportCom.sun.btrace.annotations.BTrace; ImportCom.sun.btrace.annotations.Kind; Importcom.sun.btrace.annotations.Location; ImportCom.sun.btrace.annotations.OnMethod; ImportCom.sun.btrace.annotations.TLS; /*** Monitoring method time-consuming **/@BTrace Public classPrinttimes {/*** Start Time*/@TLSPrivate Static LongStartTime = 0; /*** Called when method starts*/@OnMethod (Clazz= "Com.xxx.service.XXService", method = "/.+/")       Public Static voidStartmethod () {startTime=Timemillis (); }        /*** Call <br> at end of method*/@SuppressWarnings ("Deprecation") @OnMethod (Clazz= "Com.xxx.service.XXService", method = "/.+/", location =@Location (Kind.return)) Public Static voidEndMethod () {print (strcat (strcat (), Name (Probeclass ()),"."), Probemethod ()); Print ("  ["); Print (Strcat ("Time Taken:", str (Timemillis ()-( startTime))); println ("]"); }  }

This code matches all methods in the Com.xxx.service.XXService class, printing the execution time of each method, printing once per call, not total time. For more btrace content, please visit our website: https://kenai.com/projects/btrace

Step Two at this point, you already know which points are slow to focus on. What can I do to optimize this system?

1. Code aspect adjustment   A. parallelization (for an  adjustment to the number of thread pool threads, please refer to my other article on "setting the optimal number of threads" )   b. Increasing the cache
C. Where the insert operation is performed frequently, bulk insert with native SQL (persistence layer using Eclipselink)
2. Parameter adjustment A.JVM memory parameters B. Database connection pool parameters

3. The function realizes the design adjustment (least wants to see)

According to the actual situation to adopt appropriate means adjustment.

Step Three

Detect the performance of the system after optimization, if it is valid to retain, otherwise restore the code to the state before the optimization.

After the third step, proceed to the tertiary step. Until the performance meets the expected requirements.

Based on the analysis of optimized code, the parallelization is used to increase the cache and the operation of BULK insert optimization. No adjustment is required in terms of software parameters and functional design.

--------------- problem record -------------

When you change the insert operation to BULK INSERT, the following error is reported in the log:

caused By:org.apache.tomcat.dbcp.dbcp.SQLNestedException:Cannot get a connection, pool error Timeout waiting forIdle Object14:45:03.821 [pool-1-thread-2] ERROR Java.lang.throwable-at Org.apache.tomcat.dbcp.dbcp.PoolingDataSource.getConnection (poolingdatasource.java:114)14:45:03.822 [pool-1-thread-2] ERROR Java.lang.throwable-at Org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection (basicdatasource.java:1044)14:45:03.822 [pool-1-thread-2] ERROR java.lang.throwable-at org.eclipse.persistence.sessions.JNDIConnector.connect (jndiconnector.java:123)14:45:03.822 [pool-1-thread-2] ERROR java.lang.Throwable-... 39 More14:45:03.823 [pool-1-thread-2] ERROR java.lang.throwable-caused by:java.util.NoSuchElementException:Timeout waiting forIdle Object

There are a few ideas: 1. The database connection was not released successfully, (the database connection pool monitoring data can be seen in real time) 2. The database connection was released successfully, but some threads were not able to get the connection because multiple threads preempted the connection at the same time. (This scenario considers increasing the database connection pool)

Using Btrace to monitor the connection pool found that only getting the connection did not release the connection operation, which appears to belong to 1. Due to the use of Eclipselink thought Entitymanager. Close () can release the connection, after reviewing the Eclipselink official documents, the original is improper use, the following is to obtain the connection details:

Https://wiki.eclipse.org/EclipseLink/Examples/JPA/EMAPI
JPA 2.0
= Entitymanager.unwrap (java.sql.Connection.  Class);... entitymanager.gettransaction (). commit ();

Finally, the Connection.close () will be in the finally.

One-time Java Performance Tuning summary

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.