在servlet

时间:2016-05-24 23:20:23

标签: multithreading servlets

我们知道servlet实例是一次创建的,并且基于对servlet的多个用户请求,容器创建线程来处理每个用户请求。

如果我在多线程的servlet中存储过程怎么办?存储过程从用户请求中获取参数,并仅为该用户生成输出。

如果我有多个用户请求同时发生,该怎么办? user1生成的过程结果是否会被另一个用户请求覆盖,因为每个线程都有自己的执行流。请帮助理解。

我已经尝试了端到端的简单例子。这里我没有使用同步

public class Demo {
    public static void main(String[] args) {
      DBThread db = new DBThread();

//Only one instance and multiple threads 
         Thread request1= new Thread(db, "request1"); 
         Thread request2= new Thread(db, "request2");
       request1.start();

        request2.start();
}
}


DBThread.java


import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class DBThread implements Runnable {


    public void  getData(){
        Connection con=null;
        Statement statement = null;  
        CallableStatement callProcedure = null;
        ResultSet rs = null;
         try {

              con = getDBConnection();
             if(con==null)
             System.out.println("Unable to get connection for " + Thread.currentThread().getName());
             else
             {

                      callProcedure = con.prepareCall("{call PROCEDURE1(?)}");
                    callProcedure.setString(1,Thread.currentThread().getName());
                    callProcedure.execute();
                    statement = con.createStatement();
                        rs = statement.executeQuery("select id,inputname from temp1");

                        while(rs.next()){

                        System.out.println("Thread "+Thread.currentThread().getName() +":"+ rs.getString(1)  +":" + rs.getString(2));

                        }


                }



        } catch (SQLException e) {

            e.printStackTrace();
        } catch (Exception e) {

            e.printStackTrace();
        }
            finally{
                try{
                    if(callProcedure!=null){
                        callProcedure.close();
                    }
                    if(rs!=null)
                        rs.close();

                    if(con!=null){
                        con.close();

                        System.out.println("Connection close for " + Thread.currentThread().getName());
                    }

                }catch(Exception e){
                    System.out.println("print therea" + Thread.currentThread().getName());
                }
            }


    }



    public void run() {
        this.getData();

    }


       public  Connection getDBConnection() throws SQLException, Exception{
           String user = null;
           String pwd = null;
           String dbURL = null;
           String driverClassName = null;
             Connection con = null;

               if(con == null || con.isClosed() == true){

                  user = "readonly";
                  pwd = "XXXX"
                  dbURL = "jdbc:oracle:thin:@host:port:SID;
                  driverClassName = "oracle.jdbc.driver.OracleDriver";

                  Class.forName(driverClassName);
                  con = DriverManager.getConnection(dbURL, user, pwd);

               }
               System.out.println("Connection got for " + Thread.currentThread().getName()+ "Connection "+ con);
           return con;
       }
}




Oracle procedure :

create or replace 
PROCEDURE PROCEDURE1(id varchar2) AS 
BEGIN
DELETE from temp1;
   FOR i IN 1..5 LOOP
             INSERT INTO temp1 VALUES (i,id);
        END LOOP;
     COMMIT;

END PROCEDURE1;

1 个答案:

答案 0 :(得分:0)

由于您正在使用在函数末尾(最后一个块)关闭的相同连接,因此在并行执行的情况下,您很可能在第一个完成后的第二个线程中获得一些SQLException。