When should you call the connection.rollback () method?

Please let me know when we need to call the connection.rollback () method;

try{
  connection = getConnection();
  connection.setAutoCommit(false);
  pstmt1 = connection.preparedstatement ( ... );
  ...
  pstt1.executeUpdate();
  pstmt2 = connection.preparedstatement ( ... );
  ...
  pstt2.executeUpdate();
  connection.commit();
}catch ( Exception sqe ) {  sqe.printStacktrace();
}finally {
  closeQuitely ( pstmt1 );
  closeQuitely ( pstmt2 );
  closeQuitely ( connection );
}

In the above code, we do not use connection.rollback (), but if any exception occurs, even then everything will work fine [I think], the cos connection is already set to autoCommit = false.

So, what could be the situation when we need to use this method. Please also send an example.

+3
source share
3 answers

, . , , (, ?). , , .

, Connection-Pooling, , , , () .

catch finally. , .

, Postgres, , , reset. , Postgres current_timestamp , , -!

+5

. , -, , , . ( ) , , . , (). concurrency.

.

, . JDBC, , , " " , . , , same. moular, , , .

begin tran

// call a method
    get connection

    work

    close connection

// call another method

    get connection  // you get the **same** connection still associated with the tran

    work

    close connection

commit
+4

, . , , , .

, , , , , .

0
source

Source: https://habr.com/ru/post/1716552/


All Articles