Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens if we kill the JVM process during transaction execution?

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?

like image 754
St.Antario Avatar asked Aug 11 '15 19:08

St.Antario


People also ask

How to shutdown JVM gracefully?

The JVM shuts down abruptly when: For example, by issuing a kill -9 <jvm_pid> Calling Runtime. getRuntime(). halt() from Java code.

How to shut down running JVM processes?

There are two ways to terminate the JVM instance from the running application: Calling the exit() method, which initiates the JVM normal shutdown sequence: run all registered shutdown hooks; run all uninvoked finalizers; then end the JVM instance. Calling the halt() method, which ends the JVM instance immediately.


2 Answers

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

like image 142
blurfus Avatar answered Oct 24 '22 09:10

blurfus


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.

like image 42
M A Avatar answered Oct 24 '22 08:10

M A