• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

CMP Transactions Doubt

 
Amrit Kashyap
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4014
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Amrit Kashyap
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic