-Transaction Management in java is required when we are dealing with relational databases.
-By default when we create a database connection, it runs in auto-commit mode.
It means that whenever we execute a query and it's completed, the commit is fired automatically.
-So every SQL query we fire is a transaction and if we are running some DML or DDL queries, the changes are getting saved into database after every SQL statement finishes.
-Sometimes we want a group of SQL queries to be part of an transaction so that we can commit them when all the queries runs fine. If we get any exception we have a choice of rollback all the queries executed as part of the transaction.

Advantages of Transaction Management:
-Fast Performance: It makes the performance fast because database is hit at the time of commit.
-In JDBC, Connection interface provides methods to manage transaction.
void setAutoCommit (boolean status) : It is true by default means each transaction is committed by default.
void commit() : Commits the transaction.
void rollback() : Cancel the transaction.
Example:

Comments