Presentation is loading. Please wait.

Presentation is loading. Please wait.

Transactions. Contents ● Transactions ● Save Points ● Batch Updates.

Similar presentations


Presentation on theme: "Transactions. Contents ● Transactions ● Save Points ● Batch Updates."— Presentation transcript:

1 Transactions

2 Contents ● Transactions ● Save Points ● Batch Updates

3 Transactions ● You can group a set of statements to form a transaction. ● The transaction can be committed when all has gone well. Or, if an error has occurred in one of them, it can be rolled back as if none of the statements had been issued. ● The major reason for grouping statements into transactions is database integrity.

4 Transactions ● For example, suppose we want to transfer money from one bank account to another. – It is important that we simultaneously debit one account and credit another. – If the system fails after debiting the first account but before crediting the other account, the debit needs to be undone.

5 Transactions ● If you group update statements to a transaction: – The transaction either succeeds in its entirety ● it can be committed. – or it fails somewhere in the middle ● you can carry out a rollback and the database automatically undoes the effect of all updates that occurred since the last committed transaction.

6 ● By default, a database connection is in autocommit mode, and each SQL statement is committed to the database as soon as it is executed. Once a statement is committed, you cannot roll it back. – Turn off this default when you use transactions: ● conn.setAutoCommit(false);

7 ● Turn off autocommit off conn.setAutoCommit(false); ● Create a statement object in the normal way: Statement stat = conn.createStatement(); ● Call executeUpdate any number of times: stat.executeUpdate(command1); stat.executeUpdate(command2); stat.executeUpdate(command3);...

8 ● If all statements have been executed without error, call the commit method: conn.commit(); ● However, if an error occurred (interrupted by a SQLException ), call conn.rollback();

9 Save Points ● With some drivers, you can gain finer-grained control over the rollback process by using save points. ● Creating a save point marks a point to which you can later return without having to abandon the entire transaction.

10 Save Points ● Statement stat = conn.createStatement(); // start transaction; rollback() goes here ● stat.executeUpdate(command1); ● Savepoint svpt = conn.setSavepoint(); // set savepoint; rollback(svpt) goes here ● stat.executeUpdate(command2); ● if (...) conn.rollback(svpt); // undo effect of command2 ●... ● conn.commit();

11 ● When you no longer need a save point, you should release it: conn.releaseSavepoint(svpt);

12 Batch Updates ● Suppose a program needs to execute many INSERT statements to populate a database table. You can improve the performance of the program by using a batch update. In a batch update, a sequence of statements is collected and submitted as a batch. ● The statements in a batch can be actions such as INSERT, UPDATE, and DELETE as well as data definition statements such as CREATE TABLE and DROP TABLE. An exception is thrown if you add a SELECT statement to a batch.

13 Statement stat = conn.createStatement(); ● Call the addBatch method: ● String command = "CREATE TABLE..." stat.addBatch(command); while (...) { command = "INSERT INTO... VALUES (...)"; stat.addBatch(command); } ● Finally, you submit the entire batch: int[] counts = stat.executeBatch();

14 ● Treat the batch execution as a single transaction – If a batch fails in the middle, you want to roll back to the state before the beginning of the batch.

15 ● boolean autoCommit = conn.getAutoCommit(); conn.setAutoCommit(false); Statement stat = conn.getStatement();... // keep calling stat.addBatch(...);... stat.executeBatch(); conn.commit(); conn.setAutoCommit(autoCommit);


Download ppt "Transactions. Contents ● Transactions ● Save Points ● Batch Updates."

Similar presentations


Ads by Google