Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to do transaction management?

 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ...
Regards
Maulin
 
Howard Kushner
author
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Regards,
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Regards
Maulin
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic