Transaction Scope
An
existing
transaction ends, and a new transaction starts, at the moment a
commit or rollback command is issued. Assuming you’ve turned
off auto-commit, a COMMIT statement makes permanent any changes
you’ve made to the database using INSERT, UPDATE, or DELETE statements since the last time a
COMMIT or ROLLBACK statement was executed. With
JDBC, commit your changes manually by calling the
Connection
object’s commit( )
method. Calling the commit( )
method sends a COMMIT to the database. The commit( )
method takes no arguments but may throw an
SQLException
. However, it’s very rare for a
commit to result in an exception. For example, to commit changes for
the Connection
named conn
, use
the following code:
conn.commit( );
On the other hand, a rollback command irrevocably discards, or
undoes, any INSERT, UPDATE, or DELETE statements you’ve
executed since the last time a COMMIT or ROLLBACK statement was
executed. To roll back, use the Connection
object’s rollback( )
method. For example, to
roll back updates to the database made using the
Connection
named conn
, use the
following code:
conn.rollback( );
One last note: while auto-commit is off, if a
Connection
is closed without committing or rolling
back, or if any DDL is executed, then any uncommitted changes are
automatically committed.
Get Java Programming with Oracle JDBC now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.