This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Regarding container managed Transactions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Regarding container managed Transactions" Watch "Regarding container managed Transactions" New topic

Regarding container managed Transactions

satya gopal

Joined: May 10, 2007
Posts: 13
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

Joined: Apr 11, 2007
Posts: 36
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

Joined: May 02, 2008
Posts: 10
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

Joined: Jan 22, 2008
Posts: 81
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

SCJP <br />SCWCD <br />SCBCD <br />SCEA-1
satya gopal

Joined: May 10, 2007
Posts: 13

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

Joined: Jan 09, 2008
Posts: 91
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

Joined: Jan 15, 2008
Posts: 80
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.

Best Regards,
Moayad Abu Jaber
SCJP 5.0, SCWCD 1.4, SCBCD 5.0
nitin pai
Ranch Hand

Joined: May 30, 2006
Posts: 185
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

Joined: May 10, 2007
Posts: 13

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

Joined: Jan 15, 2008
Posts: 80
for me, now you are right.
I agree. Here's the link:
subject: Regarding container managed Transactions
Similar Threads
how to set auto increment field value ?
A connection taken from Statement != used for creation it at pooling
What's the problem(about update)?
show database on applet
Performance with preparedStatement.