Interstage Shunsaku Data Manager Application Development Guide - Microsoft(R) Windows(R) 2000/ Microsoft(R) Windows Server(TM) 2003 - - UNIX -
Contents Index PreviousNext

Part 1 The Basic for Developing Application> Chapter 7 Shunsaku File> 7.4 Operating Multiple Shunsaku Files in One Transaction

7.4.1 Sample Code for Java API

The following code is the sample code for operating multiple Shunsaku Files in one transaction using Java API.

/*
 * This program assumes that the following variables have been declared
 *  sPerson : The information of employees to be added
 *  sQuery :Character string containing search expression
 *  sReturn :Character string containing return expression
 *  updateData :Character string containing update data
 */
ShunConnection con = null;
ShunPreparedStatement pstmt_insert = null;
ShunPreparedStatement pstmt_search = null;
ShunPreparedRecordID rid = null;
ShunResultSet rs = null;
try
{
// Create ShunConnection object
    // Host name in the connection destination: DirSvr1
    // Port number: 33101
    // Shunsaku File name: fileA
    con = new ShunConnection("DirSvr1", 33101, "fileA");
    // Turn off auto-commit mode
    con.setAutoCommit(false);
    // Add data
    pstmt_insert = con.prepareInsert();
    pstmt_insert.add(sPerson);
    pstmt_insert.executeInsert();
    pstmt_insert.close();
    
    // Switch Shunsaku File in the connection destination to fileD
    con.setShunsakuFileName("fileD");
    
    // Search the update data
    pstmt_search = con.prepareSearch(sQuery, sReturn);
    rs = pstmt_search.executeSearch();
    // Obtain record ID
String updateDataRecordID = null;
    while(rs.next()) {
        updateDataRecordID = rs.getRecordID();
    }
    pstmt_search.close();
    rs.close();
    
    // Update data
    rid = con.prepareUpdateRecordID();
    rid.add(updateDataRecordID, updateData);
    rid.updateByRecordID();
    rid.close();
    
    // Commit the updates made so far
    con.commit();
    con.close();
    
}
// Processing to perform if the error occurs while executing an application
catch (ShunException ex)
{
    //Execute rollback
    if (con != null)
    {
        try
        {
            con.rollback();
        }
        catch (ShunException e)
        {
            int errorLevel = ex.getErrLevel();
            switch( errorLevel ) {
              case ShunException.SHUN_ERROR :
                System.out.println("Error level:SHUN_ERROR");
                break;
              case ShunException.SHUN_ERROR_TRANSACTION_ROLLEDBACK :
                System.out.println("Error level:SHUN_ERROR_TRANSACTION_ROLLEDBACK");
                break;
              case ShunException.SHUN_ERROR_CONNECTION_TERMINATED :
                System.out.println("Error level:SHUN_ERROR_CONNECTION_TERMINATED");
                break;
            }
            System.out.println("Error message:" + ex.getMessage());
            ex.printStackTrace();
        }
    }
    int errorLevel = ex.getErrLevel();
    switch( errorLevel ) {
      case ShunException.SHUN_ERROR :
        System.out.println("Error level  :SHUN_ERROR");
        break;
      case ShunException.SHUN_ERROR_TRANSACTION_ROLLEDBACK :
        System.out.println("Error level  :SHUN_ERROR_TRANSACTION_ROLLEDBACK");
        break;
      case ShunException.SHUN_ERROR_CONNECTION_TERMINATED :
        System.out.println("Error level  :SHUN_ERROR_CONNECTION_TERMINATED");
        break;
    }
    System.out.println("Error message:" + ex.getMessage());
    ex.printStackTrace();
}
catch (Exception ex)
{
    System.out.println("Error message:" + 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("Error level  :SHUN_ERROR");
            break;
          case ShunException.SHUN_ERROR_TRANSACTION_ROLLEDBACK :
            System.out.println("Error level  :SHUN_ERROR_TRANSACTION_ROLLEDBACK");
            break;
          case ShunException.SHUN_ERROR_CONNECTION_TERMINATED :
            System.out.println("Error level  :SHUN_ERROR_CONNECTION_TERMINATED");
            break;
        }
        System.out.println("Error message:" + ex.getMessage());
        ex.printStackTrace();
    }
}

Contents Index PreviousNext

All Rights Reserved, Copyright(C) FUJITSU LIMITED 2006