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 setAutoCommit(boolean autoCommit) 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 "setAutoCommit(boolean autoCommit)" Watch "setAutoCommit(boolean autoCommit)" New topic

setAutoCommit(boolean autoCommit)

Raje Kumar

Joined: Sep 13, 2004
Posts: 2
What does setAutoCommit(false) signify? Does it mean that it is not automatically committed? I need to give another commit command.
or does it mean that commit is done when connection is closed.
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

I'm not sure whether 'close ()' will do the commit.

When inserting about 1 million rows into a postgres-database, I got an error, because the number of uncommited inserts was limited to something between 10,000 and 200,000.

Perhaps this value is somehow customizable, but uncommited inserts are mostly done for performance-reasons on large inserts, so having a commit every 10,000 rows isn't that bad, if we don't want to get an error after 8 h of reading.
Pedro Gongora

Joined: Oct 03, 2003
Posts: 17
it means that all the commands you send to the database will not be commited until you call commit(). In other words it begins a new transaction.

see the example:

Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Drawbacks of long commits:

- long commits results in lack of performance, i.e. locks contention.
- say you inserted 1,000 rows in AutoCommitFalse mode and then you need to rollback just last 200 then what wil you do?
- some other user will get previous data for a long time. may be he/she need to prepare a report for some manager.

Drawback of short commits:

- you can not rollback your previous insert/update/delete, because you have committed it.

So, it depends on the scenerio, what you would like to do. hence its better to go for something in between. if long commits are required then go for something Stefan Wagner said.
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1082

hi Adeel

my view little bit differs

Long commit:

If I have inserted 1000 rows and I want to rollback last 200 rows , means I want to commit 800 rows .
Then I can still use connection.setSavepoint(String savepoint_x)

this will rolback to my transaction to my mentioned save point and I think we can have muliple setSavepoint.

Short Commit :

after commit we can not rollback that true but it is for everywhere even in EJB managed transactions.
This is something related to code flow
[ October 07, 2004: Message edited by: Shailesh Chandra ]

Gravitation cannot be held responsible for people falling in love ~ Albert Einstein
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
ofcourse shailesh, its always depend on the code flow. but when you commit it means commit it.

and about savePoint(). yeah right this is the way we can handle that scenrio. i haven't got any thought of it at that time. thanks for reminding anyways.
[ October 07, 2004: Message edited by: adeel ansari ]
Surya Ram

Joined: Aug 09, 2004
Posts: 7
Here is an example
SQL updates
You have to do it as some applications will have the autocommit set to false at Database level and leave the programmers to handle it in their Java code.
I agree. Here's the link:
subject: setAutoCommit(boolean autoCommit)
It's not a secret anymore!