トランザクション機能およびShunsaku Fileを使用する場合のJava APIの使用例を示します。
ここでは、“5.1 トランザクションの概要”で説明した大学の講義の履修登録を例に、Java APIの使用例を示します。
以下にJava APIを使用したプログラミング例を示します。
import com.fujitsu.shun.ShunConnection; import com.fujitsu.shun.ShunPreparedRecordID; import com.fujitsu.shun.ShunPreparedStatement; import com.fujitsu.shun.ShunResultSet; import com.fujitsu.shun.common.ShunException; public class TranAppSample { public static void main(String[] args) { ShunConnection con = null; ShunPreparedStatement pstmt = null; ShunPreparedRecordID rid = null; ShunResultSet rs = null; try { // 受講者数を増やしたいコースを検索するための検索式、リターン式を設定 // 検索式 String sCourseQuery = "/course/name == 'Business negotiation'"; // リターン式 String sCourseReturn = "/"; //更新レコードID取得用 String updateCourseRecordID = null; //更新データ String updateCourseData = "<course>" + " <name>Business negotiation</name>" + " <instructor>" + " <first-name>Max</first-name>" + " <last-name>cameron</last-name>" + " </instructor>" + " <capacity>40</capacity>" + " <current-auditors>31</current-auditors>" + "</course>"; //受講中の情報を追加したい生徒を検索するための検索式、リターン式を設定 // 検索式 String sStudentQuery = "/student/e-mail == 'mary\\.tompson@example\\.com'"; // リターン式 String sStudentReturn = "/"; //更新レコードID取得用 String updateStudentRecordID = null; //更新データ String updateStudentData = "<student>" + " <first-name>Mary</first-name>" + " <last-name>Tompson</last-name>" + " <e-mail>mary.tompson@example.com</e-mail>" + " <course>Chinese language</course>" + " <course>Business negotiation</course>" + "</student>"; // ShunConnectionオブジェクトを作成 // 接続先のホスト名:DirSvr1、 // ポート番号:23101、 // Shunsaku File名:shunsakuFile1の場合 con = new ShunConnection("DirSvr1", 23101, "shunsakuFile1"); //更新対象のコース情報、生徒情報を検索 // 検索式を指定し、ShunPreparedStatementオブジェクトを作成 pstmt = con.prepareSearch(sCourseQuery, sCourseReturn); // 検索を実行し、ShunResultSetオブジェクトを作成 rs = pstmt.executeSearch(); // 取得したコース情報のレコードID、およびXML文書を取得 while (rs.next()) { updateCourseRecordID = rs.getRecordID(); } rs.close(); pstmt.close(); // 検索式を指定し、ShunPreparedStatementオブジェクトを作成 pstmt = con.prepareSearch(sStudentQuery, sStudentReturn); // 検索を実行し、ShunResultSetオブジェクトを作成 rs = pstmt.executeSearch(); // 取得した生徒情報のレコードID、およびXML文書を取得 while (rs.next()) { updateStudentRecordID = rs.getRecordID(); } rs.close(); pstmt.close(); // 自動コミットを無効に設定 con.setAutoCommit(false); // コース情報の更新 rid = con.prepareUpdateRecordID(); // 更新データを追加 rid.add(updateCourseRecordID, updateCourseData); // データ設定に成功している場合、データを更新 if (0 < rid.getCount()) { rid.updateByRecordID(); System.out.println("コース情報の更新終了"); } rid.close(); // 生徒情報の更新 rid = con.prepareUpdateRecordID(); // 更新データを追加 rid.add(updateStudentRecordID, updateStudentData); // データ設定に成功している場合、データを更新 if (0 < rid.getCount()) { rid.updateByRecordID(); System.out.println("生徒情報の更新終了"); } rid.close(); // ここまでの更新をコミット con.commit(); con.close(); } // アプリケーション実行時にエラーが発生した場合の処理 catch (ShunException ex) { //ロールバックを実施 if (con != null) { try { con.rollback(); } catch (ShunException e) { int errorLevel = ex.getErrLevel(); switch( errorLevel ) { case ShunException.SHUN_ERROR : System.out.println("エラーレベル:SHUN_ERROR"); break; case ShunException.SHUN_ERROR_TRANSACTION_ROLLEDBACK : System.out.println("エラーレベル:SHUN_ERROR_TRANSACTION_ROLLEDBACK"); break; case ShunException.SHUN_ERROR_CONNECTION_TERMINATED : System.out.println("エラーレベル:SHUN_ERROR_CONNECTION_TERMINATED"); break; } System.out.println("エラーメッセージ:" + ex.getMessage()); ex.printStackTrace(); } } int errorLevel = ex.getErrLevel(); switch( errorLevel ) { case ShunException.SHUN_ERROR : System.out.println("エラーレベル :SHUN_ERROR"); break; case ShunException.SHUN_ERROR_TRANSACTION_ROLLEDBACK : System.out.println("エラーレベル :SHUN_ERROR_TRANSACTION_ROLLEDBACK"); break; case ShunException.SHUN_ERROR_CONNECTION_TERMINATED : System.out.println("エラーレベル :SHUN_ERROR_CONNECTION_TERMINATED"); break; } System.out.println("エラーメッセージ:" + ex.getMessage()); ex.printStackTrace(); } catch (Exception ex) { System.out.println("エラーメッセージ:" + ex.getMessage()); ex.printStackTrace(); } finally { try { if (rs != null) rs.close(); } catch (ShunException ex) { int errorLevel = ex.getErrLevel(); switch( errorLevel ) { case ShunException.SHUN_ERROR : System.out.println("エラーレベル :SHUN_ERROR"); break; case ShunException.SHUN_ERROR_TRANSACTION_ROLLEDBACK : System.out.println("エラーレベル :SHUN_ERROR_TRANSACTION_ROLLEDBACK"); break; case ShunException.SHUN_ERROR_CONNECTION_TERMINATED : System.out.println("エラーレベル :SHUN_ERROR_CONNECTION_TERMINATED"); break; } System.out.println("エラーメッセージ:" + ex.getMessage()); ex.printStackTrace(); } try { if (pstmt != null) pstmt.close(); } catch (ShunException ex) { int errorLevel = ex.getErrLevel(); switch( errorLevel ) { case ShunException.SHUN_ERROR : System.out.println("エラーレベル :SHUN_ERROR"); break; case ShunException.SHUN_ERROR_TRANSACTION_ROLLEDBACK : System.out.println("エラーレベル :SHUN_ERROR_TRANSACTION_ROLLEDBACK"); break; case ShunException.SHUN_ERROR_CONNECTION_TERMINATED : System.out.println("エラーレベル :SHUN_ERROR_CONNECTION_TERMINATED"); break; } System.out.println("エラーメッセージ:" + ex.getMessage()); ex.printStackTrace(); } try { if (rid != null) rid.close(); } catch (ShunException ex) { int errorLevel = ex.getErrLevel(); switch( errorLevel ) { case ShunException.SHUN_ERROR : System.out.println("エラーレベル :SHUN_ERROR"); break; case ShunException.SHUN_ERROR_TRANSACTION_ROLLEDBACK : System.out.println("エラーレベル :SHUN_ERROR_TRANSACTION_ROLLEDBACK"); break; case ShunException.SHUN_ERROR_CONNECTION_TERMINATED : System.out.println("エラーレベル :SHUN_ERROR_CONNECTION_TERMINATED"); break; } System.out.println("エラーメッセージ:" + ex.getMessage()); ex.printStackTrace(); } try { if (con != null) con.close(); } catch (ShunException ex) { int errorLevel = ex.getErrLevel(); switch( errorLevel ) { case ShunException.SHUN_ERROR : System.out.println("エラーレベル :SHUN_ERROR"); break; case ShunException.SHUN_ERROR_TRANSACTION_ROLLEDBACK : System.out.println("エラーレベル :SHUN_ERROR_TRANSACTION_ROLLEDBACK"); break; case ShunException.SHUN_ERROR_CONNECTION_TERMINATED : System.out.println("エラーレベル :SHUN_ERROR_CONNECTION_TERMINATED"); break; } System.out.println("エラーメッセージ:" + ex.getMessage()); ex.printStackTrace(); } } } } |
実行結果
コース情報の更新終了 生徒情報の更新終了 |