Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
195 views
in Technique[技术] by (71.8m points)

java - addBatch() used together with autoCommit = true

I have a situation like this :

con.setAutoCommit(true);
Statement stmt = con.createStatement();
stmt.addBatch(query);//add all the queries
stmt.executeBatch();

Will each query commit separately or there will be a single commit for the entire batch?

Note : The database is Oracle 11

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

According to the JDBC specification the exact behavior of a batch execute with auto-commit enabled is implementation defined. So it depends on the database and its driver, and it is better to not make assumptions on the exact behavior and disable auto-commit when using batch executes.

JDBC 4.1, section 14.1.1 says:

The commit behavior of executeBatch is always implementation-defined when an error occurs and auto-commit is true.

This only says something about the behavior for error conditions, but the implication is that individual statements might already have been committed when another statement in the batch throws an exception.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...