Discussions

EJB programming & troubleshooting: to control Transaction-Boundary in EJB

  1. to control Transaction-Boundary in EJB (8 messages)

    I have no idea how to control Transaction-Boundary in EJB, can any people make suggestions.

    i made a session-bean to trasfer account,
    in this session-bean called some entitybean ,
    but i'm afraid the instance of session bean is not unique,
    so there can be some simultaneous access to the session-bean , maybe error.
    the code is show blow:


      /**
       * transfer money from one account to another
       * return value:
       * 0: success
       * ^0: failed, error code
       *
       * error code:
       * 1: account not exists
       * 2: not enought money in source account
       */

      public int transAccount(String accountFrom, String accountTo, double transSum ){

        AcaccounttbClient cFrom = new AcaccounttbClient();
        AcaccounttbClient cTo = new AcaccounttbClient();
        Acaccounttb acTbFrom = cFrom.findByPrimaryKey(accountFrom);
        Acaccounttb acTbTo = cTo.findByPrimaryKey(accountTo);

        if ( acTbFrom == null || acTbTo == null ){
          return 1;
        }

        Double restSum = cFrom.getAccountsum();

        if ( restSum.doubleValue() < transSum ){
          return 2;
        }

        cFrom.setAccountsum(new Double(restSum.doubleValue()-transSum));

        cTo.setAccountsum(new Double(cTo.getAccountsum().doubleValue()+transSum));

        return 0;
        
      }

    (the code is well-formated, but looks urgly cause of the web page, u can copy it to another editor to see)

    can this code cause trouble ?


    so I guess if we can control the transactoin-boundary ,
    (where the transaction begin and where the transaction end)
    everything will be ok.

    thanks
  2. You can set method's transaction atttribute to control the transation-boundary. For your example, you can set the transaction atttriubte of method transAccount(..) to tx_required.
  3. do u mean I specify "container transaction" for this bean like below :

        <container-transaction>
          <method>
    <ejb-name>AccountBean</ejb-name>
    <method-name>*</method-name>
          </method>
          <trans-attribute>Required</trans-attribute>
        </container-transaction>

    that's enough ? now all method of my session-bean can be one seperate transaction ? M i right ?
  4. NOT all method of my session-bean can be one seperate transaction, but each method of your session bean will be running in a seperate transaction.

    You should also set the referenced session bean's(or entity bean) method to tx_required. So that they can join in your session bean's transaction.
  5. &#8220;The test&#8217;s test. Let&#8217;s see &#8230;&#8221;
  6. to control Transaction-Boundary in EJB[ Go to top ]

    “The test’s test. Let’s see … Please ignore …”
  7. to control Transaction-Boundary in EJB[ Go to top ]

    &#8220;The test&#8217;s test. Let&#8217;s see &#8230; Please ignore &#8230;&#8221;
  8. to control Transaction-Boundary in EJB[ Go to top ]

    “The test’s test. Let’s see … Please ignore …”
  9. to control Transaction-Boundary in EJB[ Go to top ]

    &#8220;The test&#8217;s test. Let&#8217;s see &#8230; Please ignore &#8230;&#8221;