开发者

Transaction between database and a memory task ?

I want to make some job in database then some task on memory(like change some values of objects) and i want to combine both task in same transaction.

I am using EF 4.1 and it just provide me SaveChanges method which makes transaction by itself. Simply if SaveChanges does not throw exception so i can do some task on memory. This is a solution. But it break architecture of application. Because Sa开发者_高级运维veChanges does not called before that task. Task can not know that SaveChanges will be success or fail in future when it is called.

Is it possible to make any transaction between database and memory tasks ?

If it is so how we can do that ?


Juval Lowy has an excellent article in MSDN titled "Volatile Resource Managers in .NET Bring Transactions to the Common Type" explaining how to create transactional wrappers around in-memory types.

At its core, you want to implement the ISinglePhaseNotification interface. Once you have this implemented for the in-memory type you want to be part of the transaction, you have to register it by calling the EnlistVolatile method on the Transaction class to register the instance for notification on the transaction.

Typically, you would make the call to EnlistVolatile by looking to see if the static Transaction.Current property is not null, and if it isn't, registering with that (e.g. this is what classes in the System.Data.SqlClient namespace do to in order to self-register).

However, you can enlist your instance(s) anywhere you wish once you know the Transaction is in place (after your TransactionScope is created, for example).

If you don't want to go through the above, you can also subscribe to the TransactionCompleted event and change the state of your memory variables depending on the state of the Transaction returned through the Transaction property on the TransactionEventArgs passed to the event handler.

You can get the a TransactionInformation instance through the TransactionInformation property on the Transaction. The TransactionInformation class has a Status property which returns a value from the TransactionStatus enumeration indicating what the status of the transaction is (committed, aborted, etc.).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜