Contenu connexe Similaire à Spring3.1概要 データアクセスとトランザクション処理 (20) Spring3.1概要 データアクセスとトランザクション処理7. データアクセスの処理が混在した業務ロジック (2/3) 業務ロジック 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 // 振込先の口座の残高を計算 ps.clearParameters(); ps.setString(1, to.getAccountNumnber()); rs = ps.executeQuery(); if (rs.next() == true) { zandaka = rs.getInt("zandaka"); } else { throw new BussinessException(" データがありません "); } newZandaka = zandaka + furikomigaku; to.setZandaka(newZandaka); // 振込元の残高を更新 ps = con.prepareStatement( "update account set zandaka=? where account_num=?"); ps.setInt(1, from.getZandaka()); ps.setString(2, from.getAccountNumnber()); ps.execute(); // 振込先の残高を更新 ps.clearParameters(); ps.setInt(1, to.getZandaka()); ps.setString(2, to.getAccountNumnber()); ps.execute(); con.commit(); 8. データアクセスの処理が混在した業務ロジック (3/3) 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 } catch (SQLException sqle) { try { con.rollback(); } catch (Exception e) { System.out.println(" システムエラー "+e); } int errorCode = sqle.getErrorCode(); if (errorCode == ERR_DEADLOCK) { throw new BussinessException(" デッドロック発生 ", sqle); } else { throw new SystemException(" システムエラー ", sqle); } } finally { try { con.close(); } catch (Exception e) { System.out.println(" システムエラー "+e); } } } 17. JDBC の問題 (3/3) ・例外 SQLException の解釈が必要 原因を特定するためのエラーコードが DB 製品毎に異なる updateZandaka メソッド 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 public void updateZandaka(Account account) { Connection con = null; PreparedStatement ps = null; ResultSet rs = null; try { con = dataSource.getConnection(); con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE); ps = con.prepareStatement("update account set zandaka=? where account_num=?"); ps.setInt(1, account.getZandaka()); ps.setString(2, account.getAccountNumnber()); ps.execute(); con.commit(); } catch (SQLException sqle) { try { con.rollback(); } catch (Exception e) { System.out.println(" システムエラー " + e); } int errorCode = sqle.getErrorCode(); if (errorCode == ERR_DEADLOCK) { throw new BussinessException(" デッドロック発生 ", sqle); } else { throw new SystemException(" システムエラー ", sqle); } } finally { try { con.close(); } catch (Exception e) { System.out.println(" システムエラー " + e); } } } 21. Template クラスの Bean の準備 ・・・ <bean class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource" /> </bean> <bean class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/> <property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:sample"/> <property name="user" value="spring3"/> <property name="password" value="spring3"/> </bean> ・・・ ・・・ @Repository public class AccountDaoSpringJdbc implements AccountDao { @Autowired private JdbcTemplate jdbcTemplate; @Autowired private NamedParameterJdbcTemplate npJdbcTemplate; ・・・ 25. ≪ 補足≫匿名クラスを使わない書き方 class MyRowMapper implements RowMapper<Pet> { public Pet mapRow(ResultSet rs, int rowNum) throws SQLException { Pet p = new Pet(); p.setPetId(rs.getInt("pet_id")); p.setPetName(rs.getString("pet_name")); p.setOwnerName(rs.getString("owner_name")); return p; } } Pet pet = jdbcTemplate.queryForObject( "select * from pet where pet_id=?" ,new MyRowMapper() ,id); 30. ≪ 補足≫メソッドチェーンを使わないやり方 MapSqlParameterSource map = new MapSqlParameterSource(); map.addValue("pet_id", pet.getPetId()); map.addValue("pet_name", pet.getPetName()); map.addValue("owner_name", pet.getOwnerName()); npJdbcTemplate.update( "insert into pet (pet_id, pet_name, owner_name)" + " values (:pet_id, :pet_name, :owner_name)" ,map ); 54. トランザクションマネージャの実装 ≪ Interface≫ PlatformTransactionManager DataSourceTransactionManager HibernateTransactionManager JtaTransactionManager JpaTransactionManager JdoTransactionManager データアクセス技術に合わせて 適切なトランザクションマネージャを 選択する Notes de l'éditeur というところで、時間も迫ってまいりましたので、 これで、発表を終わります。 ご清聴ありがとうございました。