JDBC Connection 리소스 관리시 주의해야 할 점

황제낙엽 2003.09.06 09:18 조회 수 : 67 추천:175

sitelink1  
sitelink2  
sitelink3  
sitelink4  
sitelink5  
sitelink6  

1. Don't share any connection with other thread(Never! 절대! 스레드끼리 connection을 공유해서는 안된다.)

Bad Case 1:
        public class ShoppingController{
                public static Connection con = null; //connection은 절대 static이어서는 안된다.
                static{
                        con = ....;
                }
                .........
        }

Bad Case 2:
        public ControlServlet extends Servlet{
                private Connection con;//connection은 맴버변수로도 선언해서는 안된다.(Servlet)
                .....
        }

Bad Case 3:
        Connection con;
        .....
        new ControlThread(con).start(); //connection은 절대 공유해서는 안된다.
        new ControlThread(con).start(); //connection은 절대 공유해서는 안된다.
        .....

Bad Case 4:
        public class ABean{
                Connection con = ...; //Bean내에서도 맴버변수로는 선언해서는 안된다.        
        }

        In JSP:
        //jsp에서도 마찬가지이다. 두개의 브라우저가 같은 session으로 접근할 수 있다.

Better Case:?
        Connection con = null;        // local variable로 선언하는 것이 정답이다.
        ...
        ... ?
        con.close();  //사용이 끝난 connection은 close로 필히 풀로 반환시킨다.



2. 가능하다면 Statement는 미리 준비토록 한다.

Bad Case :
        Statement stmt = null;
        ....
        String allItemSql = "SELECT * FROM s_item, s_cat WHERE s_item.cat_no = s_cat.cat_no and s_cat.cat_no = "+ cat_no;
        stmt = con.createStatement(); // 이렇게하면 오라클이 cat_no가 틀릴때마다 다른 질의로 판단하여 효율이 떨어진다.
        resultset = stmt.executeQuery(allItemSql);

Better Case :
        PreparedStatement pstmt = null;
        ....
        String allItemSql = "SELECT * FROM s_item i, s_cat s WHERE i.cat_no = s.cat_no and s.cat_no = ?";
        pstmt = con.prepareStatement(allItemSql); // prepareStatement메서드를 사용하여 Statement를 미리 준비해 놓는다.
        pstmt.setInt(1,cat_no);
        resultset = pstmt.executeQuery();



3. 더이상 사용하지 않는 자원은 반드시 시스템에 반환하도록 한다.

Bad Case :
        con = ...
        String allItemSql = "SELECT * FROM s_item i, s_cat s WHERE i.cat_no = s.cat_no and s.cat_no = ?";
        pstmt = con.prepareStatement(allItemSql);
        resultset = pstmt.executeQuery();
        ...
        con.close(); //connection만 닫는다고 해결되는 것이 아니다.

Better Case :
        con = ...
        String allItemSql = "SELECT * FROM s_item i, s_cat s WHERE i.cat_no = s.cat_no and s.cat_no = ?";
        pstmt = con.prepareStatement(allItemSql);
        resultset = pstmt.executeQuery();
        ...
        resultset.close(); //순서대로 리소스를 반환하고
        pstmt.close(); //모든 리소스를 닫아야 한다.
        con.close(); //하지만 이것도 완벽하지 않다. 에러상황시 리소스를 조금씩 잃게된다.

Better Case :
        try{
                con = ...
                String allItemSql = "SELECT * FROM s_item i, s_cat s WHERE i.cat_no = s.cat_no and? s.cat_no = ?";
                pstmt = con.prepareStatement(allItemSql);
                resultset = pstmt.executeQuery();
                ...
        }catch(SQLException e){}
        finally{//finally를 이용해서 무조건 모든 리소스를 반환토록 한다.
                try{
                        rset.close(); // 이 부분에서 예외가 발생하면 나머지 close는 실행안되게 된다.
                        pstmt.close();// 이것도 완벽하지 않다.
                        con.close();
                }catch(SQLException e){}
        }

Better Case :
        try {
                con = ...
                String allItemSql = "SELECT * FROM s_item i, s_cat s WHERE i.cat_no = s.cat_no and? s.cat_no = ?";
                .....
        }catch(SQLException e){}
        finally {//finally를 이용해서 무조건 모든 리소스를 반환토록 한다.
                if(rset != null){
                        try { //close시에도 exception이 발생할 수 있기 때문에 각각도 예외처리를 한다.
                                rset.close();
                        }catch(SQLException e){}
                }
                if(pstmt != null){
                        try {
                                pstmt.close();
                        }catch(SQLException e){}
                }
                if(con != null){
                        try{
                                con.close();
                        }catch(SQLException e){}
                }
        }

4. Connection은 직접 생성하지 않고 Factory패턴을 이용한 Connection pool을 이용한다.

Bad Case :
        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
        con = DriverManager.getConnection("jdbc:oracle:thin:@host:1521:SID","student","student"); //실전에서는 이런방식으로는 절대 사용하지 않는다.

Better Case :
        con = ConnectionBroker.getConnection("jdbc/pool/Oracle");         // 위의 두 라인이 이 한 라인으로 처리된다. 실전에서는 이런식으로 사용한다.
                                                                                                                  //ConnectionBroker는 Connection Pool에서 Connection을 얻어오는 클래스이다.