This topic presents case studies about ANSI session mode transaction semantics. Also see Teradata Session Mode Transaction Processing Case Studies).
Successful ANSI Mode Transaction Example
The following is an example of a successful ANSI mode transaction:
Request | Explanation |
---|---|
INSERT INTO employee SELECT * FROM employee; Result: *** Insert completed. 26 rows added. |
This single request begins an ANSI mode explicit transaction. WRITE locks are held on employee. |
UPDATE employee SET department_number = 400 WHERE department_number = 401 ;DELETE FROM employee WHERE department_number = 401; Result: *** Update completed. 7 rows changes. *** Delete completed. No rows removed. |
This request is composed of two statements. WRITE locks are still held on employee. |
SELECT * FROM employee WHERE department_number = 401; Result: *** Query completed. No rows found. |
This single-statement request is a check to make sure all employees in department_number 401 were deleted in the previous request. Even though this is a SELECT request, which only requires a READ (or ACCESS) lock, the transaction continues to hold a WRITE lock on employee. |
COMMIT; Result: *** COMMIT done. *** Total elapsed time was 1 second. |
This request terminates the transaction by committing all changes. All locks are released and the Transient Journal is dropped from the dictionary. The next request entered begins another ANSI transaction. |
ANSI Session Mode Error and Failure Responses
When ANSI transaction semantics are in effect, SQL Error responses do not cause a rollback, while failure responses do.
The following example shows how error responses do not roll back a transaction:
Request | Explanation |
---|---|
INSERT INTO employee SELECT * FROM customer_service.employee; Result: *** Insert completed. 26 rows added. |
This INSERT ... SELECT request begins an ANSI mode transaction. |
SELECT * FRM employee; Result: *** Error 3706 Syntax error; SELECT * must have a FROM clause. |
Syntaxer problems are errors, not failures in an open transaction. The inserts from the previous request remain, and all locks continue to be in force. |
SELECT * FROM employee; Result: *** Query completed. 26 rows found. 9 columns returned. |
Correcting the SELECT syntax produces a successful request that indicates the inserted rows from the first request remain in place. The transaction is still not committed. |
SELECT * FROM employee WHERE emp_num = 1010; Result: *** Error 5628 Column emp_num not found in Employee. |
Resolver problems are neither always errors, nor always failures. The problem evokes an Error response, not a Failure response. The inserts from the first request remain, and all locks continue to be in force. |
SELECT * FROM employee; Result: *** Query completed. 26 rows found. 9 columns returned. |
Repeating the selection of all columns from the table proves that the inserted rows from the first request remain. The transaction is still not committed. |
COMMIT; |
The transaction and the rows inserted in the first request are committed. |
SELECT * FRM employee; Result: *** Failure 3706 Syntax error; SELECT * must have a FROM clause. |
A failure response is returned since there is no open transaction. Note that an error response is returned, since a transaction was successfully opened. |
INSERT INTO employee SELECT * FROM customer_service.employee; Result: *** Insert completed. 26 rows added. |
This INSERT ... SELECT request begins an ANSI mode transaction. |
CREATE TABLE tbl_1 ( col_1, col_2 INTEGER); Result: *** Error 3739 The user must give a data type for . |
Syntaxer problems are errors, not failures in an open transaction. The inserts from the previous request remain, and all locks continue to be in force. |
SELECT * FROM employee; Result: *** Query completed. 26 rows found. 9 columns returned. |
Selecting all columns from the table proves that the inserted rows from the first request remain. The transaction is still not committed. |
CREATE TABLE tbl_1 ( col_1 INTEGER, col_2 INTEGER); Result: *** Failure 3802 Table ‘tbl_1’ already exists. |
This resolver problem evokes a Failure response, which terminates the transaction. The transaction rolls back and the rows inserted into employee after the commit are deleted. |
SELECT * FROM employee; Result: *** Query completed. No rows found. |
This request begins an ANSI mode transaction. Selecting all columns from the table proves that the previously inserted rows have all been deleted. |
Placing DDL Requests within ANSI Mode Transactions
Like Teradata mode transactions, there can be only one DDL request in a transaction, as the last sequential request other than a COMMIT request.
Unlike Teradata mode transactions, an ANSI mode transaction does not roll back if you attempt to submit another DDL request before committing the transaction. Instead, the transaction continues to respond with Error responses until the requestor either issues a COMMIT request or a ROLLBACK/ABORT request.
Request | Explanation |
---|---|
CREATE TABLE table_3 ( col_1 INTEGER); Result: *** Table has been created. |
This single-statement DDL request begins an ANSI mode transaction. No further requests are valid within the boundaries of the current transaction. |
SHOW TABLE table_3; Result: *** Error 3722 Only a COMMIT WORK or null statement is legal after a DDL Statement. |
This single-statement request evokes an Error response because the previous CREATE TABLE request cannot be followed by any other requests within the boundaries of the current transaction. The transaction does not roll back. |
COMMIT; Result: *** COMMIT done. |
A COMMIT request commits the transaction, which is now complete. |
SHOW TABLE table_3; CREATE MULTISET TABLE DB.table_3 ,NO FALLBACK, NO BEFORE JOURNAL, NO AFTER JOURNAL ( col_1 INTEGER) PRIMARY INDEX ( col_1 ); |
A new ANSI transaction begins with this SHOW TABLE request, which reports the DDL used to create the table named table_3. |
ANSI Mode Transactions and DDL: Multistatement Requests
Like Teradata mode transactions, you cannot mix DDL and DML statements with a single request in ANSI session mode.
The following multistatement request and macro, which have the identical semantics, both fail. An ANSI mode Failure response rolls back the transaction.
SELECT * FROM table_1 ;SELECT * FROM table_2 ;CREATE TABLE table_5 ( col_1 INTEGER);
Result:
*** Failure 3576 Data definition not valid unless solitary. Statement# 1, Info =0
CREATE MACRO mac_1 AS ( SELECT * FROM table_1; SELECT * FROM table_2; CREATE TABLE table_5 ( col_1 INTEGER););
Result:
*** Failure 3576 Data definition not valid unless solitary. Statement# 1, Info =0
ANSI Mode DELETE Performance for Different Transaction Structures
Depending on its structure, a transaction that contains a DELETE request can create a Transient Journal entry for each row deleted from a table or create only one Transient Journal entry for the entire transaction.
The following DELETE is a single request followed by another request to commit the transaction. DELETE writes a Transient Journal entry for each deleted row, so its performance is poor, particularly for large tables.
DELETE FROM table_1; COMMIT;
The following multistatement request contains the same two statements as the previous transaction. Because the statements are packaged as a multistatement request, both statements are seen by the system together. The system does not write a Transient Journal entry for each row deleted from the table, but instead writes a single Transient Journal entry. The performance is good.
DELETE FROM table_1 ;COMMIT;
For the single statement request, the system knows what the next request is and prepares to roll back the transaction. For the multistatement request, the system sees that the delete is to be committed.