iWiz ShareBase

IT Specialist À±ÅÂÇöÀÇ iWiz ShareBase´Â IT»Ó ¾Æ´Ï¶ó °¢Á¾ Àâ´ÙÇÑ Áö½ÄµéÀ» ÇÔ²² ³ª´©´Â Áö½Ä°øÀ¯ Ä¿¹Â´ÏƼÀÔ´Ï´Ù.

iWiz,ShareBase,À±ÅÂÇö,Java,JSP,EJB,IT,Á¤º¸±â¼ú,À¥ÇÁ·Î±×·¡¹Ö,PHP,ASP,DBMS,MySQL,¼­¹ö,³×Æ®¿öÅ©,server,network,WAS,À¥¾ÖÇø®ÄÉÀ̼Ç,ºí·Î±×,blog,À¥¼­¹ö,DB,¿À¶óŬ,oracle,mysql,JRun,À¥·ÎÁ÷,ÅèĹ,tomcat,¾ÆÆÄÄ¡,ÀÚµ¿Â÷,EF½î³ªÅ¸,·Î¶Ç 6/45

°¶·¯¸® Pixelgrapher.com | ·Î¶Ç 6/45 ¹øÈ£»ý¼º ¹× Åë°è µ¥ÀÌÅÍ | Àüü±â»çº¸±â | Àüü±Û #1 | Àüü±Û #2 | Àüü±Û #3 | Àüü±Û #4 | Àüü±Û #5 | Àüü±Û #6 | Àüü±Û #7 | Àüü±Û #8 | Àüü±Û #9 | Àüü±Û #10 |
HOME iWiz
ShareBase
Remember 0523 & 0818
Áö½ÄÀº ³ª´­¼ö·Ï Ä¿Áý´Ï´Ù - iWiz's ShareBase
À¥ÇÁ·Î±×·¡¹Ö(±âŸ) PHP, ASP, Perl, CGI µî °¢Á¾ À¥ÇÁ·Î±×·¡¹Ö¿¡ °üÇÑ ÀÚ·áµéÀÔ´Ï´Ù.


  iWiz(2004-01-04 22:40:26, Hit : 7268, Vote : 29
 http://www.wz.pe.kr

¼­ºí¸´ + JDBC ¿¬µ¿½Ã ÄÚµù °í·Á»çÇ× 1


´Ùµé ¾Æ½Ç¹ýÇÑ ´Ü¼øÇÑ ¾ê±é´Ï´Ù¸¸, ¾ÆÁ÷ ¸¹Àº ºÐµéÀÌ ¸ð¸£½Ã´Â °Í °°¾Æ ´Ù½ÃÇѹø
Á¤¸®ÇÕ´Ï´Ù. ¾Æ·¡ÀÇ °¢°¢ÀÇ ¿¹Á¦´Â À߸ø »ç¿ëÇÏ°í °è½Ã´Â ÀüÇüÀûÀÎ ¿¹µéÀÔ´Ï´Ù.

1. ¼­ºí·¿¿¡¼­ instance variable ÀÇ °øÀ¯

1.1 ¼­ºí·¿¿¡¼­ instance variable ÀÇ °øÀ¯ - PrintWriter -

  ´ÙÀ½°ú °°Àº Äڵ带 »ý°¢ÇØ º¸°Ú½À´Ï´Ù.

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class CountServlet extends HttpServlet {
     private PrintWriter out = null; // <-------------- (1)

     public void doGet(HttpServletRequest req, HttpServletResponse res)  
         throws ServletException, IOException
     {
         res.setContentType("text/html");
         out = res.getWriter();
         for(int i=0;i<20;i++){
             out.println("count= " + (i+1) + "<br>");  // <---- (2)
             out.flush();
             try{Thread.sleep(1000);}catch(Exception e){}
         }
    }
  }

À§ÀÇ CountServlet.java ¸¦ ÄÄÆÄÀÏÇÏ¿© µ¹·Á º¸¸é, 1Ãʰ£°ÝÀ¸·Î ÀÏ·ÃÀÇ ¼ýÀÚ°¡ ¿Ã¶ó°¡´Â
°ÍÀÌ º¸ÀÏ °Ì´Ï´Ù.(¼­ºí·¿¿£ÁøÀÇ ±¸Çö¹æ½Ä¿¡ µû¶ó Buffering ÀÌ µÇ¾î 20Ãʰ¡ ¸ðµÎ Áö³­
ÈÄ¿¡¼­ ÆÜ ³ª¿Ã ¼öµµ ÀÖ½À´Ï´Ù.)
È¥ÀÚ¼­ ´ÜÀÏ Request ¸¦ ³¯·Á º¸¸é, ¾Æ¹«·± ¹®Á¦°¡ ¾ø°ÚÁö¸¸, ÀÌÁ¦ ºê¶ó¿ìÁ® âÀ» µÎ°³ ÀÌ»ó
¶ç¿ì½Ã°í 10ÃÊÀÇ ½Ã°£ Â÷¸¦ µÎ½Ã¸é¼­ µ¿½Ã¿¡ È£ÃâÇØ º¸¼¼¿ä... ÀÌ»óÇÑ Áõ»óÀÌ ³ªÅ¸³¯
°Ì´Ï´Ù. ¸ÕÀú È£ÃâÇÑ Ã¢¿¡´Â 10 ±îÁö Á¤µµ¸¸ ³ªÅ¸³ª°í, 10ÃÊ µÚ¿¡ È£ÃâÇÑ Ã¢¿¡¼­´Â ¸ÕÀú
È£ÃâÇÑ Ã¢¿¡¼­ ³ªÅ¸³ª¾ßÇÒ ³»¿ëµé±îÁö ´ýÀ¸·Î ³ªÅ¸³ª´Â °ÍÀ» ¸ñ°ÝÇÒ ¼ö ÀÖÀ» °Ì´Ï´Ù.

ÀÌ´Â ¼­ºí·¿ÀÇ °¢ È£ÃâÀº Thread ·Î µ¿ÀÛÇÏ¿©, µû¶ó¼­, °¢ È£ÃâÀº À§ÀÇ (1) ¿¡¼­ ¼±¾ðÇÑ
instance variable µéÀ» °øÀ¯Çϱ⠶§¹®¿¡ ³ªÅ¸³ª´Â ¹®Á¦ÀÔ´Ï´Ù.

À§ ºÎºÐÀº ´ÙÀ½°ú °°ÀÌ °íÃÄÁ®¾ß ÇÕ´Ï´Ù.

  public class CountServlet extends HttpServlet {
     //private PrintWriter out = null;

     public void doGet(HttpServletRequest req, HttpServletResponse res)  
         throws ServletException, IOException
     {
         PrintWriter out = null; // <--- ÀÌ ÂÊÀ¸·Î ¿Í¾ßÁÒ !!!
         res.setContentType("text/html");
         out = res.getWriter();
         for(int i=0;i<20;i++){
             out.println("count= " + (i+1) + "<br>");  // <---- (2)
             out.flush();
             try{Thread.sleep(1000);}catch(Exception e){}
         }
    }
  }

±¹³» ¸î¸î Servlet °ü·Ã ¼­ÀûÀÇ ÀϺΠ¿¹Á¦µéÀÌ À§¿Í °°Àº À߸øµÈ ÇüÅ·Π¼³¸íÇÑ
¼Ò½ºÄÚµåµéÀÌ ´«¿¡ ¶ë´Ï´Ù. ºü¸¥ ½ÃÀÏ¿¡ ¹Ù·Î Àâ¾Æ¾ß ÇÒ °ÍÀÔ´Ï´Ù.

½ÇÁ¦ ÇÁ·ÎÁ§Æ® ȯ°æ¿¡¼­ °³¹ßµÈ ½Ç¹«½Ã½ºÅÛ¿¡¼­µµ, ±×·¯ÇÑ Ã¥À» ÅëÇØ °øºÎÇϽŵí, µ¿ÀÏÇÑ
À߸øµÈ ÄÚµùÀ» Çϰí ÀÖ´Â °³¹ßÀÚµéÀÌ ÀÖ½À´Ï´Ù. °á°úÀûÀ¸·Î Å×½ºÆ® ȯ°æ¿¡¼­´Â ³ªÅ¸³ªÁö
¾Ê´õ´Ï¸¸, ¸·»ó ½Ã½ºÅÛÀ» ¿ÀÇÂÇÏ°í³ª´Ï °í°´À¸·Î ºÎÅÍ ´ÙÀ½°ú °°Àº ¼Ò¸®¸¦ µè½À´Ï´Ù.
"³» µ¥ÀÌŸ°¡ ¾Æ´Ñµ¥ ³²ÀÇ µ¥ÀÌŸ°¡ ³» È­¸é¿¡ °£È¤ ³ªÅ¸³ª¿ä. refresh ¸¦ ´©¸£¸é ¶Ç,
Á¦´ë·Î µÇ±¸¿ä" .....


1.2 ¼­ºí·¿¿¡¼­ instance variable ÀÇ °øÀ¯

¾Õ¼­ÀÇ °æ¿ì¿Í Àǹ̸¦ °°ÀÌÇϴµ¥, ´ÙÀ½°ú °°ÀÌ ÇÏ¸é ¾ÈµÈ´Ù´Â ¾ê±âÁö¿ä.

  public class BadServlet extends HttpServlet {
     private String userid = null;
     private String username = null;
     private int hitcount = 0;

     public void doGet(HttpServletRequest req, HttpServletResponse res)  
         throws ServletException, IOException
     {
         res.setContentType("text/html");
         PrintWriter out = res.getWriter();
         userid = request.getParameter("userid");
         username = request.getParameter("username");
         hitcount = hitcount + 1;
         ....
    }
  }

»õ·Î¿î ¸Å HTTP ¿äû¸¶´Ù userid/usernameº¯¼ö´Â »õ·Ó°Ô ÇÒ´çµË´Ï´Ù. ¹®Á¦´Â ±×°ÍÀÌ Æ¯Á¤
»ç¿ëÀÚ¿¡ ÇÑÇÏ¿© ±×·¯ÇÑ °ÍÀÌ ¾Æ´Ï¶ó, BadServletÀÇ ÀνºÅϽº(instance)´Â ÇØ´ç
À¥ÄÁÅ×À̳Ê(Web Container)¿¡ »ó¿¡¼­ (¿¹¿Ü°æ¿ì°¡ ÀÖÁö¸¸) ´Ü Çϳª¸¸ Á¸ÀçÇϰí, ¼­·Î ´Ù¸¥
¸ðµç »ç¿ëÀÚµéÀÇ ¼­·Î ´Ù¸¥ ¸ðµç ¿äûµé¿¡ ´ëÇØ¼­ µ¿ÀÏÇÑ userid/username ¹× count º¯¼ö¸¦
Á¢±ÙÇÏ°Ô µË´Ï´Ù. µû¶ó¼­, ´ÙÀ½°ú °°ÀÌ ¸Þ¼Òµå ¾ÈÀ¸·Î ²ø¾îµé¿© »ç¿ëÇÏ¿©¾ß ÇÔÀ» °­Á¶ÇÕ´Ï´Ù.

  public class BadServlet extends HttpServlet {
     //private String userid = null; // <---- !!
     //private String username = null; // <---- !!
     private int hitcount = 0;

     public void doGet(HttpServletRequest req, HttpServletResponse res)  
         throws ServletException, IOException
     {
         res.setContentType("text/html");
         PrintWriter out = res.getWriter();
         String userid = request.getParameter("userid"); // <---- !!
         String username = request.getParameter("username"); // <---- !!

         //¶ÇÇÑ, instance º¯¼ö¿¡ ´ëÇÑ Á¢±ÙÀº Àû¾îµµ ¾Æ·¡Ã³·³ µ¿±âÈ­¸¦ °í·ÁÇØ¾ß...
         synchronized(this){ hitcount = hitcount + 1; }
         ....
    }
  }


1.3 ¼­ºí·¿¿¡¼­ instance variable ÀÇ °øÀ¯  - DataBase Connection -

public class TestServlet extends HttpServlet {
     private final static String drv = "oracle.jdbc.driver.OracleDriver";
     private final static String url = "jdbc:orache:thin@210.220.251.96:1521:ORA8i";
     private final static String user = "scott";
     private final static String password = "tiger";

     private ServletContext context;
     private Connection conn = null;  <--- !!!
     private Statement stmt = null; <------ !!!
     private ResultSet rs = null; <------ !!!

     public void init(ServletConfig config) throws ServletException {
         super.init(config);
         context = config.getServletContext();
         try {
             Class.forName(drv);
         }
         catch (ClassNotFoundException e) {
             throw new ServletException("Unable to load JDBC driver:"+ e.toString());
         }
     }
     public void doGet(HttpServletRequest req, HttpServletResponse res)  
         throws ServletException, IOException, SQLException
     {
         String id = req.getParameter("id");
         conn = DriverManager.getConnection(url,user,password);   ---- (1)
         stmt = conn.createStatement();  ---------- (2)
         rs = stmt.executeQuery("select .... where id = '" + id + "'"); ----- (3)
         while(rs.next()) { ----------- (4)
            ......  --------- (5)
         }  
         rs.close();  -------- (6)
         stmt.close();  ---- (7)
         conn.close();  --- (8)
         .....
    }
  }

  À§¿¡¼­ ¹¹°¡ À߸øµÇ¾úÁÒ? ¿©·¯°¡Áö°¡ ÀÖ°ÚÁö¸¸, ±× Áß¿¡ Çϳª°¡ java.sql.Connection°ú
  java.sql.Statment, java.sql.ResultSetÀ» instance variable ·Î »ç¿ëÇϰí ÀÖ´Ù´Â °ÍÀÔ´Ï´Ù.

  ÀÌ ¼­ºí·¿Àº »ç¿ëÀÚ°¡ È¥ÀÚÀÏ °æ¿ì´Â ¾Æ¹«·± ¹®Á¦¸¦ ¾ß±âÇÏÁö ¾Ê½À´Ï´Ù. ±×·¯³ª ¿©·¯»ç¶÷ÀÌ
  µ¿½Ã¿¡ ÀÌ ¼­ºí·¿À» °°ÀÌ È£ÃâÇØ º¸¸é, ÀÌ»óÇÑ Áõ»óÀÌ ³ªÅ¸³¯ °ÍÀÔ´Ï´Ù.
  ±× ÀÌÀ¯´Â conn, stmt, rs µî°ú °°Àº reference µéÀ» instance º¯¼ö·Î ¼±¾ðÇÏ¿© µÎ¾ú±â
  ¶§¹®¿¡ ¹ß»ýÇÕ´Ï´Ù.  ¼­ºí·¿Àº Thread·Î µ¿ÀÛÇϸç À§Ã³·³ instance º¯¼ö ¿µ¿ª¿¡ ¼±¾ðÇØ µÐ
  reference µéÀº doGet(), doPost() ¸¦ ¼öÇàÇϸ鼭 °¢°¢ÀÇ ¿äûµéÀÌ µ¿½Ã¿¡ °øÀ¯ÇÏ°Ô µË´Ï´Ù.

  ¿¹¸¦ µé¾î, µÎ°³ÀÇ ¿äûÀÌ ¾à°£ÀÇ ½Ã°£Â÷¸¦ µÎ°í ºñ½ÁÇÑ ¼ø°£¿¡ doGet() ¾ÈÀ¸·Î µé¾î¿Ô´Ù°í
  °¡Á¤ÇØ º¸°Ú½À´Ï´Ù.
  A ¶ó´Â ¿äûÀÌ ¼øÂ÷ÀûÀ¸·Î (1), (2), (3) ±îÁö ¼öÇàÇßÀ» ¶§, B ¶ó´Â ¿äûÀÌ °ð¹Ù·Î doGet()
  ¾ÈÀ¸·Î µé¾î¿Ã ¼ö ÀÖ½À´Ï´Ù. B ¿ª½Ã (1), (2), (3) À» ¼öÇàÇϰÚÁÒ...
  ÀÌÁ¦ ¿äû A ´Â (4) ¹ø°ú (5) ¹øÀ» ¼öÇàÇÏ·Á Çϴµ¥, °¡¸¸È÷ »ý°¢ÇØ º¸¸é, ¿äûB ·Î ÀÎÇØ
  ¿äûA¿¡ ÀÇÇØ ÇÒ´çµÇ¾ú´ø conn, stmt, rs ÀÇ reference µéÀº ¹Ù²î¾î ¹ö·È½À´Ï´Ù.
  °á±¹, ¿äû A ´Â  ¿äû B ÀÇ °á°ú¸¦ °¡Áö°í ÀÛ¾÷À» ÇÏ°Ô µË´Ï´Ù. ¹Ý¸é, ¿äû B ´Â
  ¿äû A ÀÇ ÀÇÇØ rs.next() ¸¦ ÀÌ¹Ì ¼öÇà ÇØ ¹ö·ÈÀ¸¹Ç·Î, rs.next() ÀÇ °á°ú°¡ ÀÌ¹Ì close
  µÇ¾ú´Ù´Â ¾û¶×ÇÑ °á°ú¸¦ ³º°í ¸¶´Â °ÅÁÒ...
  ´Ù¸¥ ½¬¿î ¾ê±â·Î ¼³¸íÇØ º¸¸é, A, B µÎ»ç¶÷ÀÌ ½ÄŹ¿¡ ¾É¾Æ¼­ °¢ÀÚ ÀÚ½ÅÀÌ ÁغñÇØ ¿Â »ç°ú¸¦
  Çϳª¾¿ ±ð¾Æ¼­ ½ÄŹ À§ÀÇ Á¢½Ã¿¡ ¿Ã·Á ³õ°í ³ªÁß¿¡ ¸Ô¾î·Á ÇÏ´Â °Í°ú µ¿ÀÏÇÕ´Ï´Ù. A ¶ó´Â
  »ç¶÷ÀÌ ¿­½ÉÈ÷ »ç°ú¸¦ ±ð¾Æ Á¢½Ã¿¡ ´ã¾ÆµÑ ¶§, B ¶ó´Â »ç¶÷ÀÌ µé¾î¿Í¼­ A°¡ ±ð¾ÆµÐ »ç°ú¸¦
  ¹ö¸®°í ÀÚ½ÅÀÌ ±ðÀº »ç°ú¸¦ ´ë½Å Á¢½Ã¿¡ ´ã¾Æ µÓ´Ï´Ù. ÀÌÁ¦ A¶ó´Â »ç¶÷Àº ÀÚ½ÅÀÌ ±ð¾Æ¼­
  ´ã¾Æ µÎ¾ú´Ù°í »ý°¢ÇÏ´Â ±× »ç°ú¸¦ Á¢½Ã¿¡¼­ ¸Ô¾î¹ö¸³´Ï´Ù. °ðÀ̾î B¶ó´Â »ç¶÷ÀÌ ÀÚ½ÅÀÇ
  »ç°ú¸¦ Á¢½Ã¿¡¼­ ¸Ô¾î·Á ÇÏ´Ï ÀÌ¹Ì A °¡ ¸Ô°í ³­ ÈÄ ¿´½À´Ï´Ù. ÀÌ´Â Á¢½Ã¸¦ µÎ »ç¶÷ÀÌ
  °øÀ¯Çϱ⠶§¹®¿¡ ¹ß»ýÇÏ´Â ¹®Á¦ÀݽÀ´Ï±î.
  ¸¶Âù°¡Áö·Î ¼­ºí·¿ÀÇ °¢ Thread´Â instance variable ¸¦ °øÀ¯Çϱ⠶§¹®¿¡ µ¿ÀÏÇÑ ¹®Á¦µéÀ»
  ¹ß»ýÇÏ°Ô µË´Ï´Ù.

  µû¶ó¼­ ÃÖ¼ÒÇÑ ´ÙÀ½Ã³·³ °íÃÄÁ®¾ß ÇÕ´Ï´Ù.

public class TestServlet extends HttpServlet {
     private final static String drv = "...";
     private final static String url = "....";
     private final static String user = "...";
     private final static String password = "...";

     private ServletContext context;

     public void init(ServletConfig config) throws ServletException {
         super.init(config);
         context = config.getServletContext();
         try {
             Class.forName(drv);
         }
         catch (ClassNotFoundException e) {
             throw new ServletException("Unable to load JDBC driver:"+ e.toString());
         }
     }
     public void doGet(HttpServletRequest req, HttpServletResponse res)  
         throws ServletException, IOException, SQLException
     {
         Connection conn = null;  <----- À̰÷À¸·Î ¿Í¾ßÁÒ..
         Statement stmt = null; <-------
         ResultSet rs = null; <---------

         String id = req.getParameter("id");
         conn = DriverManager.getConnection(url,user,password);
         stmt = conn.createStatement();
         rs = stmt.executeQuery("select ..... where id = '" + id + "'");
         while(rs.next()) {
            ......  
         }  
         rs.close();
         stmt.close();
         conn.close();
         .....
     }
  }


1.4 JSP¿¡¼­ Instance Variable °øÀ¯

JSP¿¡¼­ ¾Æ·¡Ã³·³ »ç¿ëÇÏ´Â °æ¿ì°¡ À§ÀÇ °æ¿ì¿Í µ¿ÀÏÇÑ instance º¯¼ö¸¦ °øÀ¯ÇÏ´Â °æ¿ì°¡
µË´Ï´Ù.
   ---------------------------------------------------------
   <%@ page session=.... import=.... contentType=........ %>
   <%!
       Connection conn = null;
       Statement stmt = null;
       ResultSet rs = null;
       String userid = null;
   %>
   <html><head></head><body>
   <%
       ........
       conn = ...
       stmt = .....

       uesrid = ......
   %>
   </body></html>
   ---------------------------------------------------------

   ¸¶Âù°¡Áö·Î À§Çèõ¸¸ÇÑ ÀÏÀ̸ç, ¿©·¯ Thread ¿¡ ÀÇÇØ ±× °ªÀÌ º¯ÇÒ ¼ö ÀÖ´Â º¯¼öµéÀº
   <%! ... %> ¸¦ ÀÌ¿ëÇÏ¿© ¼±¾ðÇÏ½Ã¸é ¾ÈµË´Ï´Ù. ÀÌó·³ instance º¯¼ö·Î »ç¿ëÇÒ °ÍÀº
   ´ÙÀ½ ó·³, ±× °ªÀÌ º¯ÇÏÁö ¾Ê´Â °ªÀ̰ųª, ȤÀº °øÀ¯º¯¼ö¿¡ ´ëÇÑ Æ¯º°ÇÑ °ü¸®¸¦
   ÇϽŠ»óÅ¿¡¼­ ÇÏ¼Å¾ß ÇÕ´Ï´Ù.

   <%!  private static final String USERID = "scott";
        private static final String PASSWORD = "tiger";
   %>

   JSP¿¡¼­ÀÇ ÀÌ¿Í °°Àº À߸øµÈ À¯Çüµµ, ¾Õ¼± ¼­ºí·¿ÀÇ °æ¿ìó·³ ÀϺΠ±¹³» JSP°ü·Ã
   ¼­Àû¿¡¼­ ¹ß°ßµË´Ï´Ù.  ÇØ´ç ¼­ÀûÀÇ ÀúÀÚ´Â °¡´ÉÇÑ »¡¸® °³Á¤ÆÇÀ» ³»¼Å¼­ ½ÃÁ¤Çϼžß
   ÇÒ °ÍÀÔ´Ï´Ù. ÇØ´ç Ã¥Àº ÃâÆÇ»ç³ª Ã¥ÀÇ À¯Çü, ±×¸®°í ±ÛÀÚü·Î ÃßÁ¤°Çµ¥, Ãʺ¸ÀÚ°¡
   ½±°Ô ¼±ÅÃÇÒ ¹ýÇÑ Ã¥ÀÎ ¸¸Å­ ±× ÆÄ±Þ·Â°ú ¿µÇâ·ÂÀÌ ³Ê¹« Å« µí ÇÕ´Ï´Ù.

   ÀÌ¿Í °°Àº ºÎºÐÀÌ ½Ç ÇÁ·ÎÁ§Æ®¿¡¼­ Á¸ÀçÇÒ °æ¿ì, ´ëºÎºÐ ½Ã½ºÅÛ ¿ÀÇ ù³¯ Âë¿¡
   ¹®Á¦¸¦ ÀνÄÇÏ°Ô µË´Ï´Ù. Connection reference °¡ ¾þ¾îÃÄÁö¹Ç·Î Pool ¿¡ ¹ÝȯÀÌ
   ÀϾÁö ¾Ê°Ô µÇ°í, ÀÌ´Â "connection pool"ÀÇ °¡¿ëÇÑ ÀÚ¿øÀÌ ºÎÇϰ¡ ¾ó¸¶ ¾øÀ½¿¡µµ
   ºÒ±¸ÇÏ°í ¸ðÀÚ¶ó´Â Çö»óÀ¸·Î ³ªÅ¸³ª¸ç, ¶§·Ð »ç¿ëÀÚÀÇ È­¸é¿¡¼­´Â ¾û¶×ÇÑ ´Ù¸¥
   »ç¶÷ÀÇ µ¥ÀÌŸ°¡ ³ªÅ¸³ª°Å³ª, SQLException ÀÌ ³ªÅ¸³³´Ï´Ù.

   NOTE: ¾î¶»°ÔÇ϶õ ¸»ÀԴϱî? °¢ È£Ãâ°£¿¡ °øÀ¯µÇ¾î¼­´Â ¾ÈµÉ º¯¼öµéÀº <%! ...%> °¡
   ¾Æ´Ï¶ó, <% ... %> ³»¿¡¼­ ¼±¾ðÇÏ¿© »ç¿ëÇ϶õ ¾ê±é´Ï´Ù. JSP°¡ Pre-compileµÇ¾î
   ServletÇüÅ·Πº¯È¯µÉ ¶§, <%! ... %>´Â ¼­ºí·¿ÀÇ instance variable·Î ±¸¼ºµÇ´Â ¹Ý¸é,
   <% ... %>´Â _jspService() ¸Þ¼Òµå ³»ÀÇ method variable·Î ±¸¼ºµË´Ï´Ù.



2. ÇϳªÀÇ ConnectionÀ» init()¿¡¼­ ¹Ì¸® ¿¬°áÇØ µÎ°í »ç¿ëÇÏ´Â °æ¿ì.

public class TestServlet extends HttpServlet {
     private final static String drv = "oracle.jdbc.driver.OracleDriver";
     private final static String url = "jdbc:orache:thin@210.220.251.96:1521:ORA8i";
     private final static String user = "scott";
     private final static String password = "tiger";

     private ServletContext context;
     private Connection conn = null;  <--- !!!

     public void init(ServletConfig config) throws ServletException {
         super.init(config);
         context = config.getServletContext();
         try {
             Class.forName(drv);
             conn = DriverManager.getConnection(url,user,password);
         }
         catch (ClassNotFoundException e) {
             throw new ServletException("Unable to load JDBC driver:"+ e.toString());
         }
         catch (SQLException e) {
             throw new ServletException("Unable to connect to database:"+ e.toString());
         }
     }
     public void doGet(HttpServletRequest req, HttpServletResponse res)  
         throws ServletException, IOException, SQLException
     {
         Statement stmt = null;
         ResultSet rs = null;
         String id = req.getParameter("id");
         stmt = conn.createStatement();
         rs = stmt.executeQuery("select ..... where id = '" + id + "'");
         while(rs.next()) {
            ......  
         }  
         rs.close();
         stmt.close();
         .....
     }
     public void destroy() {
         if ( conn != null ) try {conn.close();}catch(Exception e){}
     }    
  }

À§´Â ¹¹°¡ À߸øµÇ¾úÀ» ±î¿ä?  ¼­ºí·¿´ç Çϳª¾¿ java.sql.Connection À» init()¿¡¼­ ¹Ì¸®
¸Î¾î µÎ°í »ç¿ëÇÏ´Â ±¸Á¶ ÀÔ´Ï´Ù.

¾óÇÍ »ý°¢ÇÏ¸é ¾Æ¹«·± ¹®Á¦°¡ ¾øÀ» µíµµ ÇÕ´Ï´Ù. doGet() ³»¿¡¼­ º°µµÀÇ Statement¿Í
ResultSet À» »ç¿ëÇϰí ÀÖÀ¸´Ï, °¢ Thread´Â ÀڽŸ¸ÀÇ Reference¸¦ °®°í »ç¿ëÇÏ°Ô µÇ´Ï±î¿ä.

ÀÌ ±¸Á¶´Â Å©°Ô ¼¼°¡ÁöÀÇ ¹®Á¦¸¦ ¾È°í ÀÖ½À´Ï´Ù. Çϳª´Â DB ¿¬°áÀÚ¿øÀÇ ³¶ºñ¸¦ °¡Á®¿À¸ç,
µÎ¹øÂ°·Î ¼ö¸¹Àº µ¿½Ã»ç¿ëÀÚ¿¡ ´ëÇÑ Ã³¸®ÇѰ踦 °¡Á®¿À°í, ¶Ç ¸¶Áö¸·À¸·Î insert, update,
delete ¿Í °°ÀÌ Çϳª ÀÌ»óÀÇ SQL¹®ÀåÀ» ¼öÇàÇϸ鼭 ´ÜÀÏÀÇ Transaction 󸮸¦ º¸Àå¹ÞÀ»
¼ö ¾ø´Ù´Â °ÍÀÔ´Ï´Ù.

1) ´ë·®ÀÇ µ¿½Ã »ç¿ëÀÚ Ã³¸® ºÒ°¡.

   À§ÀÇ ±¸Á¶´Â ¼­ºí·¿´ç Çϳª¾¿ java.sql.Connection À» Á¡À¯Çϰí ÀÖ½À´Ï´Ù. ½Ç ÇÁ·ÎÁ§Æ®¿¡¼­
   º¸Åë ¼­ºí·¿ÀÌ ¸î°³³ª µÉ±î¿ä? ÃÖ¼ÒÇÑ 100 °³¿¡¼­ 400°³°¡ ³Ñ¾î °¥ ¶§µµ ÀÖ°ÚÁÒ?
   ±×·³ java.sql.Connection¿¡ ÇÒ´ç µÇ¾î¾ß ÇÒ "DB¿¬°á°¹¼ö"µµ ¼­ºí·¿ °¹¼ö ¸¹Å­ ÇÊ¿äÇϰÔ
   µË´Ï´Ù. DB ¿¬°á ÀÚ¿øÀº DB ¿¡¼­ ¼³Á¤Çϱ⠳ª¸§ÀÌÁö¸¸, Åë»ó maximum À» ¼ÂÆÃÇϱâ
   ¸¶·ÃÀÔ´Ï´Ù. ±×·¯³ª ¾Æ¹«·± ¿äûÀÌ ¾øÀ» ¶§µµ 400 ¿©°³ÀÇ DB¿¬°áÀÌ ¿¬°áµÇ¾î ÀÖ¾î¾ß ÇÑ´Ù´Â
   °ÍÀº ÀÚ¿øÀÇ ³¶ºñÀÔ´Ï´Ù.
    
2) ´ë·®ÀÇ µ¿½Ã »ç¿ëÀÚ Ã³¸® ºÒ°¡.

   ¶ÇÇÑ, °°Àº ¼­ºí·¿¿¡ ´ëÇØ µ¿½Ã¿¡ 100 ȤÀº ±× ÀÌ»óÀÇ ¿äûÀÌ µé¾î¿Â´Ù°í °¡Á¤ÇØ º¸°Ú½À
   ´Ï´Ù. ±×·³ °°Àº java.sql.Connection ¿¡ ´ëÇØ¼­ °¢°¢ÀÇ ¿äûÀÌ conn.createStatement() ¸¦
   È£ÃâÇÏ°Ô µË´Ï´Ù.
   ¹®Á¦´Â ÇϳªÀÇ Connection ¿¡ ´ëÇØ µ¿½Ã¿¡ Open ÇÒ ¼ö ÀÖ´Â Statement °¹¼ö´Â ( ÀÌ ¿ª½Ã
   DB ¿¡¼­ ¼ÂÆÃÇϱ⠳ª¸§ÀÌÁö¸¸ ) maximum Á¦ÇÑÀÌ ÀÖ½À´Ï´Ù. Oracle ÀÇ °æ¿ì Default´Â 50
   ÀÔ´Ï´Ù. ¸¸¾à ÀÌ ¼öÄ¡ ÀÌ»óÀ» µ¿½Ã¿¡ Open ÇÏ·Á°í Çϸé "maximum open cursor exceed !"
   ¶ó´Â SQLExceptoin À» ¹ß»ýÇÏ°Ô µË´Ï´Ù.

   ¿¹¸¦ µé¾î ´ÙÀ½°ú °°Àº ÇÁ·Î±×·¥À» ½ÇÇà½ÃÄÑ º¸¼¼¿ä.

   public class DbTest {
     public static void main(String[] args) throws Exception {
        Class.forName("jdbc driver...");
        Connection conn = DriverManager.getConnection("url...","id","password");
        int i=0;
        while(true) {
           Statement stmt = conn.createStatement();
           System.out.println( (++i) + "- stmt created");
        }
     }
   }

   °ú¿¬ ¸î°³ ±îÁö conn.createStement() °¡ ¼öÇàµÉ ¼ö ÀÖÀ»±î¿ä? ÀÌ´Â DB¿¡¼­ ¼³Á¤Çϱ⠳ª¸§
   ÀÔ´Ï´Ù. Áß¿äÇÑ °ÍÀº ±× ÇѰ谡 ÀÖ´Ù´Â °ÍÀÔ´Ï´Ù.
   ¶ÇÇÑ conn.createStatement() ÅëÇØ ¸¸µé¾îÁø stmt ´Â java.sql.Connection ÀÇ ÀÚ¿øÀ̱â
   ¶§¹®¿¡ À§Ã³·³ stmt ÀÇ reference °¡ ¾ø¾îÁ³´Ù°í ÇØµµ GC(Garbage Collection)ÀÌ µÇÁö
   ¾Ê½À´Ï´Ù.


  3) Transaction Áߺ¹Çö»ó ¹ß»ý

  ¿¹¸¦ µé¾î ´ÙÀ½°ú °°Àº ¼­ºñ½º°¡ ÀÖ´Ù°í °¡Á¤ÇØ º¸°Ú½À´Ï´Ù.

  public void doGet(HttpServletRequest req, HttpServletResponse res)  
      throws ServletException, IOException, SQLException
  {
      Statement stmt = null;
      String id = req.getParameter("id");
      try {
          conn.setAutoCommit(false);
          stmt = conn.createStatement();
          stmt.executeUpdate("update into XXXX..... where id = " + id + "'");
          stmt.executeUpdate("delete from XXXX..... where id = " + id + "'");
          stmt.executeUpdate(".... where id = " + id + "'");
          stmt.executeUpdate(".... where id = " + id + "'");
          conn.commit();
      }
      catch(Exception e){
          try{conn.rollback();}catch(Exception e){}
      }
      finally {
         if ( stmt != null ) try{stmt.close();}catch(Exception e){}
         conn.setAutoCommit(true);
      }
      .....
  }

  ¾Æ¹«·± ¹®Á¦°¡ ¾øÀ» µíµµ ÇÕ´Ï´Ù. ±×·¯³ª À§ÀÇ ¼­ºñ½º¸¦ µ¿½Ã¿¡ ¿äûÇÏ°Ô µÇ¸é, °°Àº
  java.sql.Connection À» °®°í ÀÛ¾÷À» Çϰí ÀÖÀ¸´Ï Transaction ÀÌ ÁßøµÇ°Ô µË´Ï´Ù.
  ¿Ö³Ä¸é, conn.commit(), conn.rollback() °ú °°ÀÌ conn À̶ó´Â Connection ¿¡ ´ëÇØ¼­
  Transaction ÀÌ °ü¸®µÇ±â ¶§¹®ÀÔ´Ï´Ù. ¿äû A °¡ ÃÑ 4°³ÀÇ SQL¹®Àå Áß 3°³¸¦ Á¤»óÀûÀ¸·Î
  ¼öÇàÇÏ°í ¸¶Áö¸· 4¹øÂ°ÀÇ SQL¹®ÀåÀ» ¼öÇàÇÏ·Á ÇÕ´Ï´Ù. ÀÌ ¶§ ¿äû B°¡ µÚµû¶ó µé¾î¿Í¼­
  2°³ÀÇ SQL ¹®ÀåµéÀ» ¿­½ÉÈ÷ ¼öÇàÇß½À´Ï´Ù.  ±Ùµ¥, ¿äû A¿¡ ÀÇÇÑ ¸¶Áö¸· SQL ¹®Àå
  ¼öÇàÁß¿¡ SQLException ÀÌ ¹ß»ýÇß½À´Ï´Ù. ±×·¸´ã ¿äû A ´Â catch(Exception e) Àý·Î
  ºÐ±â°¡ ÀϾ°í conn.rollback() À» ¼öÇàÇÏ¿© ÀÌ¹Ì ¼öÇàÇÑ 3°³ÀÇ SQL ¼öÇàµéÀ» ¸ðµÎ
  rollback ½Ãŵ´Ï´Ù. ±Ùµ¥,,, ¹®Á¦´Â ¿äû B ¿¡ ÀÇÇØ ¼öÇàµÈ 2°³ÀÇ SQL¹®Àåµéµµ °°ÀÌ
  ½ÎÀâ¾Æ¼­ rollback() µÇ¾î ¹ö¸³´Ï´Ù. ¿Ö³Ä¸é °°Àº conn °´Ã¼´Ï±î¿ä. °á±¹, ¿äûB ´Â
  ¿µ¹®µµ ¸ð¸£°í ¸¶Áö¸· 2°³ÀÇ SQL¹®À常 ¼öÇàÇÑ °á°ú¸¦ ³º°í ¸¿´Ï´Ù.

  µû¶ó¼­ Á¤¸®Çϸé, Connection, Statement, ResultSet ´Â doGet() , doPost() ³»¿¡¼­
  ¼±¾ðµÇ°í »ç¿ëµÇ¾îÁ®¾ß ÇÕ´Ï´Ù.

  public void doGet(HttpServletRequest req, HttpServletResponse res)  
      throws ServletException, IOException, SQLException
  {
      Connection conn = null;  <----- À̰÷À¸·Î ¿Í¾ßÁÒ..
      Statement stmt = null; <-------
      ResultSet rs = null; <---------
      .....
  }




3. Exception ÀÌ ¹ß»ýÇßÀ» ¶§µµ Connection Àº ´ÝÇô¾ß ÇÑ´Ù !!

  public void doGet(HttpServletRequest req, HttpServletResponse res)  
       throws ServletException, IOException, SQLException
  {
      String id = req.getParameter("id");

      Connection conn = DriverManager.getConnection("url...","id","password");
      Statement stmt = conn.createStatement();
      ResultSet rs = stmt.executeQuery("ssselect * from XXX where id = '" + id + "'");
      while(rs.next()) {
         ......  
      }  
      rs.close();
      stmt.close();
      conn.close();
      .....
  }

  À§¿¡¼± ¹¹°¡ À߸øµÇ¾úÀ»±î¿ä? ³×, »ç½Ç Ưº°È÷ À߸øµÈ °Í ¾ø½À´Ï´Ù. ´ÜÁö SQL¹®Àå¿¡ ¿ÀŸ°¡
  ÀÖ´Ù´Â °ÍÀ» Á¦¿ÜÇϰï.... ±Ùµ¥, °ú¿¬ ±×·²±î¿ä?
  SQLException À̶ó´Â °ÍÀº Runtime ½Ã¿¡ ¹ß»ýÇÕ´Ï´Ù. DB ÀÇ Á¶°ÇÀÌ ¸ÂÁö ¾Ê´Â´Ù°Å³ª
  °³¹ß±â°£ Áß¿¡ °³¹ßÀÚÀÇ ½Ç¼ö·Î SQL¹®Àå¿¡ À§Ã³·³ ¿ÀŸ¸¦ ÀûÀ» ¼öµµ ÀÖÁÒ.
  ¹®Á¦´Â Exception ÀÌ ¹ß»ýÇÏ¸é ¸¶Áö¸· ¶óÀεé Áï, rs.close(), stmt.close(), conn.close()
  °¡ ¼öÇàµÇÁö ¾Ê´Â´Ù´Â °ÍÀÔ´Ï´Ù.
  java.sql.Connection Àº reference ¸¦ ÀÒ´õ¶óµµ JVM(Java Virtual Machine)ÀÇ GC(Garbage
  Collection) ´ë»óÀÌ ¾Æ´Õ´Ï´Ù. °¡¶àÀ̳ª ¸ðÀÚ¶ó´Â "DB¿¬°áÀÚ¿ø"À» ƯÁ¤ÇÑ ¾îÇø®ÄÉÀ̼ÇÀÌ
  Á¡À¯ÇÏ°í ³õ¾Æ ÁÖÁö ¾Ê±â ¶§¹®¿¡ ¾ó¸¶¾È°¡ DB Connection À» ´õÀÌ»ó ¿¬°áÇÏÁö ¸øÇÏ´Â
  »çŰ¡ ¹ß»ýÇÕ´Ï´Ù.

  µû¶ó¼­ ´ÙÀ½Ã³·³ Exception ÀÌ ¹ß»ýÇÏµç ¹ß»ýÇÏÁö ¾Êµç ¹Ýµå½Ã java.sql.Connection À»
  close() ÇÏ´Â ·ÎÁ÷ÀÌ ²À(!) µé¾î°¡¾ß ÇÕ´Ï´Ù.

  public void doGet(HttpServletRequest req, HttpServletResponse res)  
       throws ServletException, IOException, SQLException
  {
      Connection conn = null;
      Statement stmt = null;
      ResultSet rs = null;
      String id = req.getParameter("id");
      try {
        conn = DriverManager.getConnection("url...","id","password");
        stmt = conn.createStatement();
        rs = stmt.executeQuery("sselect * from XXX where id = '" + id + "'");
        while(rs.next()) {
         ......  
        }
        rs.close();
        stmt.close();
      }
      finally {
         if ( conn != null ) try {conn.close();}catch(Exception e){}
      }
      .....
  }

  Âü°í·Î, ½ÇÇÁ·ÎÁ§Æ®ÀÇ Áø´Ü ¹× Æ©´×À» ³ª°¡º¸¸é óÀ½¿¡´Â ÀûÀýÇÑ ÀÀ´ä¼Óµµ°¡ ³ª¿À´Ù°¡
  ÀÏÁ¤ÇÑ È½¼ö ÀÌ»óÀ» È£ÃâÇÏ°í ³­ µÚºÎÅÍ ¾öû ÀÀ´ä½Ã°£ÀÌ ´À·ÁÁø´Ù¸é, ½ÊÁßÆÈ±¸´Â À§Ã³·³
  java.sql.Connection À» ´ÝÁö ¾Ê¾Æ¼­ »ý±â´Â ¹®Á¦ÀÔ´Ï´Ù.
  °¡¿ëÇÑ ¸ðµç Connection À» ¿¬°áÇÏ¿© ´õÀÌ»ó ¿¬°á½Ãų Connection ÀÚ¿øÀ» ÇÒ´çÇÒ ¼ö ¾øÀ»
  ¶§, ´ëºÎºÐ timewait ÀÌ °É¸®±â ¶§¹®ÀÔ´Ï´Ù. ÀÏ´Ü DB°ü·ÃÇÑ ÀÛ¾÷ÀÌ µé¾î¿À´Â Á·Á·
  timewait¿¡ ºüÁú °æ¿ì, "¾îÇø®ÄÉÀ̼Ǽ­¹ö"¿¡¼­ µ¿½Ã¿¡ ó¸®ÇÒ ¼ö ÀÖ´Â ÃÖ´ë °¹¼ö¸¸Å­
  È£ÃâÀÌ Â÷°îÂ÷°î ½×ÀÌ´Â °Ç ºÒ°ú ¸îºÐ °É¸®Áö ¾Ê½À´Ï´Ù. ±× µÚºÎÅÍ´Â ¾Ö±ÄÀº dummy.jsp
  Á¶Â÷ È£ÃâÀÌ µÇÁö ¾Ê°Ô µÇ°í,   ´©±º°¡´Â "½Ã½ºÅÛ ¶Ç Á×¾ú³×¿ä"¶ó¸ç ¹¦ÇÑ ¿ôÀ½À» Áþ°ï
  ÇϰÚÁÒ....



4. Connection »Ó¸¸ ¾Æ´Ï¶ó Statement, ResultSet µµ ¹Ýµå½Ã ´ÝÇô¾ß ÇÑ´Ù !!

4.1  3¹øÀÇ ¿¹Á¦¿¡¼­ Connection ÀÇ close() ¸¸ °í·ÁÇÏ¿´Áö Statement ³ª ResultSet ¿¡ ´ëÇÑ
  close´Â ÀüÇô °í·Á ÇÏÁö ¾Ê°í ÀÖ½À´Ï´Ù. ¹«½¼ ¹®Á¦°¡ ÀÖÀ»±î¿ä? Statement ¸¦ ´ÝÁö ¾Ê¾Æµµ
  Connection À» ´Ý¾ÒÀ¸´Ï Statement ³ª ResultSet Àº ÀÚµ¿À¸·Î µû¶ó¼­ ´ÝÈ÷´Â °Í ¾Æ´Ï³Ä±¸¿ä?
  Ãµ¸¸ÀÇ ¸»¾¸, ¸¸¸¸ÀÇ Äá±ðÁöÀÔ´Ï´Ù.

  ¸¸¾à, DB Connection Pooling À» »ç¿ëÇÏÁö ¾Ê°í Á÷Á¢ JDBC Driver ¸¦ ÀÌ¿ëÇÏ¿© ¸Å¹ø DB
  ¿¬°áÀ» ÇÏ¿´´Ù°¡ ²÷´Â ±¸Á¶¶ó¸é ¹®Á¦°¡ ¾ø½À´Ï´Ù.
  ±×·¯³ª DB Connection Pooling Àº ÀÌÁ¨ º¸ÆíÈ­µÇ¾î ´©±¸³ª DB Connection Pooling À» »ç¿ë
  ÇؾßÇÑ´Ù´Â °ÍÀ» ¾Ë°í ÀÖ½À´Ï´Ù. ±×°ÍÀÌ ¾îÇø®ÄÉÀÌ¼Ç ¼­¹ö°¡ Á¦°øÇØ ÁÖµç, ȤÀº ÀÛÀº
  ¼­ºí·¿¿£Áø¿¡¼­ ¿î¿µÇϰí ÀÖ´Ù¸é Á÷Á¢ ¸¸µé°Å³ª, ÀÎÅͳÝÀ¸·Î µ¹¾Æ´Ù´Ï´Â ³²ÀÇ ¼Ò½º¸¦ °¡Á®´Ù
  »ç¿ëÇϰí ÀÖÀ» °Ì´Ï´Ù.

  ÀÌó·³ DB Connection Pooling À» »ç¿ëÇϰí ÀÖÀ» °æ¿ì´Â Conneciton ÀÌ ½ÇÁ¦ close()µÇ´Â
  °ÍÀÌ ¾Æ´Ï¶ó Pool¿¡ ¹ÝȯµÇ¾î Áö°Ô µÇ´Âµ¥, ƯÁ¤ Connection ¿¡¼­ ¿­¾îµÐ Statement ¸¦
  close() ÇÏÁö ¾ÊÀºÃ¤ ±×³É ¹Ýȯ½ÃÄÑ ³õ°Ô µÇ¸é, ¾ðÁ¨°¡´Â ±× Connection Àº ´ÙÀ½°ú °°Àº
  SQLException À» ¾ß±âÇÒ °¡´É¼ºÀ» ³»Æ÷ÇÏ°Ô µË´Ï´Ù.

  Oracle :
    java.sql.SQLException : ORA-01000: maximum open cursor exceeded !!
                            (ÃÖ´ë¿­±â Ä¿¼­¼ö¸¦ ÃʰúÇß½À´Ï´Ù)
  UDB DB2 :
    COM.ibm.db2.jdbc.DB2Exception: [IBM][JDBC µå¶óÀ̹ö] CLI0601E  À¯È¿ÇÏÁö ¾ÊÀº
      ¸í·É¹® ÇÚµé ¶Ç´Â ¸í·É¹®ÀÌ ´ÝÇû½À´Ï´Ù. SQLSTATE=S1000
    COM.ibm.db2.jdbc.DB2Exception: [IBM][CLI Driver] CLI0129E  ÇÚµé(handle)ÀÌ
      ´õÀÌ»ó ¾ø½À´Ï´Ù. SQLSTATE=HY014        
    COM.ibm.db2.jdbc.DB2Exception: [IBM][CLI Driver][DB2/NT] SQL0954C  ÀÀ¿ëÇÁ·Î±×·¥
      Èü(heap)¿¡ ¸í·É¹®À» ó¸®Çϱâ À§ÇØ »ç¿ë °¡´ÉÇÑ ÀúÀ念¿ªÀÌ ÃæºÐÇÏÁö ¾Ê½À´Ï´Ù.
      SQLSTATE=57011

  ÀÌÀ¯´Â ¾Õ 2)¹ø±Û¿¡¼­ ÀÌ¹Ì ¾ð±Þµå·È½À´Ï´Ù. º¸´Ù ÀÚ¼¼ÇÑ ±â¼úÀû ³»¿ëÀº ¾Æ·¡ÀÇ ±ÛÀ»
  Âü°íÇϼ¼¿ä.
  Connection/Statement ÃÖ´ë µ¿½Ã Open ¼ö
  http://www.javaservice.net/~java/bbs/read.cgi?m=devtip&b=jdbc&c=r_p&n=972287002

  µû¶ó¼­ ¶Ç´Ù½Ã 3¹øÀÇ ¼Ò½º´Â ´ÙÀ½°ú °°Àº À¯ÇüÀ¸·Î °íÃÄÁ®¾ß ÇÕ´Ï´Ù.

  public void doGet(HttpServletRequest req, HttpServletResponse res)  
       throws ServletException, IOException, SQLException
  {
      Connection conn = null;
      Statement stmt = null;
      ResultSet rs = null;
      String id = req.getParameter("id");
      try {
        conn = ...<getConnection()>...; // (ÆíÀÇ»ó »ý·«ÇÕ´Ï´Ù.)
        stmt = conn.createStatement();
        rs = stmt.executeQuery("sselect * from XXX where id = '" + id + "'");
        while(rs.next()) {
         ......  
        }
        // rs.close();
        // stmt.close();
      }
      finally {
        if ( rs != null ) try {rs.close();}catch(Exception e){}
        if ( stmt != null ) try {stmt.close();}catch(Exception e){} // <-- !!!!
        if ( conn != null ) ...<releaseConnection()>...; // (ÆíÀÇ»ó »ý·«)

      }
      .....
  }

4.2 »ç½Ç À§¿Í °°Àº ±¸Á¶¿¡¼­, java.sql.StatementÀÇ Äõ¸®¿¡ ÀÇÇÑ ResultSetÀÇ close()¿¡
  ´ëÇÑ °ÍÀº ±×¸® Áß¿äÇÑ °ÍÀº ¾Æ´Õ´Ï´Ù. ResultSetÀº Statement °¡ close() µÉ ¶§ ÇÔ²²
  ÀÚ¿øÀÌ ÇØÁ¦µË´Ï´Ù. µû¶ó¼­ ´ÙÀ½°ú °°ÀÌ Çϼŵµ µË´Ï´Ù.

  public void doGet(HttpServletRequest req, HttpServletResponse res)  
       throws ServletException, IOException, SQLException
  {
      Connection conn = null;
      Statement stmt = null;
      String id = req.getParameter("id");
      try {
        conn = ...<getConnection()>...;
        stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery("sselect * from XXX where id = '" + id + "'");
        while(rs.next()) {
         ......  
        }
        rs.close(); //<--- !!!
      }
      finally {
        // if ( rs != null ) try {rs.close();}catch(Exception e){}
        if ( stmt != null ) try {stmt.close();}catch(Exception e){}
        if ( conn != null ) ...<releaseConnection()>...;
      }
      .....
  }


4.3  °¡Àå ´ëÇ¥ÀûÀ¸·Î À߸ø ÇÁ·Î±×·¡¹ÖÇϰí ÀÖ´Â ¿¹¸¦ µé¶ó¸é ´ÙÀ½°ú °°Àº À¯ÇüÀÔ´Ï´Ù.

  Connection conn = null;
  try {
      conn = ...<getConnection()>....;
      Statement stmt = conn.createStatement();
      stmt.executeUpdate("....");  <--- ¿©±â¼­ SQLException ÀÌ ÀϾ¸é...
      .....
      .....
      .....  <-- ¸¸¾à,ÀÌÂë¿¡¼­ NullPointerException ÀÌ ÀϾ¸é ?
      .....
      stmt.close(); <-- À̰ÍÀ» ŸÁö ¾ÊÀ½ !!!
  }
  finally{
     if ( conn != null ) ...<releaseConnection()>...;
  }


4.4  Statement °¡ close() µÇÁö ¾Ê´Â ¶Ç ÇϳªÀÇ °æ¿ì°¡ ´ÙÀ½°ú °°Àº °æ¿ìÀÔ´Ï´Ù.

  Connection conn = null;
  Statement stmt = null;
  try {
    conn = .....
    stmt = conn.createStatement(); // ....(1)
    rs = stmt.executeQuery("select a from ...");
    .....
    rs.close();

    stmt = conn.createStatement(); // ....(2)
    rs = stmt.executeQuery("select b from ...");
    ....
  }
  finally{
    if ( rs != null ) try {rs.close();}catch(Exception e){}
    if ( stmt != null ) try{stmt.close();}catch(Exception e){}
    if ( conn != null ) ....
  }

  Áï, µÎ¹ø stmt = conn.createStatement() ¸¦ ¼öÇàÇÔÀ¸·Î½á, ¸ÕÀú »ý¼ºµÈ stmt ´Â
  (2)¹ø¿¡ ÀÇÇØ ±× reference °¡ ¾þ¾îÃĹö¸®°Ô µÇ¾î ¿µ¿øÈ÷ close() µÇÁö ¾ÊÀºÃ¤
  ³²¾Æ ÀÖ°Ô µË´Ï´Ù.
  ÀÌ °æ¿ì, (2)¹ø ¹®ÀåÀ» ÁÖ¼®Ã³¸®ÇÏ¿©¾ß °ÚÁö¿ä. Çѹø »ý¼ºµÈ Statement ·Î ¿©·¯¹ø
  Query ¸¦ ¼öÇàÇÏ¸é µË´Ï´Ù.

  Connection conn = null;
  Statement stmt = null;
  try {
    conn = .....
    stmt = conn.createStatement(); // ....(1)
    rs = stmt.executeQuery("select a from ...");
    .....
    rs.close();

    // stmt = conn.createStatement(); // <--- (2) !!!
    rs = stmt.executeQuery("select b from ...");
    ....
  }
  finally{
    if ( rs != null ) try {rs.close();}catch(Exception e){}
    if ( stmt != null ) try{stmt.close();}catch(Exception e){}
    if ( conn != null ) ....
  }


4.5  Statement »Ó¸¸ ¾Æ´Ï¶ó PreparedStatement ¸¦ »ç¿ëÇÒ ¶§·Î ¸¶Âù°¡Áö ÀÔ´Ï´Ù.
  ....
  PreparedStatement pstmt = conn.prepareStatement("select ....");
  ....
  ÀÌ·¸°Ô ¸¸µé¾îÁø  pstmt µµ ¹Ýµå½Ã pstmt.close() µÇ¾î¾ß ÇÕ´Ï´Ù.

  ¿¹¸¦ µé¸é, ´ÙÀ½°ú °°Àº Äڵ带 »ý°¢ÇÒ ¼ö ÀÖ½À´Ï´Ù.

  Connection conn = null;
  try {
    conn = ...<getConnection()>...;
    PreparedStatement pstmt = conn.prepareStatement("select .... ?...?");
    pstmt.setString(1,"xxxx");
    pstmt.setString(2,"yyyy");
    ResultSet rs = pstmt.executeQuery(); <--- ¿©±â¼­ SQLException ÀÌ ÀϾ¸é
    while(rs.next()){
      ....
    }
    rs.close();
    pstmt.close();  <-- À̰ÍÀ» ŸÁö ¾ÊÀ½ !!!
  }
  finally{
     if ( conn != null ) ...<releaseConnection()>...;
  }

  µû¶ó¼­ °°Àº ¸Æ¶ôÀ¸·Î ´ÙÀ½°ú °°ÀÌ °íÃÄÁ®¾ß ÇÕ´Ï´Ù.
  
  Connection conn = null;
  PreparedStatement pstmt = null;
  ResultSet rs = null; // <-------- !!
  try {
    conn = ...<getConnection()>...;
    pstmt = conn.prepareStatement("select .... ?...?");
    pstmt.setString(1,"xxxx");
    pstmt.setString(2,"yyyy");
    rs = pstmt.executeQuery(); <--- ¿©±â¼­ SQLException ÀÌ ÀϾ¸é
    while(rs.next()){
      ....
    }
    //rs.close();
    //pstmt.close();
  }
  finally{
    if ( rs != null ) try {rs.close();}catch(Exception e){}
    if ( pstmt != null ) try {pstmt.close();}catch(Exception e){} // <-- !!!!
    if ( conn != null ) ...<releaseConnection()>...;
  }


4.6  PreparedStatement ¿¡ °ü·ÃÇØ¼­ ´ÙÀ½°ú °°Àº °æ¿ìµµ »ý°¢ÇÒ ¼ö ÀÖ½À´Ï´Ù.

4.6.1 ¾Õ¼­ÀÇ 4.4¿¡¼­ Statement¸¦ createStatement() ¿¬°ÅǪ µÎ¹ø »ý¼ºÇÏ´Â °Í°ú
  µ¿ÀÏÇÏ°Ô PreparedStatement¿¡¼­µµ ÁÖÀÇÇÏ¼Å¾ß ÇÕ´Ï´Ù. ¿¹¸¦ µé¸é ´ÙÀ½°ú °°½À´Ï´Ù.

  Connection conn = null;
  PreparedStatement pstmt = null;
  ResultSet rs = null;
  try {
    conn = .....
    pstmt = conn.prepareStatement("select a from ..."); // ....(1)
    rs = pstmt.executeQuery();
    .....
    rs.close();

    pstmt = conn.prepareStatement("select b from ..."); // <--- (2) !!!
    rs = pstmt.executeQuery();
    ....
  }
  finally{
    if ( rs != null ) try {rs.close();}catch(Exception e){}
    if ( pstmt != null ) try{pstmt.close();}catch(Exception e){} // <--- (3)
    if ( conn != null ) ...<releaseConnection()>...;
  }

  StatementÀÇ ÀνºÅϽº´Â conn.createStatement() ½Ã¿¡ ÇÒ´çµÇ¾î Áö´Â ¹Ý¸é,
  PreparedStatement´Â conn.prepareStatement("..."); ½Ã¿¡ ÇÒ´çµÇ¾î Áý´Ï´Ù. À§ÀÇ °æ¿ì¿¡¼­
  ¼³·É ¸¶Áö¸· finally Àý¿¡¼­ pstmt.close() ¸¦ Çϰí ÀÖ±â´Â ÇÏÁö¸¸, (1)¹ø¿¡¼­ ÇÒ´çµÇ¾îÁø
  pstmt ´Â (2)¿¡¼­ ¾þ¾îÃÆ±â ¶§¹®¿¡ ¿µ¿øÈ÷ close() µÇÁö ¾Ê°Ô µË´Ï´Ù. µû¶ó¼­, ´ÙÀ½°ú
  °°ÀÌ ÄÚµùµÇ¾î¾ß ÇÑ´Ù´Â °ÍÀº ÀÚ¸íÇÕ´Ï´Ù.

  Connection conn = null;
  PreparedStatement pstmt = null;
  ResultSet rs = null;
  try {
    conn = .....
    pstmt = conn.prepareStatement("select a from ..."); // ....(1)
    rs = pstmt.executeQuery();
    .....
    rs.close();
    pstmt.close(); // <------- !!!!! ÀÌó·³ ¿©±â¼­ ¸ÕÀú close() ÇØ¾ßÁö¿ä.

    pstmt = conn.prepareStatement("select b from ..."); // <--- (2)
    rs = pstmt.executeQuery();
    ....
  }
  finally{
    if ( rs != null ) try {rs.close();}catch(Exception e){}
    if ( pstmt != null ) try{pstmt.close();}catch(Exception e){} // <--- (3)
    if ( conn != null ) ...<releaseConnection()>...;
  }

  È¤Àº ´ÙÀ½°ú °°ÀÌ ¼­·Î ´Ù¸¥ µÎ°³ÀÇ PreparedStatement¸¦ ÀÌ¿ëÇÒ ¼öµµ ÀÖ½À´Ï´Ù.
  
  Connection conn = null;
  PreparedStatement pstmt1 = null;
  ResultSet rs1 = null;
  PreparedStatement pstmt2 = null;
  ResultSet rs2 = null;
  try {
    conn = .....
    pstmt1 = conn.prepareStatement("select a from ..."); // ....(1)
    rs1 = pstmt1.executeQuery();
    .....
    pstmt2 = conn.prepareStatement("select b from ..."); // <--- (2)
    rs2 = pstmt2.executeQuery();
    ....
  }
  finally{
    if ( rs1 != null ) try {rs1.close();}catch(Exception e){}
    if ( pstmt1 != null ) try{pstmt1.close();}catch(Exception e){} // <--- (3)
    if ( rs2 != null ) try {rs2.close();}catch(Exception e){}
    if ( pstmt2 != null ) try{pstmt2.close();}catch(Exception e){} // <--- (4)
    if ( conn != null ) ...<releaseConnection()>...;
  }


4.6.2 ¾Æ·¡´Â ¾Õ¼­ÀÇ 4.6.1°ú °°Àº ¸Æ¶ôÀε¥, for loop ¾È¿¡¼­ »ç¿ëµÈ °æ¿ìÀÔ´Ï´Ù.

  Connection conn = null;
  PreparedStatement pstmt = null;
  try {
    conn = ...<getConnection()>...;
    for(int i=0;i<10;i++){
      pstmt = conn.prepareStatement("update .... ?... where id = ?"); //... (1)
      pstmt.setString(1,"xxxx");
      pstmt.setString(2,"id"+(i+1) );
      int affected = pstmt.executeUpdate();
      if ( affected == 0 ) throw new Exception("NoAffected");
      else if ( affedted > 1 ) throw new Exception("TooManyAffected");
    }
  }
  finally{
     if ( pstmt != null ) try {pstmt.close();}catch(Exception e){} // ...(2)
     if ( conn != null ) ...<releaseConnection()>...;
  }

  ÀÌ °æ¿ì°¡ ½ÇÁ¦ ÇÁ·ÎÁ§Æ® performace Æ©´×À» °¡ º¸¸é Á¾Á¾ ¹ß°ßµÇ´Â À߸øµÈ
  À¯Çü ÁßÀÇ ÇϳªÀÔ´Ï´Ù. ÇÙ½ÉÀº pstmt.prepareStatement("update..."); ¹®ÀåÀ»
  ¼öÇàÇÒ ¶§ ¸¶´Ù ³»ºÎÀûÀ¸·Î pstmt °¡ »õ·Î ÇÒ´çµÈ´Ù´Â °Í¿¡ ÀÖ½À´Ï´Ù.
  °á±¹, (1)¹ø ¹®ÀåÀÌ for loop À» µ¹¸é¼­ 9°³´Â pstmt.close()°¡ µÇÁö ¾Ê°í, ¸¶Áö¸·
  ÇϳªÀÇ pstmt ¸¸  finally ÀýÀÇ (2)¹ø¿¡¼­ close µÇÁö¿ä...
  µû¶ó¼­ ´ÙÀ½Ã³·³ °íÃÄÁ®¾ß ÇÕ´Ï´Ù.

  Connection conn = null;
  PreparedStatement pstmt = null;
  try {
    conn = ...<getConnection()>...;
    pstmt = conn.prepareStatement("update .... ?... where id = ?");
    for(int i=0;i<10;i++){
      pstmt.clearParameters();      
      pstmt.setString(1,"xxxx");
      pstmt.setString(2,"id"+(i+1) );
      int affected = pstmt.executeUpdate();
      if ( affected == 0 ) throw new Exception("NoAffected");
      else if ( affedted > 1 ) throw new Exception("TooManyAffected");
    }
  }
  finally{
     if ( pstmt != null ) try {pstmt.close();}catch(Exception e){}
     if ( conn != null ) ...<releaseConnection()>...;
  }

  PreparedStatement ¶ó´Â °ÍÀÌ, Çѹø ÆÄ½ÌÇÏ¿© µ¿ÀÏÇÑ SQL¹®ÀåÀ» °ð¹Ù·Î ExecutionÇÒ ¼ö
  ÀÖ´Â ÀåÁ¡ÀÌ ÀÖ´Â °ÍÀ̰í, ±Ã±ØÀûÀ¸·Î À§¿Í °°Àº °æ¿ì¿¡ È¿°ú¸¦ ±Ø´ëÈ­ ÇÒ ¼ö ÀÖ´Â
  °ÍÀÌÁö¿ä.

  ¾î´À °³¹ßÀÚÀÇ ¼Ò½º¿¡¼­´Â À§ÀÇ °æ¿ì¸¦ ´ÙÀ½°ú °°ÀÌ for loop ¾È¿¡¼­ ¸Å¹ø
  conn.prepareStatement(...)¸¦ ÇÏ´Â °æ¿ì¸¦ º¸¾Ò½À´Ï´Ù.

  Connection conn = null;
  
  try {
    conn = ...<getConnection()>...;
    for(int i=0;i<10;i++) {
        PreparedStatement pstmt = null;
        try{
            pstmt = conn.prepareStatement("update .... ?... where id = ?");
            pstmt.clearParameters();      
            pstmt.setString(1,"xxxx");
            pstmt.setString(2,"id"+(i+1) );
            int affected = pstmt.executeUpdate();
            if ( affected == 0 ) throw new Exception("NoAffected");
            else if ( affedted > 1 ) throw new Exception("TooManyAffected");
        }
        finally{
            if ( pstmt != null ) try {pstmt.close();}catch(Exception e){}
        }
    }
  }
  finally{
     if ( conn != null ) ...<releaseConnection()>...;
  }

  À§ °æ¿ì´Â Àå¾Ö°üÁ¡¿¡¼­ º¸¸é »ç½Ç º° ¹®Á¦´Â ¾ø½À´Ï´Ù. Àû¾îµµ ´ÝÀ» °Ç ¸ðµÎ Àß ´Ý°í
  ÀÖÀ¸´Ï±î¿ä. ´ÜÁö È¿À²¼ºÀÇ ¹®Á¦°¡ ´ëµÎµÉ ¼ö ÀÖÀ» »ÓÀÔ´Ï´Ù.

4.7 »ý°¢ÇØ º¸¸é, Statement ³ª PreparedStatement °¡ close() µÇÁö ¾Ê´Â À¯ÇüÀº
  ¿©·¯°¡Áö°¡ ÀÖ½À´Ï´Ù. ±×·¯³ª ¿­·ÁÁø Statement´Â ¹Ýµå½Ã close()µÇ¾î¾ß ÇÑ´Ù¶ó´Â
  ´Ü¼øÇÑ »ç½Ç¿¡ Á¶±Ý¸¸ ½Å°æ¾²½Ã¸é ¾î¶»°Ô ÇÁ·Î±×·¡¹ÖÀÌ µÇ¾î¾ß ÇÏ´ÂÁö ½±°Ô
  °¨ÀÌ ¿À½Ç °Ì´Ï´Ù. ´ÜÁö ½Å°æÀ» ¾È¾²½Ã´Ï ¹®Á¦°¡ µÇ´Â °ÍÀÌÁö¿ä...

  Statement ¸¦ ´ÝÁö ¾Ê´Â ½Ç¼ö¸¦ ÇÑ Äڵ尡 400-1000¿©°³ÀÇ Àü ¾îÇø®ÄÉÀ̼ÇÀ» ÅëÅоî
  Çѵαºµ¥¿¡ ¼û¾î ÀÖ´Â °æ¿ì°¡ Á¦ÀÏ Ã£¾Æ³»±â ¾î·Æ½À´Ï´Ù. Çѵιø Statement ¸¦
  close ÇÏÁö ¾Ê´Â´Ù°í ÇÏ¿© °ð¹Ù·Î ¹®Á¦·Î ³ªÅ¸³ªÁö ¾Ê±â ¶§¹®ÀÔ´Ï´Ù.
  ÇϷ糪 ÀÌÆ², ȤÀº ¸çÄ¥Áö³ª¼­¾ß, Oracle ÀÇ °æ¿ì, "maximum open cursor exceed"
  ¿¡·¯¸¦ ³»°Ô µË´Ï´Ù. oracle ÀÇ °æ¿ì, À§¿Í °°Àº ¿¡·¯¸¦ conn.createStatement()½Ã¿¡
  ¹ß»ýÇϹǷΠ(°æ¿ì¿¡ µû¶ó) Å« ¹®Á¦·Î À̾îÁöÁö´Â ¾Ê½À´Ï´Ù. ã¾Æ¼­ °íÄ¡¸é µÇ´Ï±î¿ä.

  ¹Ý¸é, DB2 ÀÇ °æ¿ì´Â ¸Þ¸ð¸®°¡ Çã¿ëÇÏ´Â Çѵµ±îÁö Áö¼ÓÀûÀÎ ¸Þ¸ð¸® Áõ°¡¸¦ ¾ß±âÇÕ´Ï´Ù.
  ±Þ±â¾ß "ÇÚµé(handle)ÀÌ ´õÀÌ»ó ¾ø½À´Ï´Ù", "ÀÀ¿ëÇÁ·Î±×·¥ Èü(heap)¿¡ ¸í·É¹®À»
  Ã³¸®Çϱâ À§ÇØ »ç¿ë °¡´ÉÇÑ ÀúÀ念¿ªÀÌ ÃæºÐÇÏÁö ¾Ê½À´Ï´Ù", "À¯È¿ÇÏÁö ¾ÊÀº ¸í·É¹®
  ÇÚµé ¶Ç´Â ¸í·É¹®ÀÌ ´ÝÇû½À´Ï´Ù" µî°ú °°Àº ¿¡·¯¸¦ ³»Áö¸¸, ¿©±â¼­ ±×Ä¡´Â °ÍÀÌ ¾Æ´Ï¶ó
  »õ·Î¿î connection À» ¸Î´Â ½ÃÁ¡¿¡ SIGSEGV ¸¦ ³»¸é crashing ÀÌ ÀϾ´Ï´Ù.
  java.lang.OutOfMemoryError °¡ ¹ß»ýÇϱ⠶§¹®ÀÌÁö¿ä.

  Oracle ÀÇ °æ¿ìµµ »ç½Ç »óȲ¿¡ µû¶ó ½É°¢ÇÒ ¼ö ÀÖ½À´Ï´Ù. ¿¹¸¦ µé¾î, ¾î¶² °³¹ßÀÚ°¡
  "maximum open cursor exceed"¶ó´Â Oracle SQL ¿¡·¯ ¸Þ¼¼Áö¸¦ ¸¸³ª°í´Â ÀÚ½ÅÀÌ
  ÄÚµùÀ» À߸øÇÑ °ÍÀº ¿°µÎ¿¡ µÎÁö ¾Ê°í, ¹«Á¶°Ç DBA¿¡°Ô oraXXX.ini ÆÄÀÏ¿¡¼­
  OPEN_CURSORS °ªÀ» ¿Ã·Á´Þ¶ó°í ¿äûÇϰí, DBA´Â ±× Á¶¾ð(?)À» Ãæ½ÇÈ÷ ¹Þ¾Æµé¿©
  Default 50 ¿¡¼­ À̸¦ 3000 À¸·Î Á¶Á¤ÇÕ´Ï´Ù. ¿©±â¼­ ¹®Á¦´Â ±í¼÷È÷(?) ¼û°ÜÁý´Ï´Ù.
  close() ¾ÈµÈ Statement °¡ 3000 ȸ¿¡ µµ´ÞÇÏÁö Àü±îÁø ¾Æ¹«µµ ¹®Á¦¸¦ ÀνÄÇÏÁö
  ¸øÇϱ⠶§¹®ÀÌÁÒ. ±×·¯³ª, Statement°¡ Çϳª¾¿ close()µÇÁö ¾ÊÀº °¹¼ö¿¡ ºñ·ÊÇÏ¿©
  Oracle ¿£ÁøÀÇ ¸Þ¸ð¸® »ç¿ëÀº ÀÚ²Ù¸¸ Áõ°¡Çϰí, ÀüüÀûÀÎ ¼º´ÉÀúÇϸ¦ ¾ß±âÇÏÁö¸¸,
  À̸¦ ÀνÄÇϱâ¶õ ¸·»ó ½Ã½ºÅÛÀ» ¿ÀÇÂÇÏ°í ³ª¼­ 3-4ÀÏÀÌ Áö³­ ÈÄ¿¡ "DB¼º´ÉÀÌ ÀÌ»óÇϳ×,
  ÀÀ´ä¼Óµµ°¡ ´À¸®³×" Çϸ鼭 ¶Ç ÇѹøÀÇ "maximum open cursor exceed" ¸Þ¼¼Áö¸¦
  È®ÀÎÇÏ°í ³­ µÚÀÇ ÀÏÀÌ µÇ°ï ÇÕ´Ï´Ù.

  ¿¡·¯°¡ ¾ø´Â Á¤»óÀûÀÎ ·ÎÁ÷ flow ¿¡¼­´Â ´ëºÎºÐ Statement°¡ Àß ´ÝÈú °Ì´Ï´Ù. ±×·¯³ª,
  ¾î¶² ÀÌÀ¯¿¡¼­°Ç ¾ÆÁÖ µå¹°°Ô Runtime Exception ÀÌ ¹ß»ýÇÏ¿© exception throwingÀ¸·Î
  ÀÎÇØ "stmt.close()"¸¦ ŸÁö ¾Ê´Â °æ¿ì°¡ Á¦ÀÏ ¹«¼·Áö¿ä. Á¤¸» ¹«¼·Áö¿ä...

  Statement, PreparedStatement, CallableStatement ¸ðµÎ ¸¶Âù°¡Áö ÀÔ´Ï´Ù.



5. close() ¸¦ ÇÒ ¶© Á¦´ë·Î ÇØ¾ß ÇÑ´Ù!!

5.1 ´ÙÀ½°ú °°Àº ÇÁ·Î±×·¥ Çü½ÄÀ» »ý°¢ÇÒ ¼ö ÀÖ½À´Ï´Ù.

  Connection conn = null;
  Statement stmt = null;
  ResultSet rs = null;
  try{
     conn = ...<getConnection()>...; //.......(1)
     stmt = conn.createStatement();    //.............(2)
     rs = stmt.executeQuery("select .....");  // .....(3)
     while(rs.next()){
       ......
     }
  }
  finally {
     try {
        rs.close(); //........(4)
        stmt.close(); //......(5)
        ...<releaseConneciton()>...; //......(6)
     }catch(Exception e){}
  }

  À§¿¡¼± ¹¹°¡ À߸øµÇ¾úÀ»±î¿ä? ´Ù Á¦´ë·Î Çѵí Çѵ¥....
  finally Àý¿¡¼­ rs, stmt, conn À» null check ¾øÀÌ, ±×¸®°í µ¿ÀÏÇÑ try{}catch Àý·Î
  ½ÇÇàÇϰí ÀÖ½À´Ï´Ù.
  ¿¹¸¦ µé¾î, (1), (2) ¹øÀ» °ÅÄ¡¸é¼­ conn °ú stmt °´Ã¼±îÁö´Â Á¦´ë·Î ¼öÇàµÇ¾úÀ¸³ª
  (3)¹ø Query¹®ÀåÀ» ¼öÇàÇÏ´Â µµÁß¿¡ SQLException ÀÌ ¹ß»ýÇÒ ¼ö ÀÖ½À´Ï´Ù. ±×·¯¸é,
  finally  Àý¿¡¼­ (4) ¹ø rs.close()¸¦ ¼öÇàÇÏ·Á ÇÕ´Ï´Ù. ±×·¯³ª, executeQuery()°¡
  ½ÇÆÐ Ç߱⠶§¹®¿¡ rs ÀÇ reference ´Â null À̹ǷΠrs.close() ½Ã¿¡
  NullPointerException ÀÌ ¹ß»ýÇÕ´Ï´Ù.  °á±¹ Á¤ÀÛ ¹Ýµå½Ã ¼öÇàµÇ¾î¾ßÇÒ (5)¹ø, (6)¹øÀÌ
  ½ÇÇàµÇÁö ¾Ê½À´Ï´Ù. µû¶ó¼­ ¹Ýµå½Ã(!) ´ÙÀ½°ú °°Àº Çü½ÄÀÇ ÄÚµùÀÌ µÇ¾î¾ß ÇÕ´Ï´Ù.

  Connection conn = null;
  Statement stmt = null;
  ResultSet rs = null;
  try{
     conn = ...<getConnection()>...;
     stmt = conn.createStatement();
     rs = stmt.executeQuery("select .....");
     while(rs.next()){
       ......
     }
  }
  catch(Exception e){
     ....
  }
  finally {
     if ( rs != null ) try{rs.close();}catch(Exception e){}
     if ( stmt != null ) try{stmt.close();}catch(Exception e){}
     if ( conn != null ) ...<releaseConnection()>...;
  }


5.2.0 ¾Õ¼­ 4.2¿¡¼­µµ ¾ð±ÞÇßÁö¸¸, java.sql.StatementÀÇ executeQuery()¿¡ ÀÇÇÑ ResultSetÀº
  Statement °¡ close µÉ¶§ ÀÚ¿øÀÌ °°ÀÌ ÇØÁ¦µÇ¹Ç·Î ´ÙÀ½°ú °°ÀÌ ÇÏ¿©µµ ±×¸® ¹®Á¦°¡ µÇÁø
  ¾Ê½À´Ï´Ù.
    
  Connection conn = null;
  Statement stmt = null;
  //ResultSet rs = null;
  try{
     conn = ...<getConnection()>...;
     stmt = conn.createStatement();
     ResultSet rs = stmt.executeQuery("select .....");
     while(rs.next()){
       ......
     }
     rs.close(); // <---- !!!
  }
  catch(Exception e){
     ....
  }
  finally {
     //if ( rs != null ) try{rs.close();}catch(Exception e){}
     if ( stmt != null ) try{stmt.close();}catch(Exception e){}
     if ( conn != null ) ...<releaseConnection()>...;
  }


  5.2.1  ±×·¯³ª PreparedStatement¿¡ ÀÇÇÑ ResultSet close()´Â ¾ê±â°¡ ´Ù¸¦ ¼ö ÀÖ½À´Ï´Ù.
  (2002.06.11 Ãß°¡ »çÇ×)
  
  À¥¾îÇø®ÄÉÀ̼Ǽ­¹öµéÀº ¼º´ÉÇâ»óÀ» À§ÇØ PreparedStatement¿¡ ´ëÇÑ Ä³½Ì±â´ÉÀ» Á¦°øÇϰí
  ÀÖ½À´Ï´Ù. pstmt.close() ¸¦ ÇÏ¿´´Ù°í ÇÏ¿© Á¤¸» ÇØ´ç PreparedStatement°¡ closeµÇ´Â
  °ÍÀÌ ¾Æ´Ï¶ó, ÇØ´ç PreparedeStatement°¡ »ý°Ü³­ java.sql.Connection´ç ÁöÁ¤µÈ °³¼ö±îÁö
  À¥¾îÇø®ÄÉÀ̼Ǽ­¹ö ³»ºÎ¿¡¼­ reference°¡ »ç¶óÁöÁö ¾Ê°í ij½Ã·Î ³²¾Æ ÀÖ°Ô µË´Ï´Ù.
  °á±¹, ¿¬°üµÈ ResultSet ¿ª½Ã close()°¡ µÇÁö ¾ÊÀº ä ³²¾ÆÀÖ°Ô µÇ´Â °ÍÀÌÁö¿ä. ¸í½ÃÀûÀÎ
  rs.close()¸¦ ŸÁö ¾ÊÀ¸¸é, À¥¾îÇø®ÄÉÀ̼Ǽ­¹öÀÇ JVM³»ºÎ¿¡¼­°¡ ¾Æ´Ï¶ó, Äõ¸®ÀÇ °á°ú·Î
  µ¥ÀÌŸº£À̽º¿¡¼­ Àӽ÷Π¸¸µé¾îÁø ¸Þ¸ð¸®µ¥ÀÌŸ°¡ »ç¶óÁöÁö ¾Ê´Â °á°ú¸¦ ³º°Ô µË´Ï´Ù.
  µû¶ó¼­, ¾Õ¼­ÀÇ ÄÚµù ¹æ¹ýÀ» ¹Ýµå½Ã ´ÙÀ½Ã³·³ ÇϽñ⸦ ´Ù½Ã±Ý Á¤Á¤ÇÏ¿© ±ÇÀå µå¸³´Ï´Ù.

  Connection conn = null;
  PreparedStatement pstmt = null;
  ResultSet rs = null; // <---- !!!
  try{
     conn = ...<getConnection()>...;
     pstmt = conn.prepareStatement("select .....");
     rs = pstmt.executeQuery(); // <----- !!!
     while(rs.next()){
       ......
     }
     //rs.close(); // <---- !!!
  }
  catch(Exception e){
     ....
  }
  finally {
     if ( rs != null ) try{rs.close();}catch(Exception e){} // <---- !!!
     if ( pstmt != null ) try{pstmt.close();}catch(Exception e){}
     if ( conn != null ) ...<releaseConnection()>...;
  }

  PS: À¥¾îÇø®ÄÉÀ̼Ǽ­¹ö¿¡¼­ÀÇ PreparedStatement ij½Ì±â´É¿¡ °üÇÑ ºÎºÐÀº ¾Æ·¡ÀÇ ±ÛµéÀ»
  ÂüÁ¶Çϼ¼¿ä.
  Connection Pool & PreparedStatement Cache Size
  http://www.javaservice.net/~java/bbs/read.cgi?m=appserver&b=was&c=r_p&n=995572195
  WebLogic¿¡¼­ÀÇ PreparedStatement Cache -¼­Á¤Èñ-
  http://www.javaservice.net/~java/bbs/read.cgi?m=dbms&b=jdbc&c=r_p&n=1023286823



5.3 °£È¤, ´ÙÀ½°ú °°Àº ÄÚµùÀº ¹®Á¦¸¦ ¾ß±âÇÏÁö ¾ÊÀ» °ÍÀ̶ó°í »ý°¢ÇÏ´Â ºÐµéÀÌ ¸¹½À´Ï´Ù.

finally{
    try{
      if ( stmt != null ) stmt.close();
      if ( conn != null ) conn.close();
    }catch(Exception e){}
}
Àú¸íÇÑ ¸¹Àº Ã¥¿¡¼­µµ À§Ã³·³ ÄÚµùµÇ¾î ÀÖ´Â °æ¿ì¸¦ Á¾Á¾º¾´Ï´Ù. ¸Â½À´Ï´Ù. Ưº°È÷ ¹®Á¦¼ºÀÌ
À־ÀÌÁö´Â ¾Ê½À´Ï´Ù. ±×·¯³ª, °£È¤ À¥¾îÇø®ÄÉÀ̼Ǽ­¹öÀÇ Connection Pool°ú ¿¬°èÇÏ¿©
»ç¿ëÇÒ ¶© ¶§·Ð ¹®Á¦°¡ µÉ ¶§µµ ÀÖ½À´Ï´Ù. Áï, ¸¸¾à, stmt.close()½Ã¿¡ ExceptionÀÌ throw
µÇ¸é ¾î¶»°Ô µÇ°Ú½À´Ï±î?
¾Æ´Ï ¹«½¼ null üũ±îÁö Çߴµ¥, ¹«½¼ ExceptionÀÌ ¹ß»ýÇÏ´À³Ä°í ¹Ý¹®ÇÒ ¼öµµ ÀÖÁö¸¸,
¿À·£ ½Ã°£ÀÌ °É¸®´Â SQL JOB¿¡ ºüÁ® ÀÖ´Ù°¡ Connection PoolÀÇ "Orphan Timeout"ÀÌ Áö³ª
ÀÚµ¿À¸·Î ÇØ´ç ConnectionÀ» Pool¿¡ µ¹·Áº¸³»°Å³ª ȤÀº Ưº°ÇÑ marking󸮸¦ ÇØ µÑ ¼ö
ÀÖ½À´Ï´Ù. ÀÌ °æ¿ì¶ó¸é stmt.close()½Ã¿¡ ÇØ´ç À¥¾îÇø®ÄÉÀ̼Ǽ­¹ö¿¡ ƯȭµÈ ExceptionÀÌ
¹ß»ýÇÏ°Ô µË´Ï´Ù. ±×·¸°Ô µÇ¸é conn.close()¸¦ ŸÁö ¸øÇÏ°Ô µÇ´Â »çŰ¡ ¹ú¾îÁý´Ï´Ù.
µû¶ó¼­, ¾Õ¼­ Ç϶ó°í ±ÇÀåÇÑ Çü½ÄÀ¸·Î ÄÚµùÇϼ¼¿ä.


6. Nested SQL Query Issue !!

6.1 ¾Æ·¡¿Í °°Àº ÄÚµùÀ» »ý°¢ÇØ º¸°Ú½À´Ï´Ù.

  Connection conn = null;
  Statement stmt = null;
  try{
     conn = ...<getConnection()>...;
     stmt = conn.createStatement();
     ResultSet rs = stmt.executeQuery("select deptno ...where id ='"+ id +"'");
     while(rs.next()){
       String deptno = rs.getString("deptno");
       stmt.executeUpdate(
           "update set dept_name = ... where deptno = '"+ deptno +"'"
       );
       ......
     }
     rs.close();
  }
  catch(Exception e){
     ....
  }
  finally {
     if ( stmt != null ) try{stmt.close();}catch(Exception e){}
     if ( conn != null ) ...<releaseConnection()>...;
  }

  À§ ÄÚµå´Â »ç½Ç ½ÇÇàÇÏÀÚ ¸»ÀÚ ´ÙÀ½°ú °°Àº ¿¡·¯¸¦ ¸¸³ª°Ô µË´Ï´Ù.

  DB2 : -99999: [IBM][CLI Driver] CLI0115E Ä¿¼­ »óŰ¡ À¯È¿ÇÏÁö ¾Ê½À´Ï´Ù.
        SQLSTATE=24000
  Oracle :

  ¿¡·¯´Â µÎ¹øÂ° while(rs.next()) ½Ã¿¡ ¹ß»ýÇÕ´Ï´Ù. ¿Ö³Ä¸é, Statement°¡ nestedÇϰÔ
  ½ÇÇàµÈ executeUpdate() ¿¡ ÀÇÇØ ¾þ¾îÃÄ ¹ö·È±â ¶§¹®ÀÔ´Ï´Ù. ResultSet Àº
  Statement¿Í ¹ÐÁ¢ÇÏ°Ô °ü·ÃÀÌ ÀÖ½À´Ï´Ù. ResultSetÀº ÀڷḦ ÀúÀåÇϰí ÀÖ´Â °´Ã¼°¡
  ¾Æ´Ï¶ó, Interface ÀÏ »ÓÀ̱⠶§¹®ÀÔ´Ï´Ù. À§ ÄÚµå´Â ´ÙÀ½Ã³·³ ¹Ù²î¾îÁ®¾ß ÇÕ´Ï´Ù.

  Connection conn = null;
  Statement stmt1 = null;
  Statement stmt2 = null;
  try{
     conn = ...<getConnection()>...;
     stmt1 = conn.createStatement();
     stmt2 = conn.createStatement();
     ResultSet rs = stmt1.executeQuery("select deptno ...where id ='"+ id +"'");
     while(rs.next()){
       String deptno = rs.getString("deptno");
       stmt2.executeUpdate(
           "update set dept_name = ... where deptno = '"+ deptno +"'"
       );
       ......
     }
     rs.close();
  }
  catch(Exception e){
     ....
  }
  finally {
     if ( stmt1 != null ) try{stmt1.close();}catch(Exception e){}
     if ( stmt2 != null ) try{stmt2.close();}catch(Exception e){}
     if ( conn != null ) ...<releaseConnection()>...;
  }

  Áï, ResultSet¿¡ ´ëÇÑ fetch ÀÛ¾÷ÀÌ ¾ÆÁ÷ ³²¾Æ ÀÖ´Â »óÅ¿¡¼­ °ü·ÃµÈ Statement¸¦
  ¶Ç´Ù½Ã executeQuery()/executeUpdate() ¸¦ ÇÏ¸é ¾ÈµÈ´Ù´Â °ÍÀÔ´Ï´Ù.

  PS: IBM WebSphere ȯ°æÀÏ °æ¿ì, ¾Æ·¡ÀÇ ±ÛµéÀ» Ãß°¡·Î È®ÀÎÇϽñ⠹ٶø´Ï´Ù.
  349   Re: Function sequence error  (Version 3.x)
  http://www.javaservice.net/~java/bbs/read.cgi?m=appserver&b=was&c=r_p&n=991154615
  486   WAS4.0x: Function sequence error ÇØ°á  
  http://www.javaservice.net/~java/bbs/read.cgi?m=appserver&b=was&c=r_p&n=1015345459



7. executeUpdate() ÀÇ °á°ú¸¦ ºñÁî´Ï½º ·ÎÁ÷¿¡ ¸Â°Ô ÀûÀýÈ÷ Ȱ¿ëÇ϶ó.

7.1 ¾Æ·¡¿Í °°Àº ÄÚµùÀ» »ý°¢ÇØ º¸°Ú½À´Ï´Ù.

  public void someMethod(String empno) throws Exception {
      Connection conn = null;
      Statement stmt = null;
      try{
         conn = ...<getConnection()>...;
         stmt = conn.createStatement();
         stmt.executeUpdate(
             "UPDATE emp SET name='ÀÌ¿ø¿µ' WHERE empno = '" + empno + "'"
         );
      }
      finally {
         if ( stmt != null ) try{stmt.close();}catch(Exception e){}
         if ( conn != null ) ...<releaseConnection()>...;
      }
  }

  »ç½Ç ÈçÈ÷µé ÀÌ·¸°Ô ÇϽʴϴÙ. º°·Î À߸øµÈ °Íµµ ¾ø¾îº¸ÀÔ´Ï´Ù. ±Ùµ¥, ¸¸¾à
  DB TABLE¿¡ ÇØ´ç empno °ªÀÌ ¾øÀ¸¸é ¾î¶»°Ô µÉ±î¿ä? SQLException ÀÌ ¹ß»ý
  Çϳª¿ä? ±×·¸Áö ¾ÊÁÒ? ¾Æ¹«·± ¿¡·¯¾øÀÌ ±×³É Èê·¯ ³»·Á ¿É´Ï´Ù. ±×·¯¸é, Update¸¦
  ÇÏ·¯ µé¾î ¿Ô´Âµ¥, DB¿¡ UpdateÇÒ °ÍÀÌ ¾ø¾ú´Ù¸é ¾î¶»°Ô ÇØ¾ß Çմϱî? ±×³É ¹«½ÃÇϸé
  µÇ³ª¿ä? ¾ÈµÇÁÒ..  µû¶ó¼­, ´ÙÀ½ ó·³, ÀÌ·¯ÇÑ »óȲÀ» ÀÌ ¸Þ¼Òµå¸¦ ºÎ¸¥ °÷À¸·Î
  ¾Ë·Á Áà¾ß ÇÕ´Ï´Ù.

  public void someMetho
* iWiz´Ô¿¡ ÀÇÇØ¼­ °Ô½Ã¹° À̵¿µÇ¾ú½À´Ï´Ù (2010-02-03 16:57)



62   mod_throttle ¸ðµâÀ» ÀÌ¿ëÇÑ »ç¿ëÀÚ Æ®·¡ÇÈ Á¦¾î  iWiz 2006/06/22 9063 0
61   Tomcat-Apache using JK2 connector  iWiz 2004/03/21 7799 41
60   RedHat 9.0¿¡¼­ÀÇ JRun JSP ÄÄÆÄÀÏ·¯ÀÇ ¹®Á¦Á¡  iWiz 2004/01/04 5553 50
59   RedHat 9.0¿¡¼­ÀÇ JRun-Apache Ä¿³ØÅÍÀÇ ¹®Á¦Á¡  iWiz 2004/01/04 5187 48
58   JRun 4.0ÀÇ Æ©´× °ü·Ã ¿É¼Ç  iWiz 2004/01/04 5945 68
57   JRun 4.0ÀÇ Activity ¸ð´ÏÅ͸µ ¹æ¹ý  iWiz 2004/01/04 4921 57
56   JRun4.0: DataSource Ä¿³Ø¼ÇÇ® °ü·Ã ¿É¼Ç [4]  iWiz 2004/01/04 6664 46
55   JRun¿¡¼­ JSP ÄÄÆÄÀϽà java ÆÄÀÏ »ý¼ºÇÏ±â  iWiz 2004/01/04 8068 63
54   JRunÀÇ ½ÇÁ¦ ¼­ºñ½º ¿î¿µ½Ã °í·Á»çÇ×  iWiz 2004/01/04 6373 44
53   ¼öÁ¤µÈ ÀÎÅÍ³Ý ÀͽºÇ÷η¯¿¡¼­ »óÈ£ÀÛ¿ë ActiveX ÄÁÆ®·Ñ Ȱ¼ºÈ­ °¡ÀÌµå  iWiz 2006/03/03 8457 4
52   HTML Ư¼ö±âÈ£ ¿£ÅÍÆ¼(Entity) Å×À̺í [2]  iWiz 2006/03/03 14183 2
51   À¥»çÀÌÆ®ÀÇ »õ·Î¿î Çõ¸í Ajax [13]  iWiz 2005/11/22 5870 6
50   MSN ¸Þ½ÅÀú Ä£±¸ ÀÚµ¿µî·Ï ½ºÅ©¸³Æ®  iWiz 2004/10/12 6192 35
49   JavaScript MD5 ÇØ½¬ »ý¼º ÇÔ¼ö  iWiz 2004/01/07 9080 35
48   JavaScript·Î ¸¸µç Áø¹ýº¯È¯ ¹× º¸¼ö°è»ê±â [4]  iWiz 2004/01/04 160613 51

1 [2][3][4][5]
 

Copyright 1999-2023 Zeroboard / skin by zero
iWiz ShareBase, ¨ÏCopyleft by iWiz.  For more information contact .
º» À¥»çÀÌÆ®¿¡ °Ô½ÃµÈ À̸ÞÀÏ ÁÖ¼Ò°¡ ÀüÀÚ¿ìÆí ¼öÁý ÇÁ·Î±×·¥À̳ª ±× ¹ÛÀÇ ±â¼úÀû ÀåÄ¡¸¦ ÀÌ¿ëÇÏ¿© ¹«´ÜÀ¸·Î ¼öÁýµÇ´Â °ÍÀ» °ÅºÎÇϸç, À̸¦ À§¹Ý½Ã¿¡´Â Á¤º¸Åë½Å¸Á¹ý¿¡ ÀÇÇØ Çü»çó¹úµÊÀ» À¯³äÇϽñ⠹ٶø´Ï´Ù. [°Ô½ÃÀÏ 2004. 1. 31]