Blue Theme Orange Theme Green Theme Red Theme
 
Click Here for 3 Month Free of ASP.NET Hosting!
Home | Forums | Videos | Photos | Downloads | Blogs | E-Books | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » SQL Server 2005/2008 » Simulating autonomous transactions in Microsoft SQL Server

Simulating autonomous transactions in Microsoft SQL Server

In the article, I will cover how to use table variable techniques in order to simulate the autonomous transaction behavior of Oracle databases in Microsoft SQL Server.

Author Rank:
Technologies: SQL Server 2005,Visual C# .NET
Total downloads :
Total page views :  3860
Rating :
 0/5
This article has been rated :  0 times
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
 
Become a Sponsor


Related EbooksTop Videos


 

An autonomous transaction is a feature of Oracle databases which enables to leave the context of the calling transaction and perform an independent transaction. An autonomous transaction is a nested transaction which can modify data and commit or rollback independent of the state of the parent transaction. There is no link with the calling transaction, so only committed data can be shared by both transactions. This is a common scenario where auditing is required logging error messages despite the transaction is doing a rollback. In Microsoft SQL Server, there is no direct equivalent for this. Moreover, if you have a nested transaction, one needs to be aware of the fact that the outermost commit is what controls the inner commits and any of inner rollback will rollback all the whole transaction. In the article, I will cover how to use table variable techniques in order to simulate the autonomous transaction behavior of Oracle databases in Microsoft SQL Server.

Autonomous transactions in Oracle


Let's create two tables table1 and table2 in Oracle database (see Listing 1).

create table table1
(nId int primary key,
vcValue varchar2(20)
);
create table table2
(nId int primary key,
vcValue varchar2(20)
);

Listing 1

Now let's create two procedures: one procedure that initiates a parent transaction which in turn calls for a nested autonomous transaction (see Listing 2).


create or replace procedure insert_into_table1 (nId int, vcValue varchar2)
as
pragma autonomous_transaction;
begin
insert into table1
values(nId, vcValue);
commit;
end;
/
create or replace procedure insert_into_table2 (nId int, vcValue varchar2)
as
begin
insert_into_table1 (nId,vcValue);

insert into table2
values(nId, vcValue);
commit;
end;
/

Listing 2

Let's test the behavior of the autonomous transaction. Run the following P-SQL statements (see Listing 3).

begin
insert into table2
values(1,'value1');
commit;

insert_into_table2(1,'value1');
commit;
end;


Listing 3


The result is displayed as shown in Listing 4


Begin
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.SYS_C005744) violated
ORA-06512: at "SCOTT.INSERT_INTO_TABLE2", line 6
ORA-06512: at line 6

Listing 4

When you select the rows of the table1 by running the SQL statement in Listing 5, you will see that the autonomous transaction commits its changes and enable the insertion of a row even though the parent transaction rollbacks its changes because there is a primary key constraint violation (see Listing 6).

select *
from table1;

Listing 5

NID VCVALUE

---------- --------------------
1 value1

1 row selected.

Listing 6

Now let's simulate the same behavior in Microsoft SQL Server using table variables.

First of all, let's create the two tables (see Listing 7).

create table table1
(nId int primary key,
vcValue varchar(20)
);

create table table2
(nId int primary key,
vcValue varchar(20)
);

Listing 7

Now it's the time to code a stored procedure which implements the logic of autonomous transactions (see Listing 8).

create procedure autonomous_transactions_example
@nId int,
@vcValue varchar(20)
as
begin
declare
@temporary_table table(nId int, vcValue varchar(20));
begin tran

begin tran
insert into
@temporary_table
values(@nId, @vcValue);

if @@error>0
begin
rollback;
end
else
begin
commit;
end;

insert into table2
values(@nId, @vcValue);

if @@error>0
begin
rollback;
end
else
begin
commit;
end;


insert into table1
select * from @temporary_table;
end;

Listing 8

Now let's test the behavior of the simulated autonomous transaction. Run the following T-SQL statements (see Listing 9).

begin
begin tran
insert into
table2
values(1,'value1');
commit;

exec autonomous_transactions_example 1,'value1';
end;

Listing 9


The output of this operation is shown in Listing 10.
(1 row(s) affected)

(1 row(s) affected)
Msg 2627, Level 14, State 1, Procedure autonomous_transactions_example, Line 22
Violation of PRIMARY KEY constraint 'PK__table2__7E6CC920'. Cannot insert duplicate key in object 'dbo.table2'.
The statement has been terminated.

(1 row(s) affected)
 

Listing 10

Now if you select the rows of the table1 using the SQL statements in Listing 11, you will result the results shown in Listing 12.

select *
from table1;

Listing 11


nId vcValue
----------- --------------------
1 value1

(1 row(s) affected)

Listing 12

Conclusion
In this article, I covered the techniques to simulate the autonomous transaction behavior of Oracle databases in Microsoft SQL Server.
 


Login to add your contents and source code to this article
 [Top] Rate this article
 About the author
 
John Charles Olamendy
He’s a senior Integration Solutions Architect and Consultant. His primary area of involvement is in Object-Oriented Analysis and Design, Database design , Enterprise Application Integration, Unified Modeling Language, Design Patterns and Software Development Process. He has knowledge and extensive experience in the development of Enterprise Applications using Microsoft.NET and J2EE technologies and standards. He is proficient with distributed systems programming; and business-process integration and messaging using the principles of the Services Oriented Architecture (SOA) and related technologies such as Microsoft BizTalk Server, Web Services (Windows Communication Foundation, WSE, BEA WebLogic, Oracle AS and Axis) through multiple implementations of loosely-coupled system. He’s a prolific blogger contributing to .NET and J2EE communities and actively writes articles on subjects relating to integration of applications, business intelligence, and enterprise applications development. He holds a Master’s degree in Business Informatics at Otto Von Guericke University, Magdeburg, Germany. He was recently awarded as MVP. He currently works in the telecommunication industry and delivers integration solutions for this industry. He harbors a true passion for the technology.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010
Microsoft Visual Studio 2010 offers more to developers than any other Visual Studio release. Work more productively and collaboratively-with greater control over your work at every step. The Beta 2 can give you a head start on achieving efficiency.
 
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
Become a Sponsor
 Comments

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 1999 - 2009  Mindcracker LLC. All Rights Reserved