This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes JDBC and Relational Databases and the fly likes Transaction handling in JDBC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Transaction handling in JDBC" Watch "Transaction handling in JDBC" New topic

Transaction handling in JDBC

Shubhashis Dasgupta

Joined: Jul 27, 2001
Posts: 9
Hi all,
I have a question regarding transaction management in a JSP-Servlet-Javabean (MVC) arch. I have written a connection pool class and getting free connections from the pool wherever needed. I have some business objects like user, application etc(Object-Relational mapping). Those are not EJBs but I have written entire jdbc code in those business objects to handle create, update, remove or find operations. Now my problem is, I have to handle transaction within a client servlet which creates, updates and deletes some record from separate table thru those business objects, since the business objects use separate connection objects from pool I am not being able to handle the transaction using commit and rollback methods of connection.
Does anybody have a soln. for this?
A Agrawal
Ranch Hand

Joined: Jul 13, 2001
Posts: 41
Actually You would need to change your architecture a bit...In case of Transaction don't return the connection back to pool, keep it in some hashtable [maintained by a singleton (i.e. single instance class) on server] keeping some ID (say ContextId) as key and connection as value.
maintain this ContextId at client time you need the connection to continue/commit/rollback the transaction, take it from the hashmap on the basis of Id your client prgm is passing.
do watever you want to do with this connection and return it to the pool only when you are done.
and yes don't forget to turn autocommit property false before you start using connection for a transaction. also keep contextid unique.
hope this helps.

Amit Agrawal,
New Delhi, India.
[This message has been edited by A Agrawal (edited August 16, 2001).]

Amit Agrawal,<BR>New Delhi, India.
It is sorta covered in the JavaRanch Style Guide.
subject: Transaction handling in JDBC
It's not a secret anymore!