[thelist] Confused on EJB Container Managed Transactions

VOLKAN ÖZÇELİK volkan.ozcelik at gmail.com
Tue Jul 26 07:35:36 CDT 2005


Hi evolters!

I am sure this is the only list to solve my confusion.

My question is on J2EE. 
And it may be a damn simple one, sorry for that.

* * *

Let us say I have a method doBusinessLogic in my stateless session bean as

void doBusinessLogic() {
   try {
        con = ... make jndi lookup and obtain a connection

        1. con.execute("INSERT INTO SOME TABLE");
        2. con.execute("EXEC SOME SP");
        3. con.execute("DELETE SOMETHING FROM SOME OTHER TABLE");
        4. con.execute("SOME OTHER DB STUFF");
   }
   catch(ZillionsOfExceptions ze) {
        ze.printStacTrace();
   } 
   finally() {release resources}
}

(I know that I have to use prepared statements etc, the above is just
a simplified semi-pseudocode.)

Now to the question: 

Given that the method uses container managed transaction, if the
statements (1 up to 4) were ejb setter methods that use CMP for
persisting into DB, instead of hard coded SQL calls;

like

1. myBean.setSomehting(123);
2. myBean.setSomeOtherThing("lorem dolorem");
3. etc

then when in the middle of the transaction (say at statement 2) an
exception is thrown; myBean.setSomething should be rolled back
(
since the container generates the apropriate insert SQL behind the
scenes and knows the value before insert (i.e. it remembers it's
former state), it will not be a big deal for it to set the related
table(s) back to their original state.
)

* * *

However, in our case we are using hardcoded SQL statements. 
So if, take for example an SQLException is thrown at statement 4
("SOME OTHER DB STUFF");

will the ejb-container ever be able to roll back the entire transaction?

Or is it "what has been always going on" and I am just unaware of it?

imho, it is quite impossible for the container to roll back hardcoded
SQL executes. Else life would be really really easy:
There would be no duplicate key exceptions, no foreign key problems,
no duped data: peace at home, peace all over the world :)

(
one more thing for the non-J2EE: 
note that I do not use any "begin tran" or "commit tran" in the method. 
So there is no actual database transaction, 
only an ejb container managed transaction exists.
)

Hope I was clear on expressing my problem. And hope, someone has a
clear-cut answer for it. Else I will be having a series sleepless
nights with this question pondering in my mind all the time.

Thanks a lot in advance.

Cheers,
Volkan.


More information about the thelist mailing list