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:
And with this transaction 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.
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?
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.
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?
BEA 8.1 Certified Administrator, IBM Certified Solution Developer For XML 1.1 and Related Technologies, SCJP, SCWCD, SCBCD, SCDJWS, SCJD, SCEA,
Oracle Certified Master Java EE 5 Enterprise Architect