Transactions in java

Chapter 9 - Transactions

    Transactions in java

  •           The Transaction object manages connection to database and Entity caches.It changes through one View Object are visible to other View Objects as long as these View Objects all parented by the one root Application Module.

              If two View Objects are parented by two separate root Application Modules, then changes made through the View Object will not be seen by the second View Object until the changes are committed to database.

    Methods of the Transaction:

                   •  commit - Commit the transaction; saves all changes to the database. If the database connection is established, the transaction is implicitly started.

                   •  connect - Attempt to establish a connection to the given database URL.

                   •  disconnect - Disconnect the server from the database.

                   •  getLockingMode - Get the preferred locking mode for this transaction. Currently the locking mode defaults to LOCK_PESSIMISTIC.

                   •  rollback - Rollback the transaction; discard all changes.

                   •  setLockingMode - Set the preferred locking mode for this transaction. Changing the locking mode affects only subsequent locks that are placed.


    try {
      if (updateAttr(vo, newAttrVal)) {
        // Commit changes to the database, making
        // updated data available to other Application Modules.
        System.out.println("\n Transaction committed. \n");
      else {
        System.out.println("\n Transaction rolled back. \n");
    } catch (Exception e) {
    Transaction Contexts and Nested Application Modules

              The top-level Application Module defines a transaction context for groups of related queries and supports combining data changes from independent forms into the same transaction.

              By sharing an Application Module, forms can participate in the same transaction, and they can pass View Objects from the shared Application Module.

              An Entity Object represents a database object. It caches data and provides mapping to an underlying database object. The transaction manages the cache and synchronizes data with one database.

              Data is cached at the Entity Object level and all View Object references within the same transaction share the Entity Objects.

    package amdemo;
    import oracle.jbo.*;
    import java.util.Hashtable;
    import javax.naming.*;
    public class TestAm {
      public static void main(String[] args) {
        final String amName1 = "am1.AppMod1";
        final String amName2 = "am2.AppMod2";
        final String voName1 = "am1.DeptView1";
        final String voName2 = "am1.DeptView2";
        final String voName3 = "am2.DeptView3";
        final String voName4 = "am2.DeptView4";
        final String connStr = "jdbc:oracle:thin:scott/tiger@jtora815:1521:ORCL";
        // Set environment for local deployment.
        Hashtable env = new Hashtable(2);
        env.put(JboContext.DEPLOY_PLATFORM, JboContext.PLATFORM_LOCAL);
        ApplicationModule appMod1 = null;
        ApplicationModule appMod2 = null;
        try {
          javax.naming.Context ic = new InitialContext(env);
          ApplicationModuleHome home1 =
          appMod1 = home1.create();
          ApplicationModuleHome home2 =
          appMod2 = home2.create();
        } catch(Exception e){
        ViewObject vo1 = appMod1.createViewObject("vo1", voName1);
        ViewObject vo2 = appMod1.createViewObject("vo2", voName2);
        ViewObject vo3 = appMod2.createViewObject("vo3", voName3);
        //ViewObject vo4 = appMod2.createViewObject("vo4", voName4);
        Row r1 = vo1.first();
        r1.setAttribute("Loc", "asdf");
        System.out.println("vo1 before AppMod1 post " + r1.getAttribute("Loc"));
        Row r2 = vo2.first();
        System.out.println("vo2 before AppMod1 post " + r2.getAttribute("Loc"));
        Row r3 = vo3.first();
        System.out.println("vo3 before AppMod1 post " + r3.getAttribute("Loc"));
        //Row r4 = vo4.first();
        //System.out.println("vo4 before AppMod1 post " + r4.getAttribute("Loc"));
        System.out.println("vo1 after AppMod1 post " + r1.getAttribute("Loc"));
        r2 = vo2.first();
        System.out.println("vo2 after AppMod1 post " + r2.getAttribute("Loc"));
        r3 = vo3.first();
        System.out.println("vo3 after AppMod1 post " + r3.getAttribute("Loc"));
        //r4 = vo4.first();
        //System.out.println("vo4 after AppMod1 post " + r4.getAttribute("Loc"));
      try {
        System.out.println("Commit succeeded.");
      } catch (oracle.jbo.JboException e) {
        System.out.println("Commit failed. " + e);
        System.out.println("vo1 after AppMod1 commit " + r1.getAttribute("Loc"));
        r2 = vo2.first();
        System.out.println("vo2 after AppMod1 commit " + r2.getAttribute("Loc"));
        r3 = vo3.first();
        System.out.println("vo3 after AppMod1 commit " + r3.getAttribute("Loc"));
        //r4 = vo4.first();
        //System.out.println("vo4 after AppMod1 commit " + r4.getAttribute("Loc"));
        // Keep the console window open so you can see what happened.
        System.out.println("\n Press Enter to close this window.");
        try {; } catch (Exception e) {}

© 2015 by Learncertification All Rights Reserved. The certification names are the trademarks of their respective owners. Terms & Privacy Policy