Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding container managed Transactions

 
satya gopal
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all

i have a CMT stateful session bean. In the business method i have three database statements.In each statement i have opened a connection to access the database and i closed the connection in the statement only.
The question is if if an exception occus in the second statement will the first statement be rollbacked.
 
Steven Young
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe in this situation, the first database statement will be rolled back if the second database statement fails and causes a transaction rollback. The first statement is still past of the same container-managed transaction.

I have not been able to find a definitive statement on this in the specification, but page 329 of the EJBCore specification provides an example of a bean-managed transaction that spans several methods and opens and closes a database connection. The spec states:

"It is possible for an enterprise bean to open and close a database connection in each business method (rather than hold the connection open until the end of transaction)."

I don't see why the rule would be any different for CMT. The point is that opening and closing a database connection does not change the status of the transaction.
 
Ashok Kaliaperumal
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If an exception occurs in second statement, it will roll back the first statement. This happens due to the reason that all three statements are part of a single transaction and an exception in one statement would roll back all the previously executed statements.

~~Ashok K
 
Vishal Matere
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although, I do agree with everybody that 1st statement will be rolled back, I have a small doubt here.

After 1st statement, if database connection is closed, doesnt that mean, after 1st statement DB changes are COMMITTED. & now if 2nd statement throws exception , how will DB changes be rolled back when they are actually COMMITTED.

Is it not at the mercy of DB to rollback?

Just wondering
V
 
satya gopal
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

Thanks for your valuable replies .But i still have one doubt.
This time i have a normal java class .In the first line i have started a transaction and i have the above three database statements which opens and closes the connection in each statment.At last i have closed the
transaction . what happens if i get an exception in the second statement.

My quesion is does the first statment rollbacks happens in any NORMAL JAVA class which has transaction.
 
bernard savary
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys ,
I have basic doubt which is slightly related to the topic , in a CMT or BMT method I get a connection from a datasource and do insert DB operation inside the method and then call the transaction manager rollback either through UserTransaction.setrollbackonly or ejbcontext.setrollbackonly , after the method completes i find that the DB stmt to insert a new row is commited even though I have called the txn manager to rollback. Is there any configuration that needs to be done at the server to configure a datasource to a TransactionManager ? How does the tranaction manager work with thge resource manager ? I know it is different incase of JPA because DB operations are done through the persistencecontext .
 
Moayad Abu Jaber
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Maybe my answer will wondering the all person write here. but my answer the first statement will be committed and don't rollback. cause the container will be done the operation immediate the statement execute he don't wait other statement.
 
nitin pai
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I recently read about JPA and from what I have read I may say that:

In a transaction the database is not updated until the transaction ends or is closed. Which means that even if you close the DB connection the database won't be updated. Maybe the server does caching of queries until the transaction ends.

Let me know if this conclusion is proper.
 
satya gopal
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

so can we conclude this topic in this way.

So,even if we start a new Transaction we have to consider the AUTO-COMMIT option.

if Auto-commit option is true.
.All the database statements are commited immediately.
.so,even if a transaction is started if we get a exception in the second statement the first statement is not rollbacked.

if i am wrong correct me
 
Moayad Abu Jaber
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for me, now you are right.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic