Click here to Skip to main content
15,881,852 members
Articles / Programming Languages / C#

Declarative Transactions using ADO.NET and without Enterprise Services

Rate me:
Please Sign up or sign in to vote.
4.95/5 (20 votes)
26 Oct 20018 min read 216.1K   1.5K   87  
Sometimes, it is nice to prototype up a simple database application. This code may help, by providing the automatic transactional model of COM+ in a non-COM+ environment. This example uses "Interception" to provide automatic transactioning support for non-COM+ classes.
using System;
using System.Threading;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Messaging;
using System.Data.SqlClient;
using System.Data;

namespace RSI.Transactions
{
	[DbConnection]
	[Transaction(TransactionOption.Required)]
	class TestClass1 : ContextBoundObject
	{
		public				TestClass1() {}

		[AutoComplete]
		public void			TrxRequired() 
		{
			TestClass2 tc2 = new TestClass2();
//			throw new System.Exception("This is an exception");
			tc2.TrxRequired();
			TrxRequired2("ooga");
			tc2.TrxRequired2("booga");
//			ContextUtil.SetComplete();
		}

		[AutoComplete]
		public void			TrxRequired2(string strText) 
		{
		}
	}

	[Transaction(TransactionOption.RequiresNew)]
	[DbConnection]
	class TestClass2 : ContextBoundObject
	{
		public				TestClass2() {}

		[AutoComplete]
		public void			TrxRequired() 
		{
			TrxRequired2("ooga");
		}

		[AutoComplete]
		public void			TrxRequired2(string strText) 
		{
		}
	}

	class Class1
	{
		static void Main(string[] args)
		{
			//
			// TODO: Add code to start application here
			//
			try
			{
				TestClass1 tc1 = new TestClass1();
				tc1.TrxRequired();
			}
			catch(System.Exception exc)
			{
				System.Console.WriteLine(exc.Message);
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


Written By
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions