• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Starting a client transaction

 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I just clarify my understanding of something.

Suppose there is a client (which is outside the EJB container) that needs to call a stateless session bean method to update the DB and then get confirmation from the user that the transaction is OK. Should this be done as follows.

1. Client gets a UserTransaction and invokes the begin() method.
2. Client invokes bean method.
3. Client prompts user for confirmation (assume that the bean method returns OK)
4. If user says OK, client imvokes commit(), else invokes rollback().

Furthermore, should the bean be CMT, ie the DD has this entry:

<transaction-type>Container</transaction-type>

And with this transaction attribute:

<trans-attribute>Required</trans-attribute>

My thinking is that the client transaction will propogate into the CMT bean and that the bean's method will run in the existing transaction as it has the Required attribute.

Does this make sense?
 
Dale Seng
Ranch Hand
Posts: 275
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should ask Roger, he has loads of answers ;-)

I'm not sure I fully understand the scenario, but I'll throw something out, and you can give me more if I'm missing something.

I presume that your client is not a bean (from some other EJB container) since it's interacting with a user. To get access to UserTransaction, you'd have to have access to an EJBContext object, right? If we were a bean, then we'd get that context when we were just getting started, and we'd save it in an instance variable. But where is this client going to get the reference to an ejb context?

--Dale--
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For bean-managed transactions, a session bean obtains the UserTransaction object via the EJBContext using the getUserTransaction() method.

In other environments such as a web container, a JMS server, a thick client (which is what I was thinking of when I did my post), you obtain a UserTransaction object via JNDI. There must be a JTS-capable server to deliver the object.

Typically, the server provides the JNDI look-up name either directly or via a system or server property. For example, with WebLogic, you use JNDI to return a reference to a UserTransaction object for the WebLogic Server domain.
 
Billy Tsai
Ranch Hand
Posts: 1304
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If i wanna handle transaction programmtically in client code do I need use global transaction XA driver for the datasource in the application server?
and the code to do it is the same as BMP except BMP I get it from EJBContext? and in client side I have to look it up like getting a remote interface of a bean?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic