Second, J2EE The improvement performance technique in the previous introduction is suitable for most Java applications, and the problem to be discussed is suitable for applications of JSP, EJB or JDBC.
2.1 Use buffer marks
Some application servers have joined JSP buffer tags. For example, BEA's WebLogic Server starts supporting this feature from version 6.0, and the Open Symphony project also supports this feature. The JSP buffer tag can buffer the page segment and can buffer the entire page. When the JSP page is executed, if the target segment is already in the buffer, the code generated by generating the piece is not executed. Page-level buffer capture requests for the specified URL and buffer the entire result page. This feature is extremely useful for the homepage of the shopping basket, catalog, and portal. For such applications, the page-level buffer can save the result of the page execution, and the subsequent request is used.
For code logic complex pages, the effect of improving performance using buffer marks is more obvious; contrary, the effect may be slightly coupled.
2.2 Always access entity beans through session beans
Direct access entity beans are not conducive to performance. Each GET method is a remote call when the client remotely accesses entity beans. Session beans accessed to access entity beans are local, and all data can be organized into a structure and then returns its value.
Access to entity beans can improve transaction management because session beans will only be submitted when the transaction boundary is reached. Each direct call to the GET method generates a transaction, and the container will perform a "load-read" operation after each entity bean's transaction.
Time, using entity beans will lead to poor performance. If the unique use of entity beans is to extract and update data, it is changed to use the JDBC access database within session beans to get better performance.
2.3 Select the appropriate reference mechanism
In a typical JSP application, the header, the footer section is often extracted, then introduced to the page, footage as needed. Currently, there are two ways to introduce external resources in the JSP page. There are two: include instructions, and the include action.
Include instruction: for example <% @ include file = "Copyright.html"%>. This instruction introduces the specified resource when compiling. Before compiling, the pages with the include instructions and the specified resources are merged into one file. The referenced external resource is determined when compiling is more efficient than running.
Include Action: For example
2.4 Setting read only properties in the deployment descriptor
The deployment of the entity bean allows all GET methods to be "read only". When a transaction unit is only included with a method of performing a read operation, setting the read-only property is conducive to improve performance because the container does not have to perform a storage operation.
2.5 Bush Visits for EJB HOME
The EJB HOME interface is obtained through the JNDI name. This operation requires considerable overhead. JNDI looks up in the init () method of servlet. If you frequent EJB access frequently, you will be best to create an EJBHomeCacche class. The EJBHomeCache class should generally be implemented as a Singleton.
2.6 Realizing the local interface for EJB
The local interface is the new content of EJB 2.0 specification, which allows Bean to avoid overhead of remote calls. Consider the following code.
PayBeanHome home = (PayBeanHome) javax.rmi.PortableRemoteObject.narrow (ctx.lookup ( "PayBeanHome"), PayBeanHome.class); PayBean bean = (PayBean) javax.rmi.PortableRemoteObject.narrow (home.create (), PayBean. Class); The first statement means that we are looking for the Home interface. This lookup is made through JNDI, it is an RMI call. Then, we locate the remote object, return to the proxy reference, which is also a RMI call. The second statement demonstrates how to create an instance, involving the Stub program that creates a IIOP request and transmits the request on the network, which is also a RMI call.
To achieve a local interface, we must make the following modifications:
The method can no longer throw a java.rmi.RemoteException, including exceptions derived from RemoteException, such as TransactionRequiredException, TransactionRolledBackexception, and NosuChobjectException. EJB provides equivalent local exception, such as TransactionRequiredLocalexception, TransactionRolledBackLocalexception, and NosuchobjectLocalexception.
All data and return values are passed in a reference, not the transmission value.
The local interface must be used on the machine deployed by the EJB. In short, the client and components of the service must run on the same JVM.
If the bean implements a local interface, its reference is not serialized.
2.7 Generate the primary key
There are many ways to generate primary keys within EJBs, which analyzes several common methods and their characteristics.
Use the identification mechanism built in the database (SQL Server's Identity or Oracle's Sequence). The disadvantage of this method is that EJB portability is poor.
The primary key value (such as incremental operation) is calculated from the entity bean. Its disadvantage is that the transaction can be serialized and the speed is slow.
Use clock services such as NTP. This requires a local code for a particular platform to secure the bean to a particular OS. In addition, it also leads to such a possibility that two primary keys are generated within the same millisecond on the multi-CPU server.
Borrow for Microsoft's ideas, create a GUID in the bean. However, if you do not help JNI, Java cannot determine the MAC address of the NIC; if you use JNI, the program is to depend on a specific OS.
There are several other ways, but these methods are the same limitations. It seems that only one answer is ideal: combined with RMI and JNDI. First bind the RMI remote object to the JNDI tree through the RMI registration. The client is looking for through JNDI. Below is an example:
public class keyGenerator extends UnicastRemoteObject implements Remote {private static long KeyValue = System.currentTimeMillis (); public static synchronized long getKey () throws RemoteException {return KeyValue ;}
2.8 Clear a session that is no longer required in time
In order to clear the no longer active session, many application servers have default session timeout, generally 30 minutes. When the application server needs to save more sessions, if the memory capacity is insufficient, the operating system shifts some memory data to disk, and the application server may also turn some unusual session according to the "most recent use" algorithm. Store to disk, may even throw "insufficient memory" exception. In a large-scale system, the cost of serialized sessions is very expensive. When the session is no longer needed, the httpsession.invalidate () method should be invoked to clear the session. HttpSession.INValidate () method can usually be called at the application's exit page. 2.9 Close the useless session in the JSP page
For those pages that do not have to track session status, shut down automatically created sessions can save some resources. Use the following Page instructions:
<% @ Page session = "false"%>
2.10 servlet and memory use
Many developers are free to save a lot of information into the user session. Time, the object stored in the session does not have a timely manner to be recycled by the garbage collection mechanism. From the performance, the typical symptom is that the user feels that the system is periodically slow, but it is not possible to attribute the reason to any particular component. If you monitor the heap space of the JVM, its performance is that the memory is not properly larger.
There are two main ways to solve such memory problems. The first method is to implement the HTTPSessionBindingListener interface in the BEAN of the session. Thus, as long as the valueUnbound () method is implemented, the resources used by the bean can be explicitly released.
Another way is to invalid the session as soon as possible. Most application servers have options for setting sessions as waste intervals. In addition, you can also call the session's setMaxinActiveInterVal () method in a program, which is used to set the maximum interval of the client requests allowed by the servlet container before the invalid session.
2.11 HTTP Keep-Alive
Keep-alive features continue to be valid for the client to the server-side connection, and the Keep-Alive function avoids establishing or re-establishing the connection when the server's subsequent request appears. Most web servers on the market, including IPlanet, IIS, and Apache, HTTP Keep-Alive supports. This feature is usually useful for sites that provide static content. However, there is another problem with the burden heavier website: although there is a certain advantage of reserved the open connection, it also affects performance, because during the handling of the suspension, the resources that can be released are still occupied. . When the web server and the application server run on the same machine, the KEEP-Alive function has a high impact of resource utilization.
2.12 JDBC and Unicode
Think of you have learned some measures to improve performance when using JDBC, such as using the connection pool, correctly select the stored procedure and direct SQL, from the result set delete the extra column, pre-compiling the SQL statement, and so on.
In addition to these obvious options, another good choice for improvement may be saved as Unicode (code page 13488). Java processes all data in Unicode, so the database driver does not have to perform a conversion process. But should be remembering: If this is used, the database will become larger because each Unicode character requires 2 byte storage space. In addition, if there are other non-Unicode program access to the database, performance issues still appear because the database driver still must perform a conversion process.
2.13 JDBC and I / O
If the application needs to access a large data set, you should consider using the block extraction method. By default, JDBC extracts 32 lines of data each time. For example, suppose we want to traverse a 5000 row of record sets, JDBC must call the database 157 times to extract all data. If the block size is changed to 512, the number of calls to the database will be reduced to 10 times. This technology is invalid in some cases. For example, if you use the scrollable recording set, or specify for Update in the query, the block operation is no longer valid.
1.14 memory database
Many applications need to save considerable number of data in session objects in a session object, typical applications such as shopping baskets and directories. Since such data can be organized in the form of rows / columns, many applications have created huge vector or havehmap. Saving such data in a session greatly limits the scalability of the application because the memory has at least the memory of each session takes up with the maximum number of memory, which not only makes the server are expensive, but also garbage collection The time interval may also extend to an unbearable level.
Some people transfer the shopping basket / directory function to the database layer to a certain extent increase scalability. However, putting this part of the function in the database layer also has problems, and the root cause of the problem is related to the architecture of most relational database systems. For relational databases, one of the important principles of runtime is to ensure that all write operations are stable, reliable, and thus all performance issues are related to the ability to write data to disk physically. Relational database power map reduces I / O operation, especially for read operations, but the main way to achieve this goal is just a complex algorithm for implementing a buffer mechanism, which is the main Bottleneck of the database layer, usually the main CPU. the reason.
A solution for an alternative to traditional relational databases is to use a database that runs in memory (in-Memory Database), such as TIMESTEN. The starting point of the memory database is to allow data to be temporarily written, but these data do not have to be saved to disk permanently, all operations are in memory. In this way, the memory database does not require a complex algorithm to reduce I / O operation, and can adopt a relatively simple locking mechanism, so the speed is very fast.