File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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.
I agree. Here's the link:
subject: Transaction handling in JDBC
It's not a secret anymore!