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 how to do transaction management? 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 "how to do transaction management?" Watch "how to do transaction management?" New topic

how to do transaction management?

Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
Hi all,
I have little knowledge about "how to transaction control" via JDBC.
Here is what I know,
- we can set Connection to not commit queries automatically
- we can then group our logical operations together sharing that one connection
- then once we are sure that all those individual operations got over successfully we can call commit() on the connection.
Here is what I want to know,
1. How usually people do it? Do we just use this AUTO COMMIT on/off guy?
2. Can I create some object called TransactionManager? But what that will do really if we are just going to follow AUTO COMMIT on/off guy? (I mean does it make sense to have that sort of manager object?)
Please help me here.
I am trying to google on things meanwhile...but experienced opinions are always worth to know ...
Howard Kushner
Ranch Hand

Joined: Sep 19, 2003
Posts: 361
Hello Maulin,
I am not sure why you would want to create a Transaction Manager (TM). It would certainly be useful to manage multiple Resource Managers (RM) in a two-phase commit scenario involving possibly multiple databases, but will be a bit on the complex side. I use EJB's and the container manages all transactions for me.
That said, the TM will tell all the RM's to prepare, and when they are all ready then he wil tell them all to commit. If any fail, then he will tell them all to roll back.
In the JDBC code, use the connection to setAutoCommit(false), then use the connection to begin(), then use the connection to commit() or rollback().
Optionally, you can attempt to set the transaction isolation level of the connection using setTransactionIsolation(int parm) where the int parm is TRANSACTION_NONE, TRANSACTION_READ_COMMITTED, TRANSACTION_READ_UNCOMMITTED, TRANSACTION_REPEATABLE_READ, or TRANSACTION_SERIALIZABLE. See the JavaDoc API's for further info.
Hope that helps. Please let me know if I understood your original question.

Howard Kushner<br />IBM Certified Enterprise Developer - WebSphere Studio Application Developer V5.0<br />IBM Certified Advanced System Administrator - WebSphere Application Server V5.0<br />IBM Certified Solution Developer - Web Services with WebSphere Studio V5.1<br /><a href="" target="_blank" rel="nofollow">Developing J2EE Applications with WebSphere Studio</a> my Certification Study Guide for IBM Test 287
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
HI Howard
Yes. You did understand my question.
I agree with you. I don't need to go for any unnecessary complex model unless absolutely required. I did a simple implementation via setAutoCommit(false) and then using commit() and rollback() things as I don't have really "Resource management" or similar thing which would require sophisticated implementation.
Thanks for your help. I will look into thoes Isolation levels as well.
I have never used EJB. So, I am happy with simple things so far
I agree. Here's the link:
subject: how to do transaction management?
It's not a secret anymore!