|
RSS Feed - WebSphere MQ Support
|
RSS Feed - Message Broker Support
|
 |
|
.NET transactions without components |
« View previous topic :: View next topic » |
Author |
Message
|
dpchiesa |
Posted: Fri Jul 01, 2005 1:39 pm Post subject: .NET transactions without components |
|
|
 Apprentice
Joined: 29 May 2002 Posts: 46
|
By the way, For an MQ app using the .NET classes, the MQ client library will automatically enlist in a transaction created programmatically via System.EnterpriseServices.ServiceDomain.Enter(). This is sometimes called "services without components". It is a .NET transaction programming model that does not require registering a component with COM+, and does not require that you derive your class from ServicedComponent. It is available only in WinXP SP2 or WS2003.
The doc in WebSphere MQ Solutions in a Microsoft .Net Environment (SG24-7012-00) discusses transactions via COM+ configured components. The same transactional guarantees are available with SwC.
The code looks something like this:
Code: |
using( TxUtil.ESTransactionScope ts = new TxUtil.ESTransactionScope())
{
try {
// do transactional work...
ts.VoteToCommit = true;
}
catch (Exception ex1)
{
Console.WriteLine("Exception:\n{0}", ex1.ToString());
}
}
|
Within the transactional work, you need to ask for syncpoints on the Get() or Put(), eg gmo.Options|= MQC.MQGMO_SYNCPOINT, or pmo.Options|= MQC.MQPMO_SYNCPOINT.
The ESTransactionScope class mentioned above is derived from sample code from Florin Lazar, http://blogs.msdn.com/florinlazar/archive/2004/07/24.aspx.
My version looks like:
Code: |
namespace TxUtil
{
// Used to create transactional code blocks
public class ESTransactionScope : System.IDisposable
{
// Dispose must be called to exit the transactional block
public void Dispose()
{
if(!this.VoteToCommit)
{
ContextUtil.SetAbort();
}
ServiceDomain.Leave();
}
public ESTransactionScope()
{
EnterTxContext(TransactionOption.Required);
}
public ESTransactionScope(TransactionOption txOption)
{
EnterTxContext(txOption);
}
public ESTransactionScope(String TrackingAppName)
{
EnterTxContext(TrackingAppName, TransactionOption.Required);
}
public ESTransactionScope(String TrackingAppName, TransactionOption txOption)
{
EnterTxContext(TrackingAppName, txOption);
}
private void EnterTxContext(TransactionOption txOption)
{
EnterTxContext(System.Reflection.Assembly.GetExecutingAssembly().GetName().Name, txOption);
}
private void EnterTxContext(String TrackingAppName, TransactionOption txOption)
{
ServiceConfig config = new ServiceConfig();
config.Transaction = txOption;
config.TrackingAppName = TrackingAppName;
config.TrackingEnabled = true;
// get my own stack frame
System.Diagnostics.StackFrame sf0 = new System.Diagnostics.StackFrame( 0, true );
// for the TrackingComponentName, walk the callstack backwards
// until we get a type outside the current one.
System.Diagnostics.StackFrame sfn= null;
for (int i=1; i < 6; i++) {
sfn = new System.Diagnostics.StackFrame( i, true );
if (sf0.GetMethod().DeclaringType.FullName == sfn.GetMethod().DeclaringType.FullName) break;
}
config.TrackingComponentName = sfn.GetMethod().DeclaringType.FullName;
ServiceDomain.Enter(config);
}
// By default, the scope is marked as inconsistent, which means
// it will vote to abort.
// To Commit the transaction on exit, the caller must set
// the VoteToCommit flag before the scope is Dispose'd
public bool VoteToCommit = false;
}
}
|
_________________ -dpchiesa |
|
Back to top |
|
 |
|
|
 |
|
Page 1 of 1 |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|
|
|