Please tell me when will we require to call the technique connection.rollback()

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 above code we're not using connection.rollback(), but when some exception happens, even so everything works fine [ i suppose ], cos connection has already been occur autoCommit = false mode.

What exactly may be the possible situation when we have to make use of this method. Please publish the example too.

Within the exception situation your transaction is conflicting. Eventually it'll timeout, so that as you say it'll rollback. But for now (which might be several minutes) all of the locks taken because of your transaction is going to be held. The bond doesn't have method to understand that you might not nearly to commit(). Holding locks to have an extended period like that's very back for concurrency.

Add the rollback for your exception situation.

It might appear that closing your connection will even terminate the transction. When utilizing simple JDBC, in the existence of connection pooling as implemented in application servers closing the bond has got the semantic of "go back to pool" and the bond pool will support the connection's connection to your present transaction. If later inside your code, still within the scope of the identical transaction, you request for any connection the pool will return the same connection. This is handy indeed for writing moular programs, but has got the penalty that you simply cannot assume closing an association resolves the transaction.

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

Whenever you close your connection, your transaction is going to be ended. Most DBMS's will rollback your transaction because they do not know under what conditions the bond was ended (maybe your program was wiped out?). So if you have committed, the rollback is going to do nothing.

However, if you are using Connection-Pooling, whenever you close the bond, the Pool Manager intercepts it and can most likely (hopefully) rollback the bond and then leave the bond open.

It is good practice to rollback within the catch clause, or perhaps the finally clause. It generally does not hurt to complete a pointless rollback following a commit.

Being an aside, if you are using Postgres, it's wise to rollback before you begin to make sure that your transaction start-time is totally reset. Like Postgres supports the current_timestamp value towards the time the transaction began and when you are using put Connections, this might have been a very long time ago!

Should you close an association without carrying out it will transaction is going to be folded back. if you work with an association pool, it's most likely doing that for you personally.

An explicit rollback is most likely appropriate whenever you encounter an ailment that's not leading to the best however, you still don't wish to commit.