When an error occurs, there is no need to explicitly call rollback, it will automatically roll back.
But there is a requirement: no error occurs, but we need to judge that after a certain situation is met, we also need to roll back. At this time, we need to explicitly call rollback.
It is strongly recommended that an application explicitly commits or rolls back an active transaction prior to calling the close method. If the close method is called and there is an active transaction, the results are implementation-defined.
It means that if there are operations in the transaction, the behavior of direct shutdown is determined by the implementation, it may be commit or it may be rollback. The implementation of MySQL is rollback, so there is no problem if you close it directly.
When an error occurs, there is no need to explicitly call rollback, it will automatically roll back.
But there is a requirement: no error occurs, but we need to judge that after a certain situation is met, we also need to roll back. At this time, we need to explicitly call rollback.
Here:
http://docs.oracle.com/javase...
It means that if there are operations in the transaction, the behavior of direct shutdown is determined by the implementation, it may be
commit
or it may berollback
. The implementation of MySQL isrollback
, so there is no problem if you close it directly.