Tomcat optimization Summary

Source: Internet
Author: User
The system encountered serious performance problems because it was urgent to cancel its vacation plan and rush to the site to debug the system.

After that, check the tomcat configuration file server. XML, I always thought it was because the configuration server did not change the default allowed access limit of 75 in Tomcat. Later I found that this was not the problem, the setting of context in the configuration file is a very strange one: there is a context path in the directory AAA in % Tomcat %/webapps/root, so if a user accesses URL: http: // serverip: Port/AAA, should the system use classes in the root/WEB-INF or should they use classes in the root/AAA/WEB-INF? Download the two WEB-INF in the server, decompile the part of the classification with Jad, found that not consistent. Finally, the analysis shows that Tomcat is nested in the root context. Tomcat uses the web-INF in the root directory.

Then, the database connection pool configuration is added for the program. It is proved that if the Tomcat database connection pool technology is not used, Tomcat will support less than 1 minute when the number of concurrent connections reaches. After Tomcat's database connection pool technology is used, Tomcat can run normally for about 30 minutes. Configure the database connection pool as follows: Modify the tomcat configuration file server. xml and add it to the context tab:

6. Factory
7. Org. Apache. commons. DBCP.
8. basicdatasourcefactory

11. Driverclassname
12. Oracle. JDBC. Driver. oracledriver

15. URL
16. JDBC: oracle: thin: @ 1521: dbname

20. Username
21. Yourname

24. Password
25. Yourpasswd

28. Maxactive
29. 1000

32. Maxidle
33. 20

35. Maxwait
36. -1
Maxactive is the maximum number of activated connections. The value is 1000, indicating a maximum of 1000 database connections. Maxidle is the maximum number of idle connections. The value is 20, indicating that 20 idle connections can be maintained even if no database connection is available without being cleared. The connections are always on standby. Maxwait is the maximum number of waiting seconds. Here, the value-1 indicates unlimited waiting until the timeout period ends. The value can also be set to 9000, indicating timeout after 9 seconds.
Modify the Web. xml file and add
[The XML fragment to be added has been filtered out by the system. Hehe: Leave a placeholder.]
Copy the JDBC driver classes12.jar of Oracle to common/lib in the tomcat installation directory. Create a simple test page test2.jsp:
1. Context initctx = new initialcontext ();
3. Context envctx = (context) initctx. Lookup ("Java: COMP/ENV ");
5. DS = (datasource) envctx. Lookup ("JDBC/oracledb ");
7. Connection Cn = Ds. getconnection ();

The database connection pool configuration is complete. However, if the server dies once in half an hour, it is unacceptable. Start checking Tomcat output file % Tomcat %/logs/Catalina in detail. the output of all Tomcat consoles is recorded in this file. The error message in Java is usually printed here. After self-analysis, we found that every time Tomcat died, it was a bunch of errors such as socketexception: Too program open files. This error is certainly not a program problem.
It may be because every TCP connection to port 80 of the Linux server has a file transfer process. The Tomcat server opens a file (such as a file) and sends it to the requested client browser, therefore, if you access Tomcat too many people, it will break the default 1024 file limit in Linux.
Add the following to the/root/. bash_profile file:
Ulimit-N 4096
You can automatically execute the ulimit command at each system startup to adjust the maximum number of file uploads to 4096. Note that the ulimit command is intended for an open terminal. For example, if you run ulimit on a terminal, however, this only affects the number of commands run on your terminal and the number of files opened by the program. If another terminal is opened again, the effect is still not before the ulimit-N 4096 is run. You can experiment with it.
In this way, the server can run normally for about two hours. After using the TOP command to view the memory occupied by the Java Process, it is found that after the memory usage reaches MB, it will not increase (the server has 8 GB of memory ). After checking on the Internet, we know that Tomcat can use MB of memory by default. Due to the large number of online users, Tomcat's default memory of MB is insufficient. Therefore, modify the Tomcat Startup File Catalina. SH: Add java_opts = '-xms256m-xmx1024m' after "Echo" using java_home: $ java_home "." 256 m "indicates that the initial Tomcat memory is m during initialization, 1024 MB indicates that the maximum memory available for Tomcat is MB.
After this modification, the server is much more stable than the original one, but sometimes it is found that some inexplicable database connection errors will be reported during user login. Finally there was an error like ORA-00020: Maximum number of process (150) exceeded when you were using the Oracle client golden to connect to Oracle. This is because the database server has too many connections to access and the ORA-00020: Maximum number of process (150) exceeded error occurs. In Oracle 9i, the default number of connections is 150. to modify this configuration file, you need to modify the processes value in the spfileorcl. ora file. (In 8.1.5, It is init. ora file, modify init. ora file is invalid) This file is a binary file and cannot be opened directly using notepad Editor, otherwise the error ORA-27101 shared memory realm does not exist will be reported. Use the ultraedit or editplus editor that can edit the binary file to open this file (directly edit the binary file), and then restart the Oracle server in the Windows service.
The current situation is very good, and the problem is gradually improving. However, the system performance is still very poor, and the webpage access speed is very bad. Check the Oracle server on the win2003 server where oracle is installed, and find that the disk usage is very high, which is basically consistent with 100% usage. Go to Oracle for analysis and find that two SQL statements occupy the system resources very much, which is the bottleneck of the two sqls.
Therefore, optimize the SQL statements that used trim.
Because the trim command causes a large number of hard disk reads and writes (the index is invalid after trim is used), modify all the statements using the trim command to use the rpad command.
For example:
Select sfzh from qjgkxsk where trim (sfzh) =: 1
Select sfzh from qjgkxsk where sfzh =: rpad (1)

Suggestions for table structure: If the table structure contains less than 200 fields, we recommend that you use the char type instead of the varchar type.

Then it is found that when a unique serial number is generated, there is a lock table operation in the program, causing a biggest bottleneck. The original practice was to lock a table, obtain the maximum value, and add the maximum value to the database.
1. PS = conn. preparestatement ("lock table" + global. xsk + "in exclusive mode ");
2. ps.exe cuteupdate ();
3. PS = conn. preparestatement ("select max (SNO) from" + global. xsk + "where SnO is not null ");
4. rs = ps.exe cutequery ();
5. Rs. Next ();
6. If (Rs. getstring (1 )! = NULL)
7 .{
8. Temp = Global. formatstring ("" + (integer. parseint (Rs. getstring (1) + 1), "0", 6 );
9 .}
10. Else
11 .{
12. Temp = "000001 ";
13 .}

The improved method is to add an Oracle Auto-increment 1 sequence (Oracle, MySQL, DB2, and so on, it does not have the primary key auto-increasement function)
1. PS = conn. preparestatement ("select seq_qjgksno.nextval from dual ");
2. rs = ps.exe cutequery ();
3. Rs. Next ();
4. Temp = Global. formatstring ("" + (integer. parseint (Rs. getstring (1), "0", 6 );

After these steps, I felt that all the system problems had been solved. However, the system would crash once in a few hours. This time, a Java virtual machine error was reported, it is estimated that someone else installed 1.5beta at first.
Summarize the optimization experience:
1. grasp the main contradictions of things. In fact, at the beginning, many optimizations to Web servers were secondary contradictions. When the last two SQL statements were solved, many of the preceding problems would not occur, for example, if the number of files opened by default in Linux is 1024, it is found that if the system database access speed is fast enough, there will not be so many files opened at all, it is often very slow in the home system, so it causes constant use of F5 to refresh and destroy tomcat.
2. In this very important situation, you must be cautious when modifying the program or configuration. When I modified the Oracle configuration file, there was no backup at first. Later, it was a text file, and I simply opened a number to modify it. However, although the text is displayed when you use NotePad to open the file, some binary files are damaged when the file is opened by notepad, resulting in Oracle server startup failure, at that time, I was scared to death. Fortunately, a student's machine also had Oracle9i, which was configured by default. He called to send the file via MSN. If there is no such file, or the server is installed in a strange way, it will be miserable. After that, I backed up the data at any time for every operation.
3. when I changed the program, there was a constant call from the examinee to ask questions about the server. At that time, the pressure was very high and it was necessary to withstand the pressure. If I felt I could not afford it, I 'd like to take a rest first, then the efficiency may be higher.
4. Do a good job of testing beforehand.
Finally, attach some commands that may be used in Linux to check Performance:. view the number of files opened by a process: first find the PID with PS-Aux, and then run lsof-P % PID % WC-l
B. Check the number of connections on port 80 netstat-natgrep-I "80" WC-l


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: 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.