Advertisement
Guest User

Untitled

a guest
May 24th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. we know the servlet instance is created once and based on multiple user requests to the servlet , container creates threads to address each of the user request.
  2.  
  3. public class Demo {
  4. public static void main(String[] args) {
  5. DBThread db = new DBThread();
  6.  
  7. //Only one instance and multiple threads
  8. Thread request1= new Thread(db, "request1");
  9. Thread request2= new Thread(db, "request2");
  10. request1.start();
  11.  
  12. request2.start();
  13. }
  14. }
  15.  
  16.  
  17. DBThread.java
  18.  
  19.  
  20. import java.sql.CallableStatement;
  21. import java.sql.Connection;
  22. import java.sql.DriverManager;
  23. import java.sql.ResultSet;
  24. import java.sql.SQLException;
  25. import java.sql.Statement;
  26.  
  27.  
  28. public class DBThread implements Runnable {
  29.  
  30.  
  31. public void getData(){
  32. Connection con=null;
  33. Statement statement = null;
  34. CallableStatement callProcedure = null;
  35. ResultSet rs = null;
  36. try {
  37.  
  38. con = getDBConnection();
  39. if(con==null)
  40. System.out.println("Unable to get connection for " + Thread.currentThread().getName());
  41. else
  42. {
  43.  
  44. callProcedure = con.prepareCall("{call PROCEDURE1(?)}");
  45. callProcedure.setString(1,Thread.currentThread().getName());
  46. callProcedure.execute();
  47. statement = con.createStatement();
  48. rs = statement.executeQuery("select id,inputname from temp1");
  49.  
  50. while(rs.next()){
  51.  
  52. System.out.println("Thread "+Thread.currentThread().getName() +":"+ rs.getString(1) +":" + rs.getString(2));
  53.  
  54. }
  55.  
  56.  
  57. }
  58.  
  59.  
  60.  
  61. } catch (SQLException e) {
  62.  
  63. e.printStackTrace();
  64. } catch (Exception e) {
  65.  
  66. e.printStackTrace();
  67. }
  68. finally{
  69. try{
  70. if(callProcedure!=null){
  71. callProcedure.close();
  72. }
  73. if(rs!=null)
  74. rs.close();
  75.  
  76. if(con!=null){
  77. con.close();
  78.  
  79. System.out.println("Connection close for " + Thread.currentThread().getName());
  80. }
  81.  
  82. }catch(Exception e){
  83. System.out.println("print therea" + Thread.currentThread().getName());
  84. }
  85. }
  86.  
  87.  
  88. }
  89.  
  90.  
  91.  
  92. public void run() {
  93. this.getData();
  94.  
  95. }
  96.  
  97.  
  98. public Connection getDBConnection() throws SQLException, Exception{
  99. String user = null;
  100. String pwd = null;
  101. String dbURL = null;
  102. String driverClassName = null;
  103. Connection con = null;
  104.  
  105. if(con == null || con.isClosed() == true){
  106.  
  107. user = "readonly";
  108. pwd = "XXXX"
  109. dbURL = "jdbc:oracle:thin:@host:port:SID;
  110. driverClassName = "oracle.jdbc.driver.OracleDriver";
  111.  
  112. Class.forName(driverClassName);
  113. con = DriverManager.getConnection(dbURL, user, pwd);
  114.  
  115. }
  116. System.out.println("Connection got for " + Thread.currentThread().getName()+ "Connection "+ con);
  117. return con;
  118. }
  119. }
  120.  
  121.  
  122.  
  123.  
  124. Oracle procedure :
  125.  
  126. create or replace
  127. PROCEDURE PROCEDURE1(id varchar2) AS
  128. BEGIN
  129. DELETE from temp1;
  130. FOR i IN 1..5 LOOP
  131. INSERT INTO temp1 VALUES (i,id);
  132. END LOOP;
  133. COMMIT;
  134.  
  135. END PROCEDURE1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement