Source: Fengwang Forum
Quote address:
Http://ijsp.net/2/2003-3/27/0000357.SHTML
Description: Original text has no title, the title is my own
When doing a project, JSP has some problems when running, and I will make a summary of my problem solution for reference. Question 1: WebLogic's database connection is growing in the program run, the last number exceeds the maximum number, causing the WebLogic service to close the reason: After the database is completed, the database connection is not closed; or return the result set (Resultset), but cannot Turn off the database connection in the JSP. Solution: 1. Turn off the database connection in the operation of the database. 2. Try not to return the result set ResultSet, you can return to the Vector, a "multiple fields), which can close the database in the JavaBean. 3. If the result set is returned in the JavaBean, you can also write a ConnectDB (Connect Database), ClosedB (Close Database), and then call connectDb (), establish a database connection, and create a database connection, and you can create a database connection. Operation, the operation database is completed, and the database can be turned off via CloseDB (). Use a second method
Question 2: When running a JSP program, WebLogic's memory has grown, and he is high. Eventually leading to WebLogic memory, or even when it is. Cause: Excessive memory. Solution: 1. Since the amount of data is relatively large, use to make a string connection, and I believe that everyone is very familiar with String, and we often use it to make a string connection, for example: String A = B C File: // b, C is string but is this in the actual compilation: string a = new stringbuffer (). Append (b) .append (c) .tostring () obviously in a simple In the statement, there are more than 2 objects: .StringBuffer () .tostring returned a string. We compare the performance of these two programs: program piece 1:
Stringbuffer s = new stringbuffer (); long start = system.currenttimemillis (); for (int i = 0; i <1000; i ) {s1 = "a";} long stop = system.currenttimemillis (); system.out .println (stop-start); Drake Drake 2:
Stringbuffer s = new stringbuffer (10000); // long start = system.currenttimemillis (); for (int i = 0; i <1000; i ) {s.append ("a");} long stop = system.currenttimemillis (); System.out.println (stop-start); Compare the result, the gap is obvious. As for why String connection is done, because String cannot directly change its length, but must use StringBuffer usage.
It is therefore recommended to use the StringBuffer Append method to connect to the string.
2. When I solve this problem, I also try to use the above method, the effect is not very obvious (consuming memory). Later, when you display a lot of data, avoid the steps connected to the string, and use out.println () and direct output.
Question 3:
Java does not prevent programs from occupying too much memory. When the object is insufficient to the replete, the garbage collector will automatically start, release the memory occupied by the number of items that reference to zero, Java is not The reference to the useless object will be automatically released. If the program forgot to release the reference to the object, the memory is increased while the program is running, and the so-called memory leak will, but the creative object not only consumes the CPU time and memory. At the same time, the Garbage Collector will continue to activate the Garbage Collector for the release of the object memory JVM, which will also consume a large number of CPU times. Solution:
Since the WebLogic's memory will have a slow growth in the Memory overflow, this will result in memory overflow, and the final solution is to write a servlet program, when starting the server Start a servlet, run at the server side every 20 minutes, and reclaim memory.
Question 4: The debugging information in the log file is not comment. Workaround: After the program is passed, try to remove the debug information. At the same time, you must write the name of the program when you catch the wrong mistake, which is easy to find, this is not enough. It is best to write a way to record logs for program calls.
Question 5 improve performance and improve speed. Specific example: Let's take a look at the code snippet about the Vector class:
For (int i = 0; i If V contains 100,000 elements, this code snippet will call a v.size () method 100,000 times. Although the Size method is a simple method, it still needs a way to call overhead, at least JVM needs to configure and clear the stack environment. Here, the code inside the FOR loop does not modify the size of the Vector Type Object V in any way, so the above code is best to be rewritten into the following form: INT size = v.size (); for (int i = 0; i Although this is a simple change, it still has won performance. After all, every CPU cycle is precious. Question 6: Don't write <% @ page import = "java.lang. *"%> In the JSP file. Because Java does not need to introduce this package to reference the class files. Question 7: Use Vector HashTable once again returns the results ResulSet. Solution: Recording set: Put a record in a HashTable, then add it to the vector, loop the result set, return to the Vector. Some of the code of the Java file behind it (not including the connection and close of the database) Package zjdx.bean.common; / **************************************************** ******* Title: Hashtable_Vector_rs ***** Description: Data Display ***** Copyright: Copyright (c) 2002 ***** Company: DHC ***** Author: wangyl *** ** Version: 1.0 ***** Description: ***** Recorder: One record is put in a HashTable, then put it in the vector, loop record the result set, return to vector **** ************************************************** / Import java.io. *; import Java.sql. *; import java.util. *; import java.text. *; public class haShtable_vector_rs {/ * ------------------------ ---------------------------------------- // * function name: getMultirowInfo / * Function Description: Return to the record set, put it in the HashTable / * parameter: SQL statement, field number / * return value: success --- htable, failed --- null / * ----------- -------------------------------------------------- --- * / Public Vector ListResult (String SqlStatement, Int Num) {Vector Listrs = New Vector (); try {connectiondb (); rs = stmt.executeQuery (SQLStatement); // Judgment Field Data Type // Date Type Back 93 // INT returns 2, 4 // Bigint type return 3 // string type Return 12 // (CHAR type) Returns 1Int T = -1; RSMD = rs.getMetadata (); int columncount = 0; if (Num > 0) Columncount = Num; ElsecolumnCount = rsmd.getco Lumncount (); while (rs.next ()) {hashtable htable = new hashtable (); for (int i = 1; i <= columncount; i ) {t = rsmd.getColumnType (i); system.out.println ("i =" i ", t =" t "name =" rMd.getColumnName (i)); if (t == 12 || T == 1 || t == 3) {IF (rs .getstring (i) == null || rs.getstring (i) .Equals (")) htable.put (rsmd.getcolumnname (i)," "); elsehtable.put (rsmd.getcolumnname (i), RS .getstring (i));} else if (t == 93) {htable.put (rsmd.getColumnname (i), rs.getdate (i) .tostring ());} else if (t == 2 || T == 4) {htable.put (rsmd.getcolumnname (i), integer.tostring (rs.getint (i)));}} Listrs.add (htable);} // Ereturn Listrs;} catch (Exception Listerror) {SYSTEM .out.println ("database operation failed!" lisTerror); return null;} finally {Try {closedb ();} catch (exception closeerr) {system.out.println ("Close Database error:" Closeerr); }}}}