aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes Transaction handling in Session Bean 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 "Transaction handling in Session Bean" Watch "Transaction handling in Session Bean" New topic
Author

Transaction handling in Session Bean

Seetesh Hindlekar
Ranch Hand

Joined: Feb 13, 2004
Posts: 244
Hi all,

I have modified the Session facade to look like this :

Remote Session Bean A ==== Local Session Bean B

I have methodxxx() in Remote Session Bean A calling methodyyy() in Local Session Bean B

Both methodxxx() and methodyyy() are involved in inserting data in the database.

Please explain as of how transactions are handled when
Case 1.
ejb-jar.xml has trans-attribute as Required and transaction-type as Container

Case 2.
ejb-jar.xml has trans-attribute as Required and transaction-type as Bean-Managed

Rgds,

Seetesh
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

ejb-jar.xml has trans-attribute as Required and transaction-type as Bean-Managed


How can you specify trans-attribute when the transaction type is BMT.


Groovy
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982

Case 1.
ejb-jar.xml has trans-attribute as Required and transaction-type as Container

The transaction will be handled by the container. Which means, if A does not contain any transactional context, B will create a new one, and the methodY() will be in the newly created context.

If A contains a transactional context, the container will propogate this context to the method in B, which the 2 methods will be ran in the same context. And thus, if methodY() fails, the whole transactions for both methodX() and methodY() rollback.

If you decide to commit the transaction, you dont need to do anything. The container will help u to commit.

If you decide to rollback the transaction, you just need to call context.setRollbackOnly(), and the container will ensure that the transaction will not be committed.


Case 2.
ejb-jar.xml has trans-attribute as Required and transaction-type as Bean-Managed

For BMP, you dont have to configure the transactional attribute, as the container will not do anything for you. Inside the code, you need to explicitly get the UserTransaction object, and start your transaction in methodX() by ut.begin(). This transaction context can be propogate to methodY() in B if and only if it is a stateful session bean (Note that Entity bean cannot be BMP in EJB 2.0). Then in methodY(), you need to decide whether the transaction is committed (ut.commit()) or rollback (ut.rollback()).

Hope this help.

Nick


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
Forget to mention, if it is a stateless session bean, you cannot keep the transaction open after the end of the method, you MUST either commit it, or rollback it.

Nick
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
You can use Stateful Session Bean with transaction management

1. Bean-Managed Transaction (BMT)
In a bean-managed transaction, the code in the session or message-driven bean explicitly marks the boundaries of the transaction. An entity bean cannot have bean-managed transactions; it must use container-managed transactions instead. Although beans with container-managed transactions require less coding, they have one limitation: When a method is executing, it can be associated with either a single transaction or no transaction at all. If this limitation will make coding your bean difficult, you should consider using bean-managed transactions.

BMT with session bean , you can control transaction with JDBC and JTA

JDBC


JTA


2. Container-Managed Transaction (CMT)
In an enterprise bean with container-managed transactions, the EJB container sets the boundaries of the transactions. You can use container-managed transactions with any type of enterprise bean: session, entity, or message-driven. Container-managed transactions simplify development because the enterprise bean code does not explicitly mark the transaction's boundaries. The code does not include statements that begin and end the transaction.

CMT use transaction attribute , it config in DD file.

1. Required
2. RequiresNew
3. Mandatory
4. NotSupported
5. Supports
6. Never

Synchronizing a Session Bean's Instance Variables
The SessionSynchronization interface, which is optional, allows you to synchronize the instance variables with their corresponding values in the database. The container invokes the SessionSynchronization methods--afterBegin, beforeCompletion, and afterCompletion--at each of the main stages of a transaction.

The afterBegin method informs the instance that a new transaction has begun. The container invokes afterBegin immediately before it invokes the business method. The afterBegin method is a good place to load the instance variables from the database. The BankBean class, for example, loads the checkingBalance and savingBalance variables in the afterBegin method:




SCJA,SCJP,SCWCD,SCBCD,SCEA I
Java Developer, Thailand
Seetesh Hindlekar
Ranch Hand

Joined: Feb 13, 2004
Posts: 244
Hi Pradeep,

It didnt give me any error while typing it as bean-managed in ejb-jar.xml file. I could still go ahead and do the same logic using Weblogic 7.0.

Rgds,

Seetesh
Seetesh Hindlekar
Ranch Hand

Joined: Feb 13, 2004
Posts: 244
Hi everyone,

Thanks Nicholas Cheung for ur post.

I was expecting the same results as mentioned in ur post.

I was inserting one records at a time in one table say table1 in methodyyy() and inserting another set of records in another table say table2 in methodxxx().

Methodyyy() was returning some error ie SQLException (error defined on purpose) whereas methodxxx() was able to insert records in the table successfully which was killing the entire purpose. Problem was both methodxxx() and methodyyy() should have rollbacked the transaction which never happened in my case.

Does this mean that transaction handled in both the methods were independant of each other?

Any updates or correct me if I am wrong.

Rgds,

Seetesh
Seetesh Hindlekar
Ranch Hand

Joined: Feb 13, 2004
Posts: 244
Hi somkiat puisungnoen,

The Session Bean is defined as Stateful only and not as Stateless as I am trying to handle transactions in both CMT and BMT just to evaluate which works before I implement the same in our project.

I was also trying using the DataSource object to get the Connection along with earlier tries with UserTransaction for connection.

Any ideas which one would serve my purpose

Rgds,

Seetesh
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Transaction handling in Session Bean