aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes CMP Transactions Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "CMP Transactions Doubt " Watch "CMP Transactions Doubt " New topic
Author

CMP Transactions Doubt

Amrit Kashyap
Ranch Hand

Joined: Apr 23, 2006
Posts: 44
Hi everybody,

I've some doubt regarding transactions management for J2EE.

I've one J2EE application built using EJB, Struts, DAO, DTO, Business Delegates and some other desing patterns.

All of the EJBs are "Stateless Session Bean", with following configuration:

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

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

I am making database operations using DAO pattern, in which case I opened a new connection, do the changes and then close the connection.

But in some cases, I am accessing multiple subsystem which in turn open their respective connections to database and close them subsequently. And I am accessing these subsystem from a single session context.

I am using setRollBackOnly() in case of exception to mark the transactions for rollback.

According to my understanding, container should rollback the transactions "COMPLETELY" in case of exceptions.

But in case I am accessing multiple subsystems, the transaction does not get rolled back "COMPLETLY".

Please help, where am I wrong?
Scott Selikoff
author
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3710
    
    5

As long the J2EE knows about and is managing the database connections you are making, then yes in the event a transaction fails, the entire transaction (unless you are catching the exception) will be rolled back.


My Blog: Down Home Country Coding with Scott Selikoff
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
But in some cases, I am accessing multiple subsystem which in turn open their respective connections to database and close them subsequently. And I am accessing these subsystem from a single session context.


Every connection is associated with a different transaction, so the subsystems have their own transactions.

If the subsystems are using the same database, then simply propagate your transaction into the subsystems by using the same connection and using the transaction attribute of Required or Mandatory for the methods of the subsystems.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Amrit Kashyap
Ranch Hand

Joined: Apr 23, 2006
Posts: 44
I am using setRollBackOnly() in case of exception to mark the transactions for rollback.


Hi Roger,

Yes, my every subsystem is opening and closing independent database connections.

Due to some constraints, I am not able share the database connection, among multiple subsystem.

I am using the transaction attribute of REQUIRED.

Will not the container itself, take care of the transaction to be rolled back and do the needfull to rollback that transactions completely regardless of no. of connections opened and closed with in that transactions context,once I've marked it for Rollback.
Amrit Kashyap
Ranch Hand

Joined: Apr 23, 2006
Posts: 44
As long the J2EE knows about and is managing the database connections you are making, then yes in the event a transaction fails, the entire transaction (unless you are catching the exception) will be rolled back.


Hi Scott,

I didn't get you completely.

But anyhow, I am catching the exception and within exception handler I am marking the tranasaction to be rolleback by calling "setRollBackOnly()" and then throwing that exception again.
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
Your rollbacks are failing because the subsystems have their own transactions.

I haven't got time now to think too much about this right now, but I think that when using container-managed transactions, you will have to remove the connection handling from the subsystems. Then you can propagate your transactions into the subsystems, so your rollbacks will work.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: CMP Transactions Doubt