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 related question 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 related question" Watch "Transaction related question" New topic

Transaction related question

Vicky Pandya
Ranch Hand

Joined: Dec 16, 2004
Posts: 148
Hello all,

could be a stupid question:- I have a requirement that part of the same transaction, I need to insert and subsequently make two more updates to the same records (same pk) in a single transaction. These operations have to be all or nothing. Anyway to achieve in a single transaction? or any other idea?

Rob Spoor

Joined: Oct 27, 2005
Posts: 20269

I believe this is database related, so I'll move this to our JDBC forum.

For transactions in JDBC you can use a combination of Connection.setAutoCommit(false) to disable automatic committing of separate statements, Connection.commit() to finish a transaction and Connection.rollback() to undo everything after the last commit.

I don't get one thing though. Why can't you set these values for the newly inserted record when you insert it?

How To Ask Questions How To Answer Questions
Sridhar Santhanakrishnan
Ranch Hand

Joined: Mar 20, 2007
Posts: 317
Why do you have to update the record twice in the same transaction? Cant you use the last value directly and commit the transaction?
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

Didn't Rob already ask that?

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Sridhar Santhanakrishnan
Ranch Hand

Joined: Mar 20, 2007
Posts: 317
Paul Sturrock wrote:Didn't Rob already ask that?

Yes, he has. But I didnt actually take a good look at his post.
I agree. Here's the link:
subject: Transaction related question
It's not a secret anymore!