ページの先頭行へ戻る
Big Data Integration ServerV1.4.0 検索編アプリケーション開発ガイド
FUJITSU Software

5.2 Java APIでトランザクションを使う

Java APIを利用したアプリケーションの作成例を説明します。

XML文書の削除および更新を1トランザクションとして扱う例を以下に示します。


図5.2 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", 23101);

    // 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(); } }

(1)自動コミットを無効に切替え

ShunConnectionクラスのsetAutoCommitメソッドで、自動コミットを無効に切り替えます。


ポイント

コネクションの確立時は、自動コミットが有効になっていますので、切替えが必要です。
再度自動コミットを有効にしたい場合は、setAutoCommitメソッドにtrueを設定します。

(2)コミットの実行

トランザクションを終了し、それまでの操作をShunsakuに反映したい場合は、commitメソッドを実行します。


(3)ロールバックの実行

エラーが発生したときなど、それまでの操作をShunsakuに反映しないでトランザクションをロールバックする場合は、rollbackメソッドを実行します。


参照

Java APIの詳細については、“Java APIリファレンス”を参照してください。