简体   繁体   中英

SQL Server: Default action for opened transaction in a stored procedure if cancelled from .net code

I am executing a stored procedure on my .net code. The stored procedure has begin transaction and it only commits once there is no error, otherwise it will rollback the transaction. If I'm going to cancel the execution of the stored procedure from my .net code, does the transaction will be committed or rolled back?

The default behavior is the transaction started with BEGIN TRAN in the proc code will remain open after the proc is cancelled by the client. You can (and should) add SET XACT_ABORT ON to the stored procedure so that SQL Server will automatically roll back the transaction following an application cancel request or timeout. This is a best practice when one uses explicit transactions in stored procedures to avoid inadvertently leaving transactions open for longer than necessary, or even worse, unwittingly performing other work in the connection with the open transaction.

Don't try to manage transactions on both the client and server. Choose one or the other.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM