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

9.4.1 Java APIの記述例

1つのトランザクションを扱うJava APIの場合の記述例について、以下に示します。

/*
 * 以下のように用意されていることを仮定しています。
 *  sPerson :追加したい従業員の情報
 *  sQuery :検索式を格納した文字列
 *  sReturn :リターン式を格納した文字列
 *  updateData :更新データを格納した文字列
 */
ShunConnection con = null;
ShunPreparedStatement pstmt_insert = null;
ShunPreparedStatement pstmt_search = null;
ShunPreparedRecordID rid = null;
ShunResultSet rs = null;
try
{
// ShunConnectionオブジェクトを作成
    // 接続先のホスト名:DirSvr1
    // ポート番号:23101
    // Shunsaku File名: fileA
    con = new ShunConnection("DirSvr1", 23101, "fileA");
    // 自動コミットを無効に設定
    con.setAutoCommit(false);
    // データ追加
    pstmt_insert = con.prepareInsert();
    pstmt_insert.add(sPerson);
    pstmt_insert.executeInsert();
    pstmt_insert.close();
    
    //接続先のShunsaku FileをfileDに切替え
    con.setShunsakuFileName("fileD");
    
    // 更新用のデータを検索
    pstmt_search = con.prepareSearch(sQuery, sReturn);
    rs = pstmt_search.executeSearch();
    //レコードIDを取得
	String updateDataRecordID = null;
    while(rs.next()) {
        updateDataRecordID = rs.getRecordID();
    }
    pstmt_search.close();
    rs.close();
    
    //データ更新
    rid = con.prepareUpdateRecordID();
    rid.add(updateDataRecordID, updateData);
    rid.updateByRecordID();
    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();
        if (pstmt_insert != null)pstmt_insert.close();
        if (pstmt_search != null)pstmt_search.close();
        if (rid != null)rid.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_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();
    }
}