Interstage Shunsaku Data Manager アプリケーション開発ガイド - Microsoft(R) Windows(R) 2000/ Microsoft(R) Windows Server(TM) 2003 - - UNIX共通 - |
目次
索引
![]() ![]() |
第1部 アプリケーション開発の基本 | > 第5章 トランザクション機能 |
Java APIを利用したアプリケーションの作成例を説明します。
XML文書の削除および更新を1トランザクションとして扱う例を以下に示します。
/* * 以下のように用意されていることを仮定しています。 * sQuery :検索式を格納した文字列 * sReturn :リターン式を格納した文字列 * updateData :更新データを格納した文字列 */ ShunConnection con = null; ShunPreparedStatement searchStmt = null; ShunResultSet rs = null; ShunPreparedRecordID deleteRecId = null; ShunPreparedRecordID updateRecId = null; try { // ShunConnectionオブジェクトを作成 con = new ShunConnection("DirSvr1", 33101); // ShunPreparedStatementオブジェクトを作成 searchStmt = con.prepareSearch(sQuery, sReturn); // 返信要求件数を設定 searchStmt.setRequest(1, 30); // 検索を実行し、ShunResultSetオブジェクトを作成 rs = searchStmt.executeSearch(); // レコードIDを取得 String[] idList = new String[rs.getReturnCount()]; int count = 0; while (rs.next()) { idList[count] = rs.getRecordID(); count++; } rs.close(); searchStmt.close(); // 自動コミットを無効に切替え con.setAutoCommit(false); (1) // 削除用のShunPreparedRecordIDオブジェクトを作成 deleteRecId = con.prepareDeleteRecordID(); // 3番目のデータを削除対象に設定 deleteRecId.add(idList[2]); // データを削除 if (0 < deleteRecId.getCount()) { deleteRecId.deleteByRecordID(); System.out.println("削除終了"); } deleteRecId.close(); // 更新用のShunPreparedRecordIDオブジェクトを作成 updateRecId = con.prepareUpdateRecordID(); // 5番目のデータを更新対象に設定 updateRecId.add(idList[4], updateData); // データを更新 if (0 < updateRecId.getCount()) { updateRecId.updateByRecordID(); System.out.println("更新終了"); } updateRecId.close(); // コミットの実行 con.commit(); (2) con.close(); } catch (ShunException ex) { try { // ロールバックの実行 if (!con.getAutoCommit()) { con.rollback(); (3) } } catch (ShunException e) { int errorLevel = ex.getErrLevel(); switch (errorLevel) { case ShunException.SHUN_ERROR: System.out.println("エラーレベル :SHUN_ERROR"); break; case ShunException.SHUN_ERROR_CONNECTION_TERMINATED: System.out.println("エラーレベル :SHUN_ERROR_CONNECTION_TERMINATED"); break; case ShunException.SHUN_ERROR_TRANSACTION_ROLLEDBACK: System.out.println("エラーレベル :SHUN_ERROR_TRANSACTION_ROLLEDBACK"); break; } System.out.println("エラーメッセージ:" + ex.getMessage()); e.printStackTrace(); } // エラー情報取得 int errorLevel = ex.getErrLevel(); switch (errorLevel) { case ShunException.SHUN_ERROR: System.out.println("エラーレベル :SHUN_ERROR"); break; case ShunException.SHUN_ERROR_CONNECTION_TERMINATED: System.out.println("エラーレベル :SHUN_ERROR_CONNECTION_TERMINATED"); break; case ShunException.SHUN_ERROR_TRANSACTION_ROLLEDBACK: System.out.println("エラーレベル :SHUN_ERROR_TRANSACTION_ROLLEDBACK"); break; } System.out.println("エラーメッセージ:" + ex.getMessage()); ex.printStackTrace(); } // エラーが起きた場合の回収処理 finally { try { if (rs != null)rs.close(); if (searchStmt != null)searchStmt.close(); if (deleteRecId != null)deleteRecId.close(); if (updateRecId != null)updateRecId.close(); 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_CONNECTION_TERMINATED: System.out.println("エラーレベル :SHUN_ERROR_CONNECTION_TERMINATED"); break; case ShunException.SHUN_ERROR_TRANSACTION_ROLLEDBACK: System.out.println("エラーレベル :SHUN_ERROR_TRANSACTION_ROLLEDBACK"); break; } System.out.println("エラーメッセージ:" + ex.getMessage()); ex.printStackTrace(); } } |
ShunConnectionクラスのsetAutoCommitメソッドで、自動コミットを無効に切り替えます。
コネクションの確立時は、自動コミットが有効になっていますので、切替えが必要です。
再度自動コミットを有効にしたい場合は、setAutoCommitメソッドにtrueを設定します。
トランザクションを終了し、それまでの操作をShunsakuに反映したい場合は、commitメソッドを実行します。
エラーが発生したときなど、それまでの操作をShunsakuに反映しないでトランザクションをロールバックする場合は、rollbackメソッドを実行します。
Java APIの詳細については、“Java APIリファレンス”を参照してください。
目次
索引
![]() ![]() |