Skip to content

Latest commit

 

History

History
40 lines (25 loc) · 4.21 KB

understanding-transactions.md

File metadata and controls

40 lines (25 loc) · 4.21 KB
title description author ms.author ms.date ms.service ms.subservice ms.topic
Understanding transactions
Learn about transactions and how to group logical units of work that need to maintain consistent data across multiple database operations.
David-Engel
davidengel
08/12/2019
sql
connectivity
conceptual

Understanding transactions

[!INCLUDEDriver_JDBC_Download]

Transactions are groups of operations that are combined into logical units of work. They are used to control and maintain the consistency and integrity of each action in a transaction, despite errors that might occur in the system.

With the [!INCLUDEjdbcNoVersion], transactions can be either local or distributed. Transactions can also use isolation levels. For more information about the isolation levels supported by the JDBC driver, see Understanding Isolation Levels.

Applications should control transactions by either using Transact-SQL statements or the methods provided by the JDBC driver, but not both. Using both Transact-SQL statements and JDBC API methods on the same transaction might lead to problems, such as a transaction cannot be committed when expected, a transaction is committed or rolled back and a new one starts unexpectedly, or "Failed to resume the transaction" exceptions.

Using local transactions

A transaction is considered to be local when it is a single-phase transaction, and it is handled by the database directly. The JDBC driver supports local transactions by using various methods of the SQLServerConnection class, including setAutoCommit, commit, and rollback. Local transactions are typically managed explicitly by the application or automatically by the Java Platform, Enterprise Edition (Java EE) application server.

The following example performs a local transaction that consists of two separate statements in the try block. The statements are run against the Production.ScrapReason table in the [!INCLUDEssSampleDBnormal] sample database, and they are committed if no exceptions are thrown. The code in the catch block rolls back the transaction if an exception is thrown.

[!codeJDBC#UnderstandingTransactions1]

Using distributed transactions

A distributed transaction updates data on two or more networked databases while retaining the important atomic, consistent, isolated, and durable (ACID) properties of transaction processing. Distributed transaction support was added to the JDBC API in the JDBC 2.0 Optional API specification. The management of distributed transactions is typically performed automatically by the Java Transaction Service (JTS) transaction manager in a Java EE application server environment. However, the [!INCLUDEjdbcNoVersion] supports distributed transactions under any Java Transaction API (JTA) compliant transaction manager.

The JDBC driver seamlessly integrates with [!INCLUDEmsCoName] Distributed Transaction Coordinator (MS DTC) to provide true distributed transaction support with [!INCLUDEssNoVersion]. MS DTC is a distributed transaction facility provided by [!INCLUDEmsCoName] for [!INCLUDEmsCoName] Windows systems. MS DTC uses proven transaction processing technology from [!INCLUDEmsCoName] to support XA features such as the complete two-phase distributed commit protocol and the recovery of distributed transactions.

For more information about how to use distributed transactions, see Understanding XA transactions.

See also

Performing transactions with the JDBC driver