Transactions

Discussions

EJB programming & troubleshooting: Transactions

  1. Transactions (6 messages)

    Can somebody please tell me how multiple components use the same Transaction. That is if a Session Facade starts a transaction and calls a method of a Stateless Session bean, then how does that Sateless Session bean acquire the transaction that the Session Facade started???

    How could I have a plain java object that is called by a Session Facade use the transaction that the Session Facade started??????

    Can I do the same thing without EJBs at all???

    Threaded Messages (6)

  2. re:Transactions[ Go to top ]

    Cameron,

    It's all in the Connection objects. When an EJB asks the AppServer for a Connection Object it will NOT get a 'normal JDBC connection', but a JDBC connection WRAPPED inside some AppServer code. This AppServer object
    looks like a normal Connection object so you do not see this as a developer.

    The AppServer has an administration of which EJBs and connection objects belong to which transaction. When an EJB does a 'commit' or 'rollback' the AppServer uses this administration to call the commit/rollback methods on all the involved connections.

    So the plain java object you use operates on all these 'wrapped' connections and this is the link between your code and the AppServer.

    If an EJB calls another EJB, the remote method invocation contains the current transaction information. This makes it possible for the AppServer to 'connect' the administrations of the two beans together.

    Hope this clears things up,
    Victor Smit
  3. re:Transactions[ Go to top ]

    Victor,

    Your description of how transactions are implemented inside the App server is not accurate. The App server uses specialized XA interfaces for resource connections, it doesn't just wrap up the connection and use normal commit/rollback operations. Additionally, remote calls may carry remote transaction information in the form of a CORBA transaction service, which allows other transactional participants that are unaware of the App server's internal management of connections to participate in the transaction. This is not allways performed in server-internal remote calls to improve performance.

    Gal
  4. re:Transactions[ Go to top ]

    http://www-106.ibm.com/developerworks/java/library/j-jtp0410/
  5. re:Transactions[ Go to top ]

    After rereading Chapter 10 from the Mastering EJB II book, I see how POJO can access the transactions started by an EJB.

    I wanted my own mini-transaction service and was wondering how you could assoicate a transaction with a thread. After emailing Clinton Begin who pointed me in the right direction (ala telling me about ThreadLocal) and looking at the JBoss transaction manager source code, I now know how to do it.

    After I write the transaction manager, I'll post it on this site.
  6. re:Transactions[ Go to top ]

    Unfortunately that hasn't helped. Say I have one EJB bean (let's call it BeanA) that starts a transaction then calls a method on another EJB bean (let's call it BeanB) then how does BeanB use the transaction that BeanA started??? I had a quick look at JBoss source code and it appears as if the transaction is associated with a message path (ie. A thread of a request).

    If I have BeanA call a POJO (plain old java object) called ObjectA then how can ObjectA access the transaction that BeanA started?

    Thirdly, how would I go about having an POJO start a transaction and call other POJOs that need to use the same transaction?? Eg. What is a simple way of associating transactions to a thread?? I do not need distrubited transactions; just using the one database.
  7. re:Transactions[ Go to top ]

    You are correct transactions are associated with threads. The same is true with db connections accessed within transactions.

    My question is what you mean by access the transaction? If you want to get the db connection associated with the transaction then simply create a JNDI context, find your datasource and then get a connection. The container will give you the correct connection for that transaction.

    Otherwise what do you mean by access?

    A POJO can start a transaction by simply

    Context ctx = null;
    Hashtable env = new Hashtable();
    ctx = new InitialContext();
    UserTransaction tx = (UserTransaction)
    ctx.lookup("javax.transaction.UserTransaction");
    tx.begin();

    David