This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes EJB and other Java EE Technologies and the fly likes Session Auto-Comit? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Session Auto-Comit?" Watch "Session Auto-Comit?" New topic
Author

Session Auto-Comit?

Luciano A. Pozzo
Ranch Hand

Joined: Jun 20, 2005
Posts: 112
Hello

I have a session bean stateless defined with "transaction-type=container". So, does not matter which type of connection with database I have(pool, jdbc etc), my database transaction will be always atomic? because is inside of session bean method?

Thank's
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
Not quite. For instance, when dealing with distributed transactions the Tx Data Source must be enabled for 2-phase commit and an XA driver used.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Luciano A. Pozzo
Ranch Hand

Joined: Jun 20, 2005
Posts: 112
Hummm.... but this is correct? I have to use a diferent type of datasource for work the session transaction control? I read on Mastering EJB:

"... the EJB container intercepts the request and starts a transaction automatically on behalf of your bean. That is, the container issues the begin statement to the underlying transaction system to start the transaction. The container then delegates the invocation to your enterprise bean, which performs operations in the scope of transaction. Your bean can do anything it wants to, such as perform logic, write to a database, send asynchronous message, or call other enterprise beans.If a problem occurs, the bean can signal to the container that the transaction must abort."

Can somebody clarify the things about session bean transaction?! please!

Thanks
Abelard Chow
Greenhorn

Joined: Jan 25, 2005
Posts: 7
I will see if I can answer your question.

For transaction type, there are two settings, Bean managed and Container managed. For your SLSB, you are using Container managed transaction, which means the container is managing the transcation for you as indicated in the section of "Mastering EJB" you quoted.

With your transaction type setting, your SLSB will be bounded by a transction created by the container. Transaction begin when the method started and transction commit when the method ended. (Of course, unless rollback in the middle). So unless you are playing strange stuff with your database connection, your transction should be atomic. However it is not just because it is inside a session bean method call, but also because you are using Container manged transaction and SLSB.

Hope it help


Abelard Chow<br /> <br />Some see things as where they were and ask why, I dream things that never are and ask why not.
Luciano A. Pozzo
Ranch Hand

Joined: Jun 20, 2005
Posts: 112
Hi Abe....

Yes, I understand that this is the correct! The container manage the transaction, so should be atomic. But I tested with the method below! So when I update the database (oracle) and follow throw an Exception, the container don't rollback the update, so I have an inconsistency, because account2 will not be updated, and the account1 is updated. You know why this thing happen?

Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
You must throw a system exception, not an application exception. Try throwing EJBException.
Luciano A. Pozzo
Ranch Hand

Joined: Jun 20, 2005
Posts: 112
Hummmm.... yes, now worked, the database did not updated!

So... why only System Exceptions should thrown for work the transaction system? and another question: what can I do to solve this problem: if SQLException is thrown, the transaction will not happen again.. because is a Application Exception (SQLException is a Exception)....

But thank-you Roger, was great

Sachin Holla
Greenhorn

Joined: Dec 30, 2005
Posts: 5
Hi..

System exception is something which cannot be recovered, so the continer will rollback the transaction. Container views application exceptions as an abnormal application-level condition, which typically can be recovered. So the container will not rollback the transaction, it tries commit...

So in such cases, you should explicitly mark the transaction for rollback using EjbContext.setRollbackOnly() method. Your code would look like this:

try {
// ---- do your db ops ---

} catch (SQLException e) {
// Mark the transaction for rollback..
sessionCtx.setRollbackOnly();

// If you want throw some appliation exception here..
throw new NotEnoughBalanceException();
}


- holla
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Session Auto-Comit?
 
Similar Threads
transaction in session bean
Transaction handling in Session Bean
Non-EJB transaction rollbacks
SessionBean calling multiple stored procedures
JTA Problem in VAJ 4