Transactions in ODBC

Transactions in ODBC are managed at the connection level. When an application completes a transaction, it commits or rolls back all work completed through all statement handles on that connection. To commit or roll back a transaction, applications should call SQLEndTran instead of submitting a COMMIT or ROLLBACK statement.

An application calls SQLSetConnectAttr to switch between the two ODBC modes of managing transactions:

  • Autocommit mode

    Each statement is automatically committed when it is completed successfully. When you run in autocommit mode, no other transaction management functions are required.

  • Manual-commit mode

    All executed statements are included in the same transaction until it is specifically stopped by calling SQLEndTran.

Autocommit mode is the default transaction mode for ODBC. When a connection is made, it is in autocommit mode until SQLSetConnectAttr is called to switch to manual-commit mode by setting autocommit mode off. When an application turns autocommit off, the next statement sent to the database starts a transaction. The transaction then remains in effect until the application calls SQLEndTran with either the SQL_COMMIT or SQL_ROLLBACK options. The command sent to the database after SQLEndTran starts the next transaction.

If an application switches from manual-commit to autocommit mode, the driver commits any transactions currently open on the connection.

ODBC applications should not use Transact-SQL transaction statements such as BEGIN TRANSACTION, COMMIT TRANSACTION, or ROLLBACK TRANSACTION because this can cause indeterminate behavior in the driver. An ODBC application should run in autocommit mode and not use any transaction management functions or statements, or run in manual-commit mode and use the ODBC SQLEndTran function to either commit or roll back transactions.


SQLEndTran Function

Conformance

Version Introduced: ODBC 3.0 Standards Compliance: ISO 92

Summary

SQLEndTran requests a commit or rollback operation for all active operations on all statements associated with a connection. SQLEndTran can also request that a commit or rollback operation be performed for all connections associated with an environment.

NoteNote

For more information about what the Driver Manager maps this function to when an ODBC 3.x application is working with an ODBC 2.x driver, see Mapping Replacement Functions for Backward Compatibility of Applications.

SQLRETURN SQLEndTran(
     SQLSMALLINT   HandleType,
     SQLHANDLE     Handle,
     SQLSMALLINT   CompletionType);
HandleType

[Input] Handle type identifier. Contains either SQL_HANDLE_ENV (if Handle is an environment handle) or SQL_HANDLE_DBC (if Handle is a connection handle).

Handle

[Input] The handle, of the type indicated by HandleType, indicating the scope of the transaction. See "Comments" for more information.

CompletionType

[Input] One of the following two values:

SQL_COMMIT SQL_ROLLBACK

SQL_SUCCESS, SQL_SUCCESS_WITH_INFO, SQL_ERROR, SQL_INVALID_HANDLE, or SQL_STILL_EXECUTING.


reference:

http://msdn.microsoft.com/en-us/library/ms131281.aspx

http://msdn.microsoft.com/zh-cn/library/ms716544.aspx


Transactions(事务)是指将一组数据库操作作为单个逻辑工作单元执行的过程,它要么完全执行,要么完全不执行。如果执行过程中出现故障,那么所有的更改都会回滚,以确保数据库保持一致性。 在关系型数据库中,事务通常遵循ACID(原子性、一致性、隔离性和持久性)原则,以确保数据的正确性和完整性。以下是ACID的详细解释: - 原子性(Atomicity):一个事务是原子的,即它是一个不可分割的最小工作单元。如果事务成功执行,则所有更改将提交到数据库中;如果事务失败,则所有更改将回滚到状态的起点。 - 一致性(Consistency):在事务开始之前和事务结束时,数据库必须处于一致状态。这意味着事务中的所有更改都必须满足数据库中的约束条件。 - 隔离性(Isolation):隔离性指的是并发事务之间的互相隔离性。这意味着一个事务的执行不应该影响另一个事务的执行。 - 持久性(Durability):一旦事务完成,其结果就应该是永久的。这意味着事务中的所有更改都应该保存在数据库中,并且在系统故障时也应该能够恢复。 在实际应用中,事务通常由BEGIN、COMMIT和ROLLBACK语句控制。BEGIN语句用于标识事务的开始,COMMIT语句用于标识事务的结束,并将更改保存到数据库中,而ROLLBACK语句用于回滚事务中的所有更改。 事务可以确保数据的正确性和完整性,特别是在具有高并发读写操作的应用场景下非常有用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值