wood burning stoves 2.0*
The moose likes EJB and other Java EE Technologies and the fly likes EJB transaction management? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "EJB transaction management?" Watch "EJB transaction management?" New topic
Author

EJB transaction management?

Herman Zweibel
Greenhorn

Joined: Feb 02, 2004
Posts: 12
This is probably a stupid question but:
For a function of my web application I need to perform two SQL queries - the first will delete rows and a second query will insert into a table.
The issue is - I don't want either query to be commited unless both are successful.
Is EJB a good choice for this kind of problem?
Many Thanks
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Herman Zweibel:
Is EJB a good choice for this kind of problem?

Sure, but certainly not necessary. You can do the above using JTA and JDBC. Your code will acquire a UserTransaction via JNDI from your container, begin a transaction, execute the two SQL statements, and finally commit the transaction. If an SQLExcption is thrown, make sure to rollback the transaction.
If you want to use EJBs, you could create a stateless session bean that does the above. This would allow you to use container-managed transactions (you just mark the method as "Required" and it will take care of the rest). However, if you'll only be doing this a few times, it may be overkill.
Herman Zweibel
Greenhorn

Joined: Feb 02, 2004
Posts: 12
Really helpful - thanks!!
David Buchwald
Greenhorn

Joined: May 08, 2001
Posts: 8
Don't forget that you will need to use an XA compliant JDBC driver.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by David Buchwald:
Don't forget that you will need to use an XA compliant JDBC driver.

That should only be necessary if the two queries need to be executed through two different connections, right?
Chris Mathews
Ranch Hand

Joined: Jul 18, 2001
Posts: 2712
Originally posted by Herman Zweibel:
This is probably a stupid question but:
For a function of my web application I need to perform two SQL queries - the first will delete rows and a second query will insert into a table.
The issue is - I don't want either query to be commited unless both are successful.
Is EJB a good choice for this kind of problem?
Many Thanks

Not knowing more about your application it is kind of hard to really say. But if this is your only requirement then I say that EJB is not the best choice. You can easily do this type of transaction with straight JDBC... there does not seem to be a real need for declarative transactions or 2PC. See my recent article for more details on when I feel EJB is appropriate.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: EJB transaction management?