wood burning stoves 2.0
The moose likes EJB and other Java EE Technologies and the fly likes JTA query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "JTA query" Watch "JTA query" New topic

JTA query

Patrio Jensen

Joined: Feb 20, 2009
Posts: 10

I am new to JTA. I was going through JTA examples and I could see 2 ways to use JTA in my application client code for distributed transactions.

1. By getting UserTransaction from EJB container.

2. By retrieving XAResource.
I am not sure when to use which code.
Is it that in case of EJBs I need to use first approach and for rest I should use second one.

Jaikiran Pai

Joined: Jul 20, 2005
Posts: 10441

EJBs allow you to choose between Container Managed Transactions (CMT) and Bean Managed Transactions (BMT).

In case of CMT, its the container which manages the transactions (i.e. begin/commit/rollback) on the bean methods. EJBs allow you to mark individual methods on the beans to have specific transaction attributes. A bean developer can mark a bean method as having transaction attribute of REQUIRED (there are many other transaction attribtues available). When this method on the bean is invoked, the container will then create a new transaction (if there's no transaction in progress currently) or add this method to the currently running transaction. Any transaction aware resources (like datasources) which are used as part of this method will then be tracked within this transaction context by the container.

On the other hand, BMT leaves it to the bean developer to manage transactions (begin/commit/rollback). The first piece of code in your post is an example for BMT, where you are starting/commiting the transaction.

So the real question is - do you want CMT or BMT for your beans. Also, are you using EJB3 or EJB2.x. Based on the version, there are different ways to configure the transaction attributes on the bean methods.

[My Blog] [JavaRanch Journal]
Jaikiran Pai

Joined: Jul 20, 2005
Posts: 10441

Patrio JS,

Please check your private messages, by clicking on the "My Private Messages" link on top of this page, for a message from JavaRanch.
jQuery in Action, 3rd edition
subject: JTA query
It's not a secret anymore!