9

I'm using PostgreSQL 9.4.

What happens if someone kills the JVM process during exectuion transactional batch-update (batch size = 50) when some of the queries in the batch are already executed?

What will be in the database?

15
  • 6
    Yes, what happens? - Expected behaviour is that the transactions that are not committed, time out on the DB side and are rolled back. Transactions that are committed are, well, committed.
    – blurfus
    Aug 11, 2015 at 19:49
  • 1
    What will be in the database? -> Everything and only what was successfully committed. Everything else won't make it. (If your "batch" process doesn't use transactions then your DB will probably be in an inconsistent state - at least from the business perspective, because from a strict data perspective, PostgreSQL, as a decent ACID RDMS, is able to guarantee durability [aka what was committed/inserted remains committed/inserted]).
    – acdcjunior
    Aug 11, 2015 at 19:54
  • 1
    If some have actually executed but not others then it's not a batch. In a transactional batch, either all of them are executed in reality, or none of them are. Aug 11, 2015 at 19:58
  • 2
    Try to imagine this: 1- jvm starts the transaction; 2- database do the process; 3 - jvm sends the commit to end transaction if you kill the JVM no matters what happens in the database it will not run the commit therefore it will rollback Aug 11, 2015 at 19:59
  • 3
    @PeterLawrey JDBC doesn't specify how batch execution should work with autoCommit=true, and leaves it up to the individual databases and driver implementations. Some drivers rewrite (prepared statement) batches to a single statement, other databases have APIs that allows them to send a batch of parameters for execute, and only commit afterwards, etc. So the commit point with auto commit might vary between implementation (and for batch size within an implementation), so your earlier comment about transactionality is too simplistic if you consider autoCommit=true Aug 12, 2015 at 14:17

2 Answers 2

9

Expected behaviour is that the transactions that are not committed, time out on the DB side and are rolled back. Transactions that are committed are, well, committed.

Integrating answers from various comments:

If some have actually executed but not others then it's not a batch. In a transactional batch, either all of them are executed in reality, or none of them are. – Peter Lawrey

Another

Try to imagine this: 1- jvm starts the transaction; 2- database do the process; 3 - jvm sends the commit to end transaction if you kill the JVM no matters what happens in the database it will not run the commit therefore it will rollback – Jorge Campos

Lastly, for PostgreSQL

What will be in the database? -> Everything and only what was successfully committed. Everything else won't make it. (If your "batch" process doesn't use transactions then your DB will probably be in an inconsistent state - at least from the business perspective, because from a strict data perspective, PostgreSQL, as a decent ACID RDBMS, is able to guarantee durability [aka what was committed/inserted remains committed/inserted].) – acdcjunior

1
  • I'll be spreading upvotes to all the contributors for this answer. Thanks guys
    – blurfus
    Aug 12, 2015 at 15:12
6

There are two situations in which the connection can be: either in auto-commit mode or not in auto-commit mode (by calling Connection#setAutoCommit(false)).

In the first case, when executing a batch of update SQL commands, there could be partial execution of commands, i.e. some commands may be committed and others still not executed. See this quote from the documentation of Statement#executeBatch():

If one of the commands in a batch update fails to execute properly, this method throws a BatchUpdateException, and a JDBC driver may or may not continue to process the remaining commands in the batch. However, the driver's behavior must be consistent with a particular DBMS, either always continuing to process commands or never continuing to process commands.

When a connection is not in auto-commit mode, then only when a call to Connection#commit is returned can we assume that all submitted commands are committed. Either all or none are committed after this call.

4
  • 3
    Note: if you are using auto commit mode, you have turned off batching. Aug 11, 2015 at 20:04
  • @PeterLawrey Couldn't you explain why?
    – St.Antario
    Aug 12, 2015 at 4:07
  • 1
    @St.Antario a batch by definition batch up a series of operations/messages/tasks. When you commit, you say this is the end of the batch. By auto-commiting, you are making the batch size 1. Aug 12, 2015 at 9:20
  • 1
    @PeterLawrey The exact behavior under autoCommit=true depends on the driver. Some drivers will execute the entire batch as one transaction, while others execute the individual items of the batch as separate transactions. Aug 12, 2015 at 11:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.