Controlling Database Transactions


In Chapter 8 you learned about database transactions and how to use the SQL COMMIT statement to permanently record changes you make to the contents of tables. You also saw how to use the ROLLBACK statement to undo changes in a database transaction. The same concepts apply to SQL statements executed using JDBC statements within your Java programs.

By default, the results of your INSERT , UPDATE , and DELETE statements executed using JDBC are immediately committed. This is known as auto-commit mode.

Note  

Generally, using auto-commit mode is not the preferred way of committing changes because it is counter to the idea of considering transactions as logical units of work. With auto-commit mode, all statements are considered as individual transactions, and this is usuall y an incorrect assumption. Also, auto-commit mode may cause your SQL statements to take longer to complete, due to the fact that each statement is always committed.

Fortunately, you can enable or disable auto-commit mode using the setAutoCommit() method of the Connection class, passing it a Boolean true or false value. The following example disables auto-commit mode for the Connection object named myConnection :

 myConnection.setAutoCommit(false); 
Note  

You can also enable auto-commit mode using setAutoCommit(true) .

Once auto-commit has been disabled, you can commit your transaction changes using the commit() method of the Connection class, or you can roll back your changes using the rollback() method. In the following example, the commit() method is used to commit changes made to the database using the myConnection object:

 myConnection.commit(); 

In the next example, the rollback() method is used to roll back changes made to the database:

 myConnection.rollback(); 

If auto-commit has been disabled and you close your Connection object, an implicit commit is performed. Therefore, any DML statements you have performed up to that point and haven t already committed will be committed automatically.




Oracle Database 10g SQL
Oracle Database 10g SQL (Osborne ORACLE Press Series)
ISBN: 0072229810
EAN: 2147483647
Year: 2004
Pages: 217

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net