Scope of a transaction

Discussions

EJB design: Scope of a transaction

  1. Scope of a transaction (2 messages)

    How do we mark the beginning and ending of a transaction in declarative transaction management?
    Is it in the form of a method,the transaction starts at the
    beginning of the method and ends at the completion of the method.
    If the transaction is executed in the form of a method,how do I mark the transaction when I want the method(transaction) to be executed twice as a single transaction?

    Ex:I have a method for the purchase of an airline ticket which is executed as a transaction,this is fine as long as you want to buy a single ticket,but when you want to buy two tickets(one ticket will not do),the same method has to executed twice as a single transaction.In this case how do we mark the beginning and ending of a transaction declaratively?

    Threaded Messages (2)

  2. Scope of a transaction[ Go to top ]

    You can get the transaction demarcation interface by calling EJBObject.getUserTransaction(). From there you can programmatically manage your txn by making method calls to the UserTransaction object returned. You will also have to set up your deployment descriptor appropriately.
  3. Scope of a transaction[ Go to top ]

    You have two real options here

    1) Make the bean and the method whichs buys the tickets in a stateless session bean called say TicketAgent and the method as buyATicket(). Mark this as tx required. Place another session bean before it called PurchaseAgent, and have it simply call the buyATicket() on TicketAgent twice. Now mark this one as required as well. In this scenario PurchaseAgent creates the inital tx, calls the same method on TicketAgent twice within its own single method and both purchases live in the same tx.

    PurchaseAgent (TX_REQUIRED)
        --- buyTwoTickets(), tx starts here
            --- lookup the TicketAgent(TX_REQUIRED)
            --- Call buyATicket();
            --- Call buyATicket();
        --- end of buyTwoTickets(), tx ends here


    2) Make the one bean a stateful bean and manage the tx using the UserTransaction.

    I would strongly reccomend solution A as it creates a more re-useable design.

    Dave Wolf
    Internet Applications Division