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

I am using PostgreSQL 9.4.

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

What will be in the database?

+8
java postgresql jdbc
source share
2 answers

The expected behavior is that transactions that are not running, a timeout on the DB side, and rollback. Transactions that are committed, committed, good, committed.

Integration of responses with various comments:

If some of them are really executed, but not others, then this is not a package. In a transactional batch, either all of them are actually executed, or none of them are executed. - Peter Lowry

Other

Try to imagine this: 1- jvm starts the transaction; 2 - the database performs this process; 3 - jvm sends a commit to complete the transaction, if you kill the JVM, it doesn't matter what happens in the database, it won’t start committing, so it will roll back - Jorge Campos

Finally for PostgreSQL

What will be in the database? β†’ Everything and only what was successfully accomplished. All the rest will not do it. (If your β€œbatch” process does not use transactions, then your database will probably be in an inconsistent state - at least from a business point of view, because from the point of view of strict data PostgreSQL, as a decent ACID DBMS, can guarantee durability [ aka what was committed / inserted remains committed / inserted). - acdcjunior

+7
source share

There are two situations in which a 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 package of SQL update commands, partial execution of commands can be executed, that is, some commands can be executed, while others have not been executed yet. See this quote from the documentation of Statement#executeBatch() :

If one of the batch update commands does not work properly, this method throws a BatchUpdateException , and the JDBC driver may or may not continue to process the remaining commands in the batch. However, the driver behavior must correspond to a specific DBMS, either always continuing to process commands, or never continue to process commands.

If the connection is not performed in the automatic commit mode, then only when the Connection#commit call returns, we can assume that all the sent commands are committed. After this call, all or all are committed.

+6
source share

All Articles