wood burning stoves*
The moose likes Websphere and the fly likes Can I use transactions within a servlet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Websphere
Bookmark "Can I use transactions within a servlet?" Watch "Can I use transactions within a servlet?" New topic
Author

Can I use transactions within a servlet?

Joseph Magerramov
Ranch Hand

Joined: Oct 28, 2002
Posts: 35
Our software doesn't use EJBs but rather accesses database directly. For example, a certain servlet could call several methods, each of them obtaining a separate connection from the datasource and using it to update database.
Since there are several connections, connection.setAutoCommit(false) obviously won't work. I was wondering if it's possible to execute all this db calls as part of one transaction. Any suggestions or samples would be appreciated.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Check out UserTransactions and TransactionManager. I haven't tried it in WebSphere, let alone in a Servlet or library code, but from memory something like this may work:
The UserTranaction is an interface, the factory to obtain one is registered in JNDI.
It would look like this:

Also be aware that it may be bound in JNDI as javax.transaction.UserTransaction, I can't remember...
It is also a common practice to wrap the ut.begin/commit around a single method that does all the SQL.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

My appologies, it may also be "java:comp/UserTransaction". I wish I'd written it down. You may want to go looking for it
Joseph Magerramov
Ranch Hand

Joined: Oct 28, 2002
Posts: 35
Thank you David.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can I use transactions within a servlet?