// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) using System; using System.Linq; using System.Transactions; using Apache.NMS; using Apache.NMS.ActiveMQ; namespace Tx.XAMQDB.EF { public class MSDTC : MQDB { public override IConnection GetSourceConnection() { IConnectionFactory cf = new ConnectionFactory(new Uri("tcp://localhost:61616")); IConnection con = cf.CreateConnection(); con.Start(); return con; } public IConnection GetSourceTxConnection() { IConnectionFactory cf = new NetTxConnectionFactory(new Uri("tcp://localhost:61616")); IConnection con = cf.CreateConnection(); con.Start(); return con; } public override TargetDbContext GetTargetDbContext() { return new TargetDbContext("PGSQL"); } public override void Test() { for(int i = 0; i < 10; i++) { using(IConnection srccon = GetSourceTxConnection()) { using(ISession srcses = srccon.CreateSession(AcknowledgementMode.AutoAcknowledge)) { using(TransactionScope tx = new TransactionScope()) { using(IQueue qin = srcses.GetQueue("Qin")) { using(IMessageConsumer receiver = srcses.CreateConsumer(qin)) { using(TargetDbContext trgctx = GetTargetDbContext()) { try { IMessage msg = receiver.Receive(); if(i % 2 != 0) throw new Problem(); TblTrg otrg = trgctx.TblTrg.First(row => row.Id == 1); otrg.Val = otrg.Val + 1; trgctx.SaveChanges(); tx.Complete(); } catch(Problem) { Console.WriteLine("Problem"); } } } } } } } } } public static void Main(string[] args) { Demo(new MSDTC()); Console.ReadKey(); } } }