Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Transaction rollback  RSS feed

 
prasad bhalerao
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I have a question regarding transaction in EJB.

I am using a session facade design pattern in which i have 4 layers as follows
AO (Application objects),BO(Business Objects),EO(Entity Object),DAO(Data access objects... which contains plain java code)

AO gives call to BO. AO is implemented using Stateless session beans)
BO is a plane java object which gives call to EO
Here EO s again implemented using Stateless session Beans.
And finally EO gives call to DAO methods.

In this case i am not at all using Entity bean. I am updating 3 table in single EO method. e.g. UpdateProfile. i.e. i am invoking 3 dao methods from updateProfile EO method.
Now in this case if call to update first two table is successful but if method call to update 3rd table fails i.e. 3rd DAO method fails i want rollback the whole transaction.
i.e i want to rollback the database updates to keep database in consistent state.
When one of the database update fails i am throwing application specific exception.

Can this database rollback be achieved using CMT? Please advise me how to do this without using entity beans.

Thanks in advance!

 
Devaka Cooray
Marshal
Posts: 5554
707
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"prasad bm", Please check your private messages regarding an important administrative matter from JavaRanch.
 
Abhijeet Nalawade
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In this case i am not at all using Entity bean. I am updating 3 table in single EO method. e.g. UpdateProfile. i.e. i am invoking 3 dao methods from updateProfile EO method.
Now in this case if call to update first two table is successful but if method call to update 3rd table fails i.e. 3rd DAO method fails i want rollback the whole transaction.
i.e i want to rollback the database updates to keep database in consistent state.
When one of the database update fails i am throwing application specific exception.

Can this database rollback be achieved using CMT? Please advise me how to do this without using entity beans.


Can be done by Bean managed Transactions
For eg.
 
prasad bhalerao
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Abhijeet thnx for the reply..
But in this case if i use CMT and if i throw exception from updateUserTable3() method this transaction will be rolled back... am i right? m just confused.... So in this case rollback be acheived by CMT?!
if i use setRollbackonly if exception is thrown would this rollback the transaction?
Also in this case if i say the transaction is rolled back how it is going to rollback the database update? Some of you might find this question stupid but please advise me?
Does any one know what exactly happens when transaction is rolled back how it actually rollsback the database? What mechanism is used to achieve this?

 
Abhijeet Nalawade
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

But in this case if i use CMT and if i throw exception from updateUserTable3() method this transaction will be rolled back... am i right? m just confused.... So in this case rollback be acheived by CMT?!

First correction we are using BMT and not CMT. If exception is thrown in any of the 3 update table method the whole transaction from start to end will be rolled back.

if i use setRollbackonly if exception is thrown would this rollback the transaction?

For BMT the UserTransaction.setRollbackonly API will execute all the code block between transaction start to end even when Tx fails in between. Thread on setRollBackOnly
 
prasad bhalerao
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for link... it was really helpful
1 more question...
When i say transaction is rolled back how does it really rolls back the database?

e.g : Assume that there is only 1 update preparedStatments in each of the following methods.
# updateUserTable1();
# updateUserTable2();
# updateUserTable3();

After doing the update in first 2 method if i rollback the transaction, how does it rolls back the database?
What mechanism is used to achieve this?
 
prasad bhalerao
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well i got my answer....

When the container rolls back a transaction, it always undoes the changes to data made by SQL calls within the transaction. However, only in entity beans will the container undo changes made to instance variables. (It does so by automatically invoking the entity bean's ejbLoad method, which loads the instance variables from the database.) When a rollback occurs, a session bean must explicitly reset any instance variables changed within the transaction. The easiest way to reset a session bean's instance variables is by implementing the SessionSynchronization interface.

If anyone wants to add something to this it would be great!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!