Writing a thread-safe JSP application

Source: Internet
Author: User
Tags getmessage variables sleep string static class stmt thread perl script

JSP default is multithreaded, this is the JSP and Asp,php,perl script language is not the same place, but also one of its advantages, but if you do not pay attention to the synchronization problem in multiple threads, will make the written JSP program has difficult to find errors. The following is an example of the multithreading problem in JSP and how to solve it.

One, JSP in the existence of multithreading problems:

When a client requests a JSP file for the first time, the server compiles the JSP into a class file, creates an instance of the class, and then creates a thread to handle the client-side request. If more than one client requests the JSP file at the same time, the server creates multiple threads. Each client request corresponds to a thread. Implementing multithreading can greatly reduce the resource requirements of the system and increase the concurrency and response time of the system. The variables that might be used in a JSP are described below:

Instance variables

Instance variables are allocated in the heap and are shared by all threads belonging to that instance, so they are not thread-safe.
JSP system provides 8 class variables, JSP used in the OUT,REQUEST,RESPONSE,SESSION,CONFIG,PAGE,PAGECONXT is thread-safe, application is used throughout the system, so is not thread-safe.
Local variables

Local variables are allocated on the stack, because each thread has its own stack space, so it is thread-safe.

Static class

Static classes can be used directly, and are not thread-safe, without being instantiated.

External resources:

There may be multiple threads or processes in your program that simultaneously manipulate the same resource (for example, multiple threads or processes writing to a file at the same time). Be aware of synchronization issues at this point.

Second, the following examples of multithreading problems:

  <%@ page import= "
String Name
String product;
Long quantity;
Name=request.getparameter ("name");
Product=request.getparameter ("Product");
Quantity=request.getparameter ("Quantity"); /* (1) * *
Savebuy ();
public void Savebuy ()
/* Perform database operations, save data to the table * *
try {
Properties Props = new properties ();
Props.put ("User", "Scott");
Props.put ("Password", "Tiger");
Props.put ("Server", "DEMO");
Driver mydriver = (Driver) Iver "). newinstance ();
conn = Mydriver.connect ("jdbc:weblogic:oracle", props);
stmt = Conn.createstatement ();
String inssql = "INSERT into buy (empid, name, dept) VALUES (?,?,?,?)";
stmt = Conn.preparestatement (Inssql);
Stmt.setstring (1, name);
Stmt.setstring (2, procuct);
Stmt.setint (3, quantity);
Stmt.execute ();
catch (Exception e)
System.out.println ("SQLException was thrown:" + e.getmessage ());
Finally//close connections and {
try {
if (stmt!= null)
Stmt.close ();
IF (conn!= null)
Conn.close ();
catch (SQLException Sqle) {
System.out.println ("SQLException was thrown:" + sqle.getmessage ());

The above program simulates a part of online shopping and saves the user name, the name of the item purchased, and the number of items you enter in the browser to the table. An instance variable is used in the Savebuy () function, so it is not thread safe. Because: Each statement in the program is not an atomic operation, such as Name=request.getparameter ("name"), the execution of which will correspond to multiple machine instructions, at any time may be scheduled by the system to sleep state, so that other threads to continue to execute. If thread a goes to sleep when it executes to (1), thread B starts executing and changes the value of the quantity, then when it executes, it starts executing from the call Savebuy () function, so that the quantity that it saves to the table is the value that was changed by thread B. It is a serious problem that thread a corresponds to the number of users actually buying that is inconsistent with the data kept in the table.

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.