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 5 Transaction Function

5.2 Using Transactions with Java APIs

This section provides an example of creating an application that uses Java APIs.

The following example handles the deletion and updating of an XML document as a single transaction.

[Figure 5-1 Example of Handling the Deletion and Updating of an XML Document as a Single Transaction]

mark1Sample Code

/*
 * This program assumes that the following variables have been declared
 *  sQuery :Character string containing search expression
 *  sReturn :Character string containing return expression
 *  updateData :Character string containing update data
 */
ShunConnection con = null;
ShunPreparedStatement searchStmt = null;
ShunResultSet rs = null;
ShunPreparedRecordID deleteRecId = null;
ShunPreparedRecordID updateRecId = null;
try {

    // Create ShunConnection object 
    con = new ShunConnection("DirSvr1", 33101);

    // Create ShunPreparedStatement object 
    searchStmt = con.prepareSearch(sQuery, sReturn);
    // Set the number of items to return per request 
    searchStmt.setRequest(1, 30);
    // Execute search and create ShunResultSet object 
    rs = searchStmt.executeSearch();

    // Obtain record ID
    String[] idList = new String[rs.getReturnCount()];
    int count = 0;
    while (rs.next()) {
        idList[count] = rs.getRecordID();
        count++;
    }
    rs.close();
    searchStmt.close();

    // Turn off auto commit
    con.setAutoCommit(false);                                   (1)

    // Create a ShunPreparedRecordID object in order to delete records
    deleteRecId = con.prepareDeleteRecordID();
    // Target the third item of data for deletion
    deleteRecId.add(idList[2]);

    // Delete data
    if (0 < deleteRecId.getCount()) {
        deleteRecId.deleteByRecordID();
        System.out.println("Deletion complete");
    }
    deleteRecId.close();
    // Create a ShunPreparedRecordID object for updating data
    updateRecId = con.prepareUpdateRecordID();
    // Target the fifth item of data to be updated
    updateRecId.add(idList[4], updateData);

    // Update data
    if (0 < updateRecId.getCount()) {
        updateRecId.updateByRecordID();
        System.out.println("Update complete");
    }
    updateRecId.close();

    // Execute commit 
    con.commit();                                           (2)
    con.close();
} catch (ShunException ex) {
    try {
        // Execute rollback 
        if (!con.getAutoCommit()) {
            con.rollback();                                  (3)
        }
    } 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_CONNECTION_TERMINATED:
            System.out.println("Error level: SHUN_ERROR_CONNECTION_TERMINATED");
            break;
        case ShunException.SHUN_ERROR_TRANSACTION_ROLLEDBACK:
            System.out.println(" Error level: SHUN_ERROR_TRANSACTION_ROLLEDBACK");
            break;
        }
        System.out.println("Error message: " + ex.getMessage());
        e.printStackTrace();

    }
    // Obtain error information
    int errorLevel = ex.getErrLevel();
    switch (errorLevel) {
    case ShunException.SHUN_ERROR:
        System.out.println(" Error level: SHUN_ERROR");
        break;
    case ShunException.SHUN_ERROR_CONNECTION_TERMINATED:
        System.out.println(" Error level: SHUN_ERROR_CONNECTION_TERMINATED");
        break;
    case ShunException.SHUN_ERROR_TRANSACTION_ROLLEDBACK:
        System.out.println("Error level: SHUN_ERROR_TRANSACTION_ROLLEDBACK");
        break;
    }
    System.out.println("Error message: " + ex.getMessage());
    ex.printStackTrace();

}
// Recovery process in the event of an error 
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("Error level: SHUN_ERROR");
            break;
        case ShunException.SHUN_ERROR_CONNECTION_TERMINATED:
            System.out.println("Error level: SHUN_ERROR_CONNECTION_TERMINATED");
            break;
        case ShunException.SHUN_ERROR_TRANSACTION_ROLLEDBACK:
            System.out.println(" Error level: SHUN_ERROR_TRANSACTION_ROLLEDBACK");
            break;
        }
        System.out.println("Error message: " + ex.getMessage());
        ex.printStackTrace();
    }
}

mark1(1) Switching auto commit to disabled state

The setAutoCommit method of the ShunConnection class is used to disable auto commit.

Switching is necessary because auto commit is enabled when a connection is established.
Set the setAutoCommit method to 'true' to enable auto commit again.

mark1(2) Executing commit operation

The commit method is executed to terminate a transaction and update Shunsaku with the operations that have been performed.

mark1(3) Executing rollback

If an error occurs, the rollback method is executed to roll back a transaction without updating Shunsaku with the operations that have been performed.

Refer to the Java API Reference for more information on Java APIs.

Down5.2.1 Error Handling

Contents Index PreviousNext

All Rights Reserved, Copyright(C) FUJITSU LIMITED 2006