Refactoring JDBC programming 2008.10.12 박찬욱
<ul><li>Hand-made JdbcTemplate </li></ul><ul><ul><li>1.1  문제 인식 공유 </li></ul></ul><ul><ul><li>1.2  해결 방안 모색 </li></ul></ul...
오늘의 목표는 ? <ul><li>Composition + interface-based  프로그래밍 기법 </li></ul><ul><li>Spring JdbcTemplate  이해하기 </li></ul><ul><li>어디...
1.  Hand-made JdbcTemplate
1.1  문제 인식 공유 <ul><li>퀴즈 </li></ul><ul><ul><li>iBatis </li></ul></ul><ul><ul><li>ORM(Hibernate or TopLink  등 ) </li></ul><...
1.1  문제 인식 공유 <ul><li>JDBC  근본적인 문제 </li></ul><ul><ul><li>TCFTC </li></ul></ul><ul><ul><ul><li>T ry- C atch- F inally- T r...
1.1  문제 인식 공유 <ul><li>전혀  OO  스럽지 않은 반복적인 코드 (boilerplate code) 의 사용으로  Data Access  코드가 드러워져 간다 </li></ul>“ Bad Java code...
1.2  해결 방안 모색 Strategy pattern 을  도입해서  문제 해결  시도
1.2  해결 방안 모색 <ul><li>Strategy pattern  이란 ? </li></ul><ul><ul><li>실행 시점에 (at runtime)  알고리즘을 선택할 수 있는 방법 </li></ul></ul>
1.2  해결 방안 모색 <ul><li>왜  Strategy pattern 을 택했나 ? </li></ul><ul><li>Programming to  Interface  instead of Concrete Class <...
2.1 Best Practice of JDBC Strategy <ul><li>Strategy pattern </li></ul>참조 : wiki[2]
1.3 Refactoring with Strategy pattern <ul><li>변경되는 부분과 그렇지 않은 부분 식별하기 ,  각각 </li></ul><ul><ul><li>Updating sql  실행 </li></...
1.3 Refactoring with Strategy pattern <ul><li>try  { </li></ul><ul><ul><li>// get DB connection </li></ul></ul><ul><ul><li...
1.3 Refactoring with Strategy pattern <ul><li>try  { </li></ul><ul><ul><li>// get DB connection </li></ul></ul><ul><ul><li...
1.3 Refactoring with Strategy pattern <ul><li>try  { </li></ul><ul><ul><li>// get DB connection </li></ul></ul><ul><ul><li...
1.3 Refactoring with Strategy pattern <ul><li>try  { </li></ul><ul><ul><li>// get DB connection </li></ul></ul><ul><ul><li...
1.3 Refactoring with Strategy pattern <ul><li>Strategy pattern 이 적용된  Template.update()  </li></ul>
1.3 Refactoring with Strategy pattern <ul><li>변경되는 부분과 그렇지 않은 부분 식별하기 ,  각각 </li></ul><ul><ul><li>querying sql  실행 </li></...
1.3 Refactoring with Strategy pattern <ul><li>try  { </li></ul><ul><ul><li>// get DB connection </li></ul></ul><ul><ul><li...
1.3 Refactoring with Strategy pattern <ul><li>try  { </li></ul><ul><ul><li>// get DB connection </li></ul></ul><ul><ul><li...
1.3 Refactoring with Strategy pattern <ul><li>try  { </li></ul><ul><ul><li>// get DB connection </li></ul></ul><ul><ul><li...
1.3 Refactoring with Strategy pattern <ul><li>try  { </li></ul><ul><ul><li>// get DB connection </li></ul></ul><ul><ul><li...
1.3 Refactoring with Strategy pattern <ul><li>Strategy pattern 이 적용된  Template.query()  </li></ul>
1.3 Refactoring with Strategy pattern <ul><li>Refactoring 1. </li></ul><ul><ul><li>결과 값의 타입은 ? </li></ul></ul><ul><ul><li>...
1.3 Refactoring with Strategy pattern <ul><li>return  (Lecture)  template .query( </li></ul><ul><li>&quot;select * from LE...
1.3 Refactoring with Strategy pattern <ul><li>return  (Lecture)  template .query( </li></ul><ul><li>&quot;select * from LE...
1.3 Refactoring with Strategy pattern <ul><li>return  (Lecture)  template .query( </li></ul><ul><li>&quot;select * from LE...
1.3 Refactoring with Strategy pattern <ul><li>Refactoring 2. </li></ul><ul><ul><li>여전히  Template 의  query() 와  update() 에서...
1.3 Refactoring with Strategy pattern <ul><li>Refactoring 2.  결과 </li></ul>
1.3 Refactoring with Strategy pattern <ul><li>콜백 인터페이스가 적용된 공통 메소드 </li></ul>
1.4 Summary <ul><li>구현한 템플릿 클래스와 인터페이스 </li></ul>
1.4 Summary <ul><li>JdbcTemplate  도입된 이후의 효과 </li></ul><ul><ul><li>JDBC workflow 의 흐름 진행 주체 </li></ul></ul><ul><ul><ul><li...
2. SPRING JDBCTEMPLATE
2.1 Best Practice of JDBC Strategy <ul><li>Spring JDBC core package’s Central class </li></ul><ul><li>Jdbc UseCase Best Pr...
2.1 Best Practice of JDBC Strategy <ul><li>JdbcTemplate with Strategy pattern </li></ul>DAO Template Callback Interface im...
2.1 Best Practice of JDBC Strategy Jdbc-based DAO Spring-based DAO DriverManager / DataSource DataSource Statement / Prepa...
2.1 Best Practice of JDBC Strategy Task Spring You Connection(DataSource) management Provide SQL Statement management Para...
2.1 Best Practice of JDBC Strategy <ul><li>Convenience , but  powerful  Jdbc Template </li></ul><ul><ul><li>Resource manag...
2.1 Best Practice of JDBC Strategy <ul><li>Convenience , but  powerful  JdbcTemplate </li></ul><ul><ul><li>Consistent exce...
2.1 Best Practice of JDBC Strategy <ul><li>Convenience , but  powerful  JdbcTemplate </li></ul><ul><ul><li>Logging for SQL...
2.1 Best Practice of JDBC Strategy <ul><li>JdbcTemplate  구성 방법 </li></ul><ul><ul><li>DataSource(or Connection Pool) 가 쓰레드 ...
2.2 SIMPLIFY JDBC OPERATION
2.2 Simplify JDBC operation <ul><li>SimpleJdbc*  활용하기 </li></ul><ul><ul><li>SimpleJdbcTemplate </li></ul></ul><ul><ul><ul>...
2.2 Simplify JDBC operation <ul><li>SimpleJdbc*  활용하기 </li></ul><ul><ul><li>SimpleJdbcInsert </li></ul></ul><ul><ul><ul><l...
2.2 Simplify JDBC operation <ul><li>SimpleJdbc*  활용하기 </li></ul><ul><ul><li>SimpleJdbcCall </li></ul></ul><ul><ul><ul><li>...
2.2 Simplify JDBC operation <ul><li>The  Pareto Principle  in action </li></ul><ul><ul><li>JdbcTemplate+callback interface...
2.2 Simplify JDBC operation <ul><li>RowMapper(with ResultSetExtractor) </li></ul><ul><ul><li>per-row basis </li></ul></ul>...
2.2 Simplify JDBC operation <ul><li>SqlQuery </li></ul><ul><ul><li>by Inheritance </li></ul></ul><ul><ul><li>Reusable, thr...
2.2 Simplify JDBC operation <ul><li>RowCallbackHandler </li></ul><ul><ul><li>Stateful </li></ul></ul><ul><ul><li>public vo...
2.2 Simplify JDBC operation <ul><li>DataFieldMaxValueIncrementer  활용하기 </li></ul><ul><ul><li>Sequence-based </li></ul></ul...
Reference <ul><li>wiki[1]:  http://en.wikipedia.org/wiki/Image:Strategy_Pattern_Diagram_ZP.svg </li></ul><ul><li>wiki[2]: ...
감사합니다 .
Upcoming SlideShare
Loading in …5
×

Refactoring Jdbc Programming

3,384 views

Published on

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,384
On SlideShare
0
From Embeds
0
Number of Embeds
1,343
Actions
Shares
0
Downloads
68
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Refactoring Jdbc Programming

  1. 1. Refactoring JDBC programming 2008.10.12 박찬욱
  2. 2. <ul><li>Hand-made JdbcTemplate </li></ul><ul><ul><li>1.1 문제 인식 공유 </li></ul></ul><ul><ul><li>1.2 해결 방안 모색 </li></ul></ul><ul><ul><li>1.3 Refactoring with Strategy pattern </li></ul></ul><ul><li>Spring JdbcTemplate </li></ul><ul><ul><li>2.1 Best Practice of JDBC Strategy </li></ul></ul><ul><ul><li>2.2 Simplify JDBC operation </li></ul></ul>Table of Contents
  3. 3. 오늘의 목표는 ? <ul><li>Composition + interface-based 프로그래밍 기법 </li></ul><ul><li>Spring JdbcTemplate 이해하기 </li></ul><ul><li>어디 쓸 만한 곳이 없을까 ? </li></ul>
  4. 4. 1. Hand-made JdbcTemplate
  5. 5. 1.1 문제 인식 공유 <ul><li>퀴즈 </li></ul><ul><ul><li>iBatis </li></ul></ul><ul><ul><li>ORM(Hibernate or TopLink 등 ) </li></ul></ul><ul><ul><li>내부 추상화 프레임웍 </li></ul></ul><ul><ul><li>쌩 (pure) JDBC~? </li></ul></ul>
  6. 6. 1.1 문제 인식 공유 <ul><li>JDBC 근본적인 문제 </li></ul><ul><ul><li>TCFTC </li></ul></ul><ul><ul><ul><li>T ry- C atch- F inally- T ry- C atch </li></ul></ul></ul><ul><ul><ul><li>최고의 boilerplate 코드 </li></ul></ul></ul><ul><ul><li>Connection / Resource 누수 문제 </li></ul></ul><ul><ul><li>SQLException </li></ul></ul><ul><ul><ul><li>데이터베이스 벤더 별로 정의된 error code, error state 정보 </li></ul></ul></ul>Connection con = null; Statement stmt = null; try { con = dataSource.getConnection(); stmt = con.createStatement(); stmt.executeUpdate(“UPDATE TABLE_NAME SET...”); }c atch(SQLException e){ // 예외 처리 ... }finally { if (stmt != null) { try { stmt.close(); } catch (SQLException ex) {} } if (con != null) try { con.close(); } catch (SQLException ex) { } }
  7. 7. 1.1 문제 인식 공유 <ul><li>전혀 OO 스럽지 않은 반복적인 코드 (boilerplate code) 의 사용으로 Data Access 코드가 드러워져 간다 </li></ul>“ Bad Java code is bad J2EE code.” - Rod Johnson
  8. 8. 1.2 해결 방안 모색 Strategy pattern 을 도입해서 문제 해결 시도
  9. 9. 1.2 해결 방안 모색 <ul><li>Strategy pattern 이란 ? </li></ul><ul><ul><li>실행 시점에 (at runtime) 알고리즘을 선택할 수 있는 방법 </li></ul></ul>
  10. 10. 1.2 해결 방안 모색 <ul><li>왜 Strategy pattern 을 택했나 ? </li></ul><ul><li>Programming to Interface instead of Concrete Class </li></ul><ul><li>(Achieving Loose Coupling with Interface) </li></ul><ul><li>- Favor object composition over class inheritance </li></ul><ul><li>(Prefer Object Composition to Concrete Inheritance) </li></ul>
  11. 11. 2.1 Best Practice of JDBC Strategy <ul><li>Strategy pattern </li></ul>참조 : wiki[2]
  12. 12. 1.3 Refactoring with Strategy pattern <ul><li>변경되는 부분과 그렇지 않은 부분 식별하기 , 각각 </li></ul><ul><ul><li>Updating sql 실행 </li></ul></ul><ul><li>try { </li></ul><ul><ul><li>// get DB connection </li></ul></ul><ul><ul><li>con = getConnection(); </li></ul></ul><ul><ul><li>// create Statement </li></ul></ul><ul><ul><li>stmt = con.prepareStatement( </li></ul></ul><ul><ul><li>&quot;update LECTURE SET name=? where ID=?“ </li></ul></ul><ul><ul><li>); </li></ul></ul><ul><ul><li>// binding sql parameter </li></ul></ul><ul><ul><li>stmt.setString(1, name); </li></ul></ul><ul><ul><li>//... </li></ul></ul><ul><ul><li>// execute Query </li></ul></ul><ul><ul><li>result = stmt.executeUpdate(); </li></ul></ul><ul><li>} catch (SQLException exception) { </li></ul><ul><ul><li>// 예외처리 </li></ul></ul><ul><li>} finally { </li></ul><ul><ul><li>if (stmt != null ) { </li></ul></ul><ul><ul><li>try { stmt.close();} catch (SQLException e){} </li></ul></ul><ul><ul><li>} // end if </li></ul></ul><ul><ul><li>if (con != null ) { </li></ul></ul><ul><ul><li>try { con.close();} catch (SQLException e){} </li></ul></ul><ul><ul><li>} // end if </li></ul></ul><ul><li>} // end try-catch-finally </li></ul><ul><li>return result; </li></ul><ul><li>try { </li></ul><ul><ul><li>// get DB connection </li></ul></ul><ul><ul><li>con = getConnection(); </li></ul></ul><ul><ul><li>// create Statement </li></ul></ul><ul><ul><li>stmt = con.prepareStatement( </li></ul></ul><ul><ul><li>&quot;insert into LECTURE values(?, ?, ?, ?, ?)“ </li></ul></ul><ul><ul><li>); </li></ul></ul><ul><ul><li>// binding sql parameters </li></ul></ul><ul><ul><li>stmt.setInt(1, incrementer .nextIntValue()); </li></ul></ul><ul><ul><li>// ... </li></ul></ul><ul><ul><li>// execute Query </li></ul></ul><ul><ul><li>result = stmt.executeUpdate(); </li></ul></ul><ul><li>} catch (SQLException exception) { </li></ul><ul><ul><li>// 예외처리 </li></ul></ul><ul><li>} finally { </li></ul><ul><ul><li>if (stmt != null ) { </li></ul></ul><ul><ul><li>try { stmt.close();} catch (SQLException e){} </li></ul></ul><ul><ul><li>} // end if </li></ul></ul><ul><ul><li>if (con != null ) { </li></ul></ul><ul><ul><li>try { con.close();} catch (SQLException e){} </li></ul></ul><ul><ul><li>} // end if </li></ul></ul><ul><li>} // end try-catch-finally </li></ul><ul><li>return result; </li></ul>update() insert()
  13. 13. 1.3 Refactoring with Strategy pattern <ul><li>try { </li></ul><ul><ul><li>// get DB connection </li></ul></ul><ul><ul><li>con = getConnection(); </li></ul></ul><ul><ul><li>// create Statement </li></ul></ul><ul><ul><li>stmt = con.prepareStatement( </li></ul></ul><ul><ul><li>&quot;update LECTURE SET name=? where ID=?“ </li></ul></ul><ul><ul><li>); </li></ul></ul><ul><ul><li>// binding sql parameter </li></ul></ul><ul><ul><li>stmt.setString(1, name); </li></ul></ul><ul><ul><li>//... </li></ul></ul><ul><ul><li>// execute Query </li></ul></ul><ul><ul><li>result = stmt.executeUpdate(); </li></ul></ul><ul><li>} catch (SQLException exception) { </li></ul><ul><ul><li>// 예외처리 </li></ul></ul><ul><li>} finally { </li></ul><ul><ul><li>if (stmt != null ) { </li></ul></ul><ul><ul><li>try { stmt.close();} catch (SQLException e){} </li></ul></ul><ul><ul><li>} // end if </li></ul></ul><ul><ul><li>if (con != null ) { </li></ul></ul><ul><ul><li>try { con.close();} catch (SQLException e){} </li></ul></ul><ul><ul><li>} // end if </li></ul></ul><ul><li>} // end try-catch-finally </li></ul><ul><li>return result; </li></ul>메소드 인자로 빼냄 콜백 메소드로 구현 공통 메소드로 추출 update() insert()
  14. 14. 1.3 Refactoring with Strategy pattern <ul><li>try { </li></ul><ul><ul><li>// get DB connection </li></ul></ul><ul><ul><li>con = getConnection(); </li></ul></ul><ul><ul><li>// create Statement </li></ul></ul><ul><ul><li>stmt = con.prepareStatement( </li></ul></ul><ul><ul><li>&quot;update LECTURE SET name=? where ID=?“ </li></ul></ul><ul><ul><li>); </li></ul></ul><ul><ul><li>// binding sql parameter </li></ul></ul><ul><ul><li>stmt.setString(1, name); </li></ul></ul><ul><ul><li>//... </li></ul></ul><ul><ul><li>// execute Query </li></ul></ul><ul><ul><li>result = stmt.executeUpdate(); </li></ul></ul><ul><li>} catch (SQLException exception) { </li></ul><ul><ul><li>// 예외처리 </li></ul></ul><ul><li>} finally { </li></ul><ul><ul><li>if (stmt != null ) { </li></ul></ul><ul><ul><li>try { stmt.close();} catch (SQLException e){} </li></ul></ul><ul><ul><li>} // end if </li></ul></ul><ul><ul><li>if (con != null ) { </li></ul></ul><ul><ul><li>try { con.close();} catch (SQLException e){} </li></ul></ul><ul><ul><li>} // end if </li></ul></ul><ul><li>} // end try-catch-finally </li></ul><ul><li>return result; </li></ul>update() insert()
  15. 15. 1.3 Refactoring with Strategy pattern <ul><li>try { </li></ul><ul><ul><li>// get DB connection </li></ul></ul><ul><ul><li>con = getConnection(); </li></ul></ul><ul><ul><li>// create Statement </li></ul></ul><ul><ul><li>stmt = con.prepareStatement( </li></ul></ul><ul><ul><li>&quot;update LECTURE SET name=? where ID=?“ </li></ul></ul><ul><ul><li>); </li></ul></ul><ul><ul><li>// binding sql parameter </li></ul></ul><ul><ul><li>stmt.setString(1, name); </li></ul></ul><ul><ul><li>//... </li></ul></ul><ul><ul><li>// execute Query </li></ul></ul><ul><ul><li>result = stmt.executeUpdate(); </li></ul></ul><ul><li>} catch (SQLException exception) { </li></ul><ul><ul><li>// 예외처리 </li></ul></ul><ul><li>} finally { </li></ul><ul><ul><li>if (stmt != null ) { </li></ul></ul><ul><ul><li>try { stmt.close();} catch (SQLException e){} </li></ul></ul><ul><ul><li>} // end if </li></ul></ul><ul><ul><li>if (con != null ) { </li></ul></ul><ul><ul><li>try { con.close();} catch (SQLException e){} </li></ul></ul><ul><ul><li>} // end if </li></ul></ul><ul><li>} // end try-catch-finally </li></ul><ul><li>return result; </li></ul>update()
  16. 16. 1.3 Refactoring with Strategy pattern <ul><li>try { </li></ul><ul><ul><li>// get DB connection </li></ul></ul><ul><ul><li>con = getConnection(); </li></ul></ul><ul><ul><li>// create Statement </li></ul></ul><ul><ul><li>stmt = con.prepareStatement( </li></ul></ul><ul><ul><li>&quot; insert into LECTURE values(?, ?, ?, ?, ?)“ </li></ul></ul><ul><ul><li>); </li></ul></ul><ul><ul><li>// binding sql parameter </li></ul></ul><ul><ul><li>stmt.setString(1, name); </li></ul></ul><ul><ul><li>//... </li></ul></ul><ul><ul><li>// execute Query </li></ul></ul><ul><ul><li>result = stmt.executeUpdate(); </li></ul></ul><ul><li>} catch (SQLException exception) { </li></ul><ul><ul><li>// 예외처리 </li></ul></ul><ul><li>} finally { </li></ul><ul><ul><li>if (stmt != null ) { </li></ul></ul><ul><ul><li>try { stmt.close();} catch (SQLException e){} </li></ul></ul><ul><ul><li>} // end if </li></ul></ul><ul><ul><li>if (con != null ) { </li></ul></ul><ul><ul><li>try { con.close();} catch (SQLException e){} </li></ul></ul><ul><ul><li>} // end if </li></ul></ul><ul><li>} // end try-catch-finally </li></ul><ul><li>return result; </li></ul>insert()
  17. 17. 1.3 Refactoring with Strategy pattern <ul><li>Strategy pattern 이 적용된 Template.update() </li></ul>
  18. 18. 1.3 Refactoring with Strategy pattern <ul><li>변경되는 부분과 그렇지 않은 부분 식별하기 , 각각 </li></ul><ul><ul><li>querying sql 실행 </li></ul></ul><ul><li>try { </li></ul><ul><ul><li>// get DB connection </li></ul></ul><ul><ul><li>con = getConnection(); </li></ul></ul><ul><ul><li>// create Statement </li></ul></ul><ul><ul><li>rs = con.prepareStatement( </li></ul></ul><ul><ul><li>“ select * from LECTURE where ID=?“ </li></ul></ul><ul><ul><li>); </li></ul></ul><ul><ul><li>// binding sql parameter </li></ul></ul><ul><ul><li>stmt.setInt(1, id); </li></ul></ul><ul><ul><li>// execute Query </li></ul></ul><ul><ul><li>rs = stmt.executeQuery(); </li></ul></ul><ul><ul><li>//extract result </li></ul></ul><ul><ul><li>while (rs.next()) { </li></ul></ul><ul><ul><ul><li>result = new Lecture(); </li></ul></ul></ul><ul><ul><ul><li>Result.setName(rs.getString(2)); </li></ul></ul></ul><ul><ul><ul><li>// extracting... </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} catch (SQLException exception) { </li></ul><ul><ul><li>// 예외처리 </li></ul></ul><ul><li>} finally { </li></ul><ul><ul><li>// 자원 반환 처리 </li></ul></ul><ul><li>} </li></ul><ul><li>return result; </li></ul><ul><li>try { </li></ul><ul><ul><li>// get DB connection </li></ul></ul><ul><ul><li>con = getConnection(); </li></ul></ul><ul><ul><li>// create Statement </li></ul></ul><ul><ul><li>rs = con.prepareStatement( </li></ul></ul><ul><ul><li>“ select * from LECTURE“ </li></ul></ul><ul><ul><li>); </li></ul></ul><ul><ul><li>// binding sql parameter </li></ul></ul><ul><ul><li>// execute Query </li></ul></ul><ul><ul><li>rs = stmt.executeQuery(); </li></ul></ul><ul><ul><li>//extract result </li></ul></ul><ul><ul><li>while (rs.next()) { </li></ul></ul><ul><ul><ul><li>lecture = new Lecture(); </li></ul></ul></ul><ul><ul><ul><li>Result.setName(rs.getString(2)); </li></ul></ul></ul><ul><ul><ul><li>// extracting... </li></ul></ul></ul><ul><ul><li>result.add(lecture); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} catch (SQLException exception) { </li></ul><ul><ul><li>// 예외처리 </li></ul></ul><ul><li>} finally { </li></ul><ul><ul><li>// 자원 반환 처리 </li></ul></ul><ul><li>} </li></ul><ul><li>return result; </li></ul>get() getall()
  19. 19. 1.3 Refactoring with Strategy pattern <ul><li>try { </li></ul><ul><ul><li>// get DB connection </li></ul></ul><ul><ul><li>con = getConnection(); </li></ul></ul><ul><ul><li>// create Statement </li></ul></ul><ul><ul><li>rs = con.prepareStatement( </li></ul></ul><ul><ul><li>“ select * from LECTURE where ID=?“ </li></ul></ul><ul><ul><li>); </li></ul></ul><ul><ul><li>// binding sql parameter </li></ul></ul><ul><ul><li>stmt.setInt(1, id); </li></ul></ul><ul><ul><li>// execute Query </li></ul></ul><ul><ul><li>rs = stmt.executeQuery(); </li></ul></ul><ul><ul><li>//extract result </li></ul></ul><ul><ul><li>while (rs.next()) { </li></ul></ul><ul><ul><ul><li>result = new Lecture(); </li></ul></ul></ul><ul><ul><ul><li>Result.setName(rs.getString(2)); </li></ul></ul></ul><ul><ul><ul><li>// extracting... </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} catch (SQLException exception) { </li></ul><ul><ul><li>// 예외처리 </li></ul></ul><ul><li>} finally { </li></ul><ul><ul><li>// 자원 반환 처리 </li></ul></ul><ul><li>} </li></ul><ul><li>return result; </li></ul>get() 메소드 인자로 빼냄 콜백 메소드로 구현 공통 메소드로 추출 getall()
  20. 20. 1.3 Refactoring with Strategy pattern <ul><li>try { </li></ul><ul><ul><li>// get DB connection </li></ul></ul><ul><ul><li>con = getConnection(); </li></ul></ul><ul><ul><li>// create Statement </li></ul></ul><ul><ul><li>rs = con.prepareStatement( </li></ul></ul><ul><ul><li>“ select * from LECTURE where ID=?“ </li></ul></ul><ul><ul><li>); </li></ul></ul><ul><ul><li>// binding sql parameter </li></ul></ul><ul><ul><li>stmt.setInt(1, id); </li></ul></ul><ul><ul><li>// execute Query </li></ul></ul><ul><ul><li>rs = stmt.executeQuery(); </li></ul></ul><ul><ul><li>//extract result </li></ul></ul><ul><ul><li>while (rs.next()) { </li></ul></ul><ul><ul><ul><li>result = new Lecture(); </li></ul></ul></ul><ul><ul><ul><li>Result.setName(rs.getString(2)); </li></ul></ul></ul><ul><ul><ul><li>// extracting... </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} catch (SQLException exception) { </li></ul><ul><ul><li>// 예외처리 </li></ul></ul><ul><li>} finally { </li></ul><ul><ul><li>// 자원 반환 처리 </li></ul></ul><ul><li>} </li></ul><ul><li>return result; </li></ul>get() getall()
  21. 21. 1.3 Refactoring with Strategy pattern <ul><li>try { </li></ul><ul><ul><li>// get DB connection </li></ul></ul><ul><ul><li>con = getConnection(); </li></ul></ul><ul><ul><li>// create Statement </li></ul></ul><ul><ul><li>rs = con.prepareStatement( </li></ul></ul><ul><ul><li>“ select * from LECTURE where ID=?“ </li></ul></ul><ul><ul><li>); </li></ul></ul><ul><ul><li>// binding sql parameter </li></ul></ul><ul><ul><li>stmt.setInt(1, id); </li></ul></ul><ul><ul><li>// execute Query </li></ul></ul><ul><ul><li>rs = stmt.executeQuery(); </li></ul></ul><ul><ul><li>//extract result </li></ul></ul><ul><ul><li>while (rs.next()) { </li></ul></ul><ul><ul><ul><li>result = new Lecture(); </li></ul></ul></ul><ul><ul><ul><li>Result.setName(rs.getString(2)); </li></ul></ul></ul><ul><ul><ul><li>// extracting... </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} catch (SQLException exception) { </li></ul><ul><ul><li>// 예외처리 </li></ul></ul><ul><li>} finally { </li></ul><ul><ul><li>// 자원 반환 처리 </li></ul></ul><ul><li>} </li></ul><ul><li>return result; </li></ul>getall()
  22. 22. 1.3 Refactoring with Strategy pattern <ul><li>try { </li></ul><ul><ul><li>// get DB connection </li></ul></ul><ul><ul><li>con = getConnection(); </li></ul></ul><ul><ul><li>// create Statement </li></ul></ul><ul><ul><li>rs = con.prepareStatement( </li></ul></ul><ul><ul><li>“ select * from LECTURE where ID=?“ </li></ul></ul><ul><ul><li>); </li></ul></ul><ul><ul><li>// binding sql parameter </li></ul></ul><ul><ul><li>stmt.setInt(1, id); </li></ul></ul><ul><ul><li>// execute Query </li></ul></ul><ul><ul><li>rs = stmt.executeQuery(); </li></ul></ul><ul><ul><li>//extract result </li></ul></ul><ul><ul><li>while (rs.next()) { </li></ul></ul><ul><ul><ul><li>result = new Lecture(); </li></ul></ul></ul><ul><ul><ul><li>Result.setName(rs.getString(2)); </li></ul></ul></ul><ul><ul><ul><li>// extracting... </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} catch (SQLException exception) { </li></ul><ul><ul><li>// 예외처리 </li></ul></ul><ul><li>} finally { </li></ul><ul><ul><li>// 자원 반환 처리 </li></ul></ul><ul><li>} </li></ul><ul><li>return result; </li></ul>get()
  23. 23. 1.3 Refactoring with Strategy pattern <ul><li>Strategy pattern 이 적용된 Template.query() </li></ul>
  24. 24. 1.3 Refactoring with Strategy pattern <ul><li>Refactoring 1. </li></ul><ul><ul><li>결과 값의 타입은 ? </li></ul></ul><ul><ul><li>ResultSet 처리 </li></ul></ul><ul><ul><ul><li>동일한 DTO 인 경우 , 거의 동일하게 ResultSet 에서 값을 빼내는 코드가 반복됨 </li></ul></ul></ul><ul><li>return (Lecture) template .query( </li></ul><ul><li>&quot;select * from LECTURE where ID=?&quot; , </li></ul><ul><li>... </li></ul><ul><li>}, new ResultSetExtractor() { </li></ul><ul><ul><li>public Object extractResult(ResultSet rs) </li></ul></ul><ul><ul><li>throws SQLException { </li></ul></ul><ul><ul><li>// extract result to Single Object </li></ul></ul><ul><ul><ul><li>Lecture result = new Lecture(); </li></ul></ul></ul><ul><ul><ul><li>while (rs.next()) { </li></ul></ul></ul><ul><ul><ul><ul><li>result.setId(rs.getInt(1)); </li></ul></ul></ul></ul><ul><ul><ul><ul><li>result.setName(rs.getString(2)); </li></ul></ul></ul></ul><ul><ul><ul><ul><li>result.setSpeaker(rs.getString(3)); </li></ul></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><ul><li>return result; </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>}); </li></ul><ul><li>return (List<Lecture>) template .query( </li></ul><ul><li>&quot;select * from LECTURE&quot; , </li></ul><ul><li>... </li></ul><ul><li>}, new ResultSetExtractor() { </li></ul><ul><ul><li>public Object extractResult(ResultSet rs) </li></ul></ul><ul><ul><li>throws SQLException { </li></ul></ul><ul><ul><li>// extract result to Collection Object </li></ul></ul><ul><li>List<Lecture> result = </li></ul><ul><li>new ArrayList<Lecture>(); </li></ul><ul><ul><ul><li>while (rs.next()) { </li></ul></ul></ul><ul><ul><ul><ul><li>Lecture lecture = new Lecture(); </li></ul></ul></ul></ul><ul><ul><ul><ul><li>lecture.setId(rs.getInt(1)); </li></ul></ul></ul></ul><ul><ul><ul><ul><li>lecture.setName(rs.getString(2)); </li></ul></ul></ul></ul><ul><ul><ul><ul><li>lecture.setSpeaker(rs.getString(3)); </li></ul></ul></ul></ul><ul><li> result.add(lecture); </li></ul><ul><li>} </li></ul><ul><ul><ul><li>return result; </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>}); </li></ul>get() getall()
  25. 25. 1.3 Refactoring with Strategy pattern <ul><li>return (Lecture) template .query( </li></ul><ul><li>&quot;select * from LECTURE where ID=?&quot; , </li></ul><ul><li>... </li></ul><ul><li>}, new ResultSetExtractor() { </li></ul><ul><ul><li>public Object extractResult(ResultSet rs) </li></ul></ul><ul><ul><li>throws SQLException { </li></ul></ul><ul><ul><li>// extract result to Single Object </li></ul></ul><ul><ul><ul><li>Lecture result = new Lecture(); </li></ul></ul></ul><ul><ul><ul><li>while (rs.next()) { </li></ul></ul></ul><ul><ul><ul><ul><li>result.setId(rs.getInt(1)); </li></ul></ul></ul></ul><ul><ul><ul><ul><li>result.setName(rs.getString(2)); </li></ul></ul></ul></ul><ul><ul><ul><ul><li>result.setSpeaker(rs.getString(3)); </li></ul></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><ul><li>return result; </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>}); </li></ul>get() 콜백 메소드로 구현 API 로 구분
  26. 26. 1.3 Refactoring with Strategy pattern <ul><li>return (Lecture) template .query( </li></ul><ul><li>&quot;select * from LECTURE where ID=?&quot; , </li></ul><ul><li>... </li></ul><ul><li>}, new ResultSetExtractor() { </li></ul><ul><ul><li>public Object extractResult(ResultSet rs) </li></ul></ul><ul><ul><li>throws SQLException { </li></ul></ul><ul><ul><li>// extract result to Single Object </li></ul></ul><ul><ul><ul><li>Lecture result = new Lecture(); </li></ul></ul></ul><ul><ul><ul><li>while (rs.next()) { </li></ul></ul></ul><ul><ul><ul><ul><li>result.setId(rs.getInt(1)); </li></ul></ul></ul></ul><ul><ul><ul><ul><li>result.setName(rs.getString(2)); </li></ul></ul></ul></ul><ul><ul><ul><ul><li>result.setSpeaker(rs.getString(3)); </li></ul></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><ul><li>return result; </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>}); </li></ul>get()
  27. 27. 1.3 Refactoring with Strategy pattern <ul><li>return (Lecture) template .query( </li></ul><ul><li>&quot;select * from LECTURE where ID=?&quot; , </li></ul><ul><li>... </li></ul><ul><li>}, new ResultSetExtractor() { </li></ul><ul><ul><li>public Object extractResult(ResultSet rs) </li></ul></ul><ul><ul><li>throws SQLException { </li></ul></ul><ul><ul><li>// extract result to Single Object </li></ul></ul><ul><ul><ul><li>Lecture result = new Lecture(); </li></ul></ul></ul><ul><ul><ul><li>while (rs.next()) { </li></ul></ul></ul><ul><ul><ul><ul><li>result.setId(rs.getInt(1)); </li></ul></ul></ul></ul><ul><ul><ul><ul><li>result.setName(rs.getString(2)); </li></ul></ul></ul></ul><ul><ul><ul><ul><li>result.setSpeaker(rs.getString(3)); </li></ul></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><ul><li>return result; </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>}); </li></ul>get()
  28. 28. 1.3 Refactoring with Strategy pattern <ul><li>Refactoring 2. </li></ul><ul><ul><li>여전히 Template 의 query() 와 update() 에서 JDBC API 가 중복되어 사용된다 ! </li></ul></ul>
  29. 29. 1.3 Refactoring with Strategy pattern <ul><li>Refactoring 2. 결과 </li></ul>
  30. 30. 1.3 Refactoring with Strategy pattern <ul><li>콜백 인터페이스가 적용된 공통 메소드 </li></ul>
  31. 31. 1.4 Summary <ul><li>구현한 템플릿 클래스와 인터페이스 </li></ul>
  32. 32. 1.4 Summary <ul><li>JdbcTemplate 도입된 이후의 효과 </li></ul><ul><ul><li>JDBC workflow 의 흐름 진행 주체 </li></ul></ul><ul><ul><ul><li>DAO  JdbcTemplate </li></ul></ul></ul><ul><ul><li>DAO 의 역할 충실화 </li></ul></ul><ul><ul><ul><li>SQL, 파라미터 제공 or 결과 매핑 </li></ul></ul></ul><ul><ul><ul><li>이 외의 다른 역할은 JdbcTemplate 를 비롯한 각 담당자에게 위임 </li></ul></ul></ul>
  33. 33. 2. SPRING JDBCTEMPLATE
  34. 34. 2.1 Best Practice of JDBC Strategy <ul><li>Spring JDBC core package’s Central class </li></ul><ul><li>Jdbc UseCase Best Practice </li></ul><ul><li>Collaborate with Various Callback Interface </li></ul><ul><li> Strategy pattern ! </li></ul><ul><li>Convenience DA operation </li></ul>&quot;This is a special case of the Strategy design pattern . It appears different because the interface involved are so simple &quot;
  35. 35. 2.1 Best Practice of JDBC Strategy <ul><li>JdbcTemplate with Strategy pattern </li></ul>DAO Template Callback Interface implementation 참조 : wiki[2]
  36. 36. 2.1 Best Practice of JDBC Strategy Jdbc-based DAO Spring-based DAO DriverManager / DataSource DataSource Statement / PreparedStatement / CallableStatement JdbcTemplate / Callback interface ResultSet POJO / POJO’s collection
  37. 37. 2.1 Best Practice of JDBC Strategy Task Spring You Connection(DataSource) management Provide SQL Statement management Parameter Declaration Provide parameter value ResultSet management Row Data Retrieval Transaction management Exception handling
  38. 38. 2.1 Best Practice of JDBC Strategy <ul><li>Convenience , but powerful Jdbc Template </li></ul><ul><ul><li>Resource management </li></ul></ul><ul><ul><ul><li>DataSourceUtils </li></ul></ul></ul><ul><ul><li>Integrated with Transaction management </li></ul></ul><ul><ul><ul><li>Spring-tx (non-invasive) </li></ul></ul></ul>
  39. 39. 2.1 Best Practice of JDBC Strategy <ul><li>Convenience , but powerful JdbcTemplate </li></ul><ul><ul><li>Consistent exception management </li></ul></ul><ul><ul><ul><li>예외 발생 시 처리 기준 </li></ul></ul></ul><ul><ul><ul><ul><li>에러 코드 (error code), 에러 상태 (error state) </li></ul></ul></ul></ul><ul><ul><ul><ul><li> 예외 종류 (type of Exception) </li></ul></ul></ul></ul><ul><ul><ul><li>Check exception  Unchecked exception </li></ul></ul></ul><ul><ul><ul><li>SQLExceptionTranslator </li></ul></ul></ul>
  40. 40. 2.1 Best Practice of JDBC Strategy <ul><li>Convenience , but powerful JdbcTemplate </li></ul><ul><ul><li>Logging for SQL inform. (DEBUG level) </li></ul></ul><ul><ul><li>Various Template </li></ul></ul><ul><ul><ul><li>JdbcTemplate </li></ul></ul></ul><ul><ul><ul><li>NamedParameterJdbcTemplate </li></ul></ul></ul><ul><ul><ul><li>SimpleJdbcTemplate </li></ul></ul></ul><ul><ul><li>Convenience DA operation </li></ul></ul><ul><ul><ul><li>named parameter </li></ul></ul></ul><ul><ul><ul><li>Auto-detect column by Jdbc Driver </li></ul></ul></ul><ul><ul><ul><li>Easily using Batch, LOB </li></ul></ul></ul>
  41. 41. 2.1 Best Practice of JDBC Strategy <ul><li>JdbcTemplate 구성 방법 </li></ul><ul><ul><li>DataSource(or Connection Pool) 가 쓰레드 안전하다면 , JdbcTemplate 도 쓰레드 안전 </li></ul></ul><ul><li>private JdbcTemplate template ; </li></ul><ul><li>public void setTemplate(DataSource dataSource) { </li></ul><ul><ul><li>this . template = new JdbcTemplate(dataSource); </li></ul></ul><ul><li>} </li></ul><ul><li>< bean id = &quot;lectureDao&quot; class = &quot;org.springframework.lecture.jdbc.dao.JdbcLectureDao&quot; > </li></ul><ul><ul><li>< property name = &quot;template&quot; ref = &quot;dataSource&quot; /> </li></ul></ul><ul><li></ bean > </li></ul><ul><li>private JdbcTemplate template ; </li></ul><ul><li>public void setTemplate(JdbcTemplate template) { </li></ul><ul><ul><li>this . template = template; </li></ul></ul><ul><li>} </li></ul><ul><li>< bean id = &quot;lectureDao&quot; </li></ul><ul><li>class = &quot;org.springframework.lecture.jdbc.dao.JdbcLectureDao&quot; > </li></ul><ul><ul><li>< property name = &quot;template&quot; ref = &quot;jdbcTemplate&quot; /> </li></ul></ul><ul><li></ bean > </li></ul>
  42. 42. 2.2 SIMPLIFY JDBC OPERATION
  43. 43. 2.2 Simplify JDBC operation <ul><li>SimpleJdbc* 활용하기 </li></ul><ul><ul><li>SimpleJdbcTemplate </li></ul></ul><ul><ul><ul><li>Wrapper around classic JdbcTemplate class(getJdbcOperations()) </li></ul></ul></ul><ul><ul><ul><li>Java-5-based convenience wrapper for the classic Spring JdbcTemplate </li></ul></ul></ul><ul><ul><ul><li>Varargs, Generic, Autoboxing, Unboxing... </li></ul></ul></ul>
  44. 44. 2.2 Simplify JDBC operation <ul><li>SimpleJdbc* 활용하기 </li></ul><ul><ul><li>SimpleJdbcInsert </li></ul></ul><ul><ul><ul><li>Simplify Insert behavior </li></ul></ul></ul><ul><ul><ul><li>JdbcTemplate + DatabaseMetaData </li></ul></ul></ul><ul><ul><ul><li>‘ fluid’ interface style </li></ul></ul></ul>
  45. 45. 2.2 Simplify JDBC operation <ul><li>SimpleJdbc* 활용하기 </li></ul><ul><ul><li>SimpleJdbcCall </li></ul></ul><ul><ul><ul><li>multi-threaded, reusable object representing a call to stored procedure or a stored function </li></ul></ul></ul><ul><ul><li>SimpleJdbcTestUtils </li></ul></ul>
  46. 46. 2.2 Simplify JDBC operation <ul><li>The Pareto Principle in action </li></ul><ul><ul><li>JdbcTemplate+callback interface by Reflection = 80 </li></ul></ul><ul><ul><li>SqlQuery + inheritance by explicit parameter mapping =20 </li></ul></ul>
  47. 47. 2.2 Simplify JDBC operation <ul><li>RowMapper(with ResultSetExtractor) </li></ul><ul><ul><li>per-row basis </li></ul></ul><ul><ul><li>Stateless & reusable </li></ul></ul><ul><ul><li>Ideal choice of row-mapping logic </li></ul></ul><ul><li>ResultSetExtractor </li></ul><ul><ul><li>per-resultSet basis </li></ul></ul><ul><ul><li>Stateless & reusable, if not access stateful resource </li></ul></ul>
  48. 48. 2.2 Simplify JDBC operation <ul><li>SqlQuery </li></ul><ul><ul><li>by Inheritance </li></ul></ul><ul><ul><li>Reusable, threadsafe class </li></ul></ul><ul><ul><li>Encapsulate SQL </li></ul></ul><ul><ul><li>MappingSqlQuery & UpdatableSqlQuery </li></ul></ul><ul><ul><li>Using meaningful method name </li></ul></ul>
  49. 49. 2.2 Simplify JDBC operation <ul><li>RowCallbackHandler </li></ul><ul><ul><li>Stateful </li></ul></ul><ul><ul><li>public void processRow(ResultSet rs) throws SQLException </li></ul></ul>
  50. 50. 2.2 Simplify JDBC operation <ul><li>DataFieldMaxValueIncrementer 활용하기 </li></ul><ul><ul><li>Sequence-based </li></ul></ul><ul><ul><ul><li>Oracle, PostgreSQL, DB2(plain, mainframe), HSQL, H2 </li></ul></ul></ul><ul><ul><li>Column-based </li></ul></ul><ul><ul><ul><li>MySQL, MS-SqlServer, Sybase, Hsql, Derby </li></ul></ul></ul><ul><li>BeanProperty* 활용하기 </li></ul><ul><ul><li>(Parameterized)BeanPropertyRowMapper </li></ul></ul><ul><ul><li>BeanPropertySqlParameterSource </li></ul></ul>
  51. 51. Reference <ul><li>wiki[1]: http://en.wikipedia.org/wiki/Image:Strategy_Pattern_Diagram_ZP.svg </li></ul><ul><li>wiki[2]: http://en.wikipedia.org/wiki/Image:Strategy_pattern_in_LePUS3.gif </li></ul><ul><li>Tomas[1]: JDBC Development with the Spring Framework </li></ul><ul><li>Spring reference </li></ul><ul><li>Spring API </li></ul><ul><li>J2EE Design and Development </li></ul><ul><li>J2EE without EJB </li></ul>
  52. 52. 감사합니다 .

×