Skip to content

Instantly share code, notes, and snippets.

@jen20
Created January 2, 2013 13:33
Show Gist options
  • Save jen20/1de2dc287e880fd7f16c to your computer and use it in GitHub Desktop.
Save jen20/1de2dc287e880fd7f16c to your computer and use it in GitHub Desktop.
namespace EventStoreDTCFirstTry
{
public class EventStoreDistributedTransaction
{
private static readonly Guid ResourceManagerId = new Guid("F6049197-5B56-4C90-ACA9-39A2516C547A");
private readonly EventStoreConnection _connection;
private readonly EventStoreTransaction _esTransaction;
public EventStoreDistributedTransaction(EventStoreConnection connection, string stream, int expectedVersion)
{
_connection = connection;
_esTransaction = _connection.StartTransaction(stream, expectedVersion);
if (Transaction.Current != null)
Transaction.Current.EnlistDurable(ResourceManagerId, new InternalEnlistment(this), EnlistmentOptions.None);
}
public void Write(IEnumerable<IEvent> events)
{
_connection.TransactionalWrite(_esTransaction.TransactionId, _esTransaction.Stream, events);
}
private void Commit()
{
_connection.CommitTransaction(_esTransaction.TransactionId, _esTransaction.Stream);
}
private class InternalEnlistment : IEnlistmentNotification
{
private readonly EventStoreDistributedTransaction _transaction;
public InternalEnlistment(EventStoreDistributedTransaction transaction)
{
_transaction = transaction;
}
public void Prepare(PreparingEnlistment preparingEnlistment)
{
preparingEnlistment.Prepared();
}
public void Commit(Enlistment enlistment)
{
_transaction.Commit();
enlistment.Done();
}
public void Rollback(Enlistment enlistment)
{
enlistment.Done();
}
public void InDoubt(Enlistment enlistment)
{
enlistment.Done();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment