Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Can I use transactions within a servlet?

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you David.
 
What are you saying? I thought you said that Santa gave you that. And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic