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:
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){}
}
}
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을 얻어오는 클래스이다.
댓글 0
번호 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|
43 | JDF(Java Development Framework) 관련 | 황제낙엽 | 2004.02.11 | 505 |
42 | JDBC Date 포맷 변환 | 황제낙엽 | 2003.11.08 | 938 |
» | Connection 리소스 관리시 주의해야 할 점 | 황제낙엽 | 2003.09.06 | 429 |
40 | XSLTFilter ... (퍼옴) | 황제낙엽 | 2003.07.21 | 634 |
39 | (초보탈출 II) 내부클래스 | 황제낙엽 | 2003.05.20 | 505 |
38 | (초보탈출 I) 패키지 | 황제낙엽 | 2003.05.20 | 544 |
37 | 열네번째날 (화상 채팅 프로그램 프로젝트) | 황제낙엽 | 2003.04.21 | 945 |
36 | 열세번째날 - WEB APPLICATION | 황제낙엽 | 2003.04.15 | 824 |
35 | 열두번째날 - I/O | 황제낙엽 | 2003.04.15 | 285 |
34 | 열하나째날 - 스레드 | 황제낙엽 | 2003.04.15 | 330 |
33 | 열번째날 - 애플릿과 그래픽 | 황제낙엽 | 2003.04.15 | 617 |
32 | 아홉째날 - SWING | 황제낙엽 | 2003.04.15 | 542 |
31 | 여덟째날 - 예외처리 | 황제낙엽 | 2003.04.14 | 527 |
30 | 일곱째날 - EVENT | 황제낙엽 | 2003.04.14 | 245 |
29 | 여섯째날 - AWT | 황제낙엽 | 2003.04.14 | 628 |
28 | 다섯째날 - Collection, String, StringBuffer | 황제낙엽 | 2003.04.14 | 562 |
27 | 넷째날 - 추상클래스, 내부클래스, Interface, Static영역 | 황제낙엽 | 2003.04.14 | 566 |
26 | 셋째날 - 패키지, 상속, 접근제어 | 황제낙엽 | 2003.04.14 | 509 |
25 | 둘째날 - 클래스, 메서드, 연산자 | 황제낙엽 | 2003.04.14 | 482 |
24 | 첫째날 - 자바의 특징과 DATA TYPE | 황제낙엽 | 2003.04.14 | 457 |