您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

编写线程安全的JSP程序_MySQL

2026/2/17 13:53:44发布23次查看
作者:徐春金
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());
        }
    }
}
%>
在该jsp文件中加上: ,使它以单线程方式执行,这时,仍然只有一个实例,所有客户端的请求以串行方 式执行。这样会降低系统的性能.
对函数savebuy()加synchronized进行线程同步,该jsp仍然以多线程方式执行,但也会降低系统的性能
public synchronized void savebuy()
{
       ......
}
采用局部变量代替实例变量,函数savebuy()声明如下:
因为在savebuy()中使用的是传给他的形参,是在堆栈中分配的,所以是线程安全的.
public void savebuy(string name,string product, int quantity)
{
      ......
}
调用方式改为:
string name
string product;
long quantity; name=request.getparameter(name);
product=request.getparameter(product);
quantity=request.getparameter(quantity);
savebuy(name,product,quantity)
%>
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product