04 September 2004

Should I use SetAbort()/SetComplete() or DisableCommit()/EnableCommit()?

It was originally from a MCP vendor exercise question. I have it modified slightly:


You are creating a serviced component named UserManager. UserManager adds user
accounts to multiple transactional data sources.
The UserManager class includes the following code segment:

[Transaction(TransactionOption.Required)]
[ObjectPooling(true, MinPoolSize=1, MaxPoolSize=50)]
[JustInTimeActivation()]
public class UserManager : ServicedComponent {
public void AddUser(string TestKname, string TestKpassword)
{
// Code to add the user to data sources goes here.
}
}

You must ensure that the AddUser method reliably saves the new user to either all data
sources or no data sources. What should you do?
A. To AddUser, add the following attribute:
[AutoComplete()]
B. To UserManager, add the following attribute:
[JustInTimeActivation(false)]
C. To the end of AddUser, add the following line of code:
ContextUtil.EnableCommit();
D. To the end of AddUser, add the following line of code:
ContextUtil.SetComplete();
E. To the end of AddUser, add the following line of code:
ContextUtil.MyTransactionVote = true;


IMHO, there are two issues need to be addressed here:
1. Object activation and pooling. .NET 247 : object persistence across client method calls discusses this issue. Basically, the ‘done’ flag decide whether an object is ‘done’ and can be deactivated.
2. MyTransactionVote or consistent flag.

The SDK Doc says, ‘When MyTransactionVote is set to Commit, the COM+ consistent bit is set to true and the COM+ context votes to commit the transaction. If MyTransactionVote is set to Abort, the consistent bit is set to false and the COM+ context votes to abort the transaction.’

And here is matrix of the four methods:
METHOD---------------consistent---------------done
SetComplete()------------true-----------------true
SetAbort()---------------false----------------true
EnableCommit()-----------true-----------------false
DisableCommit()----------false----------------false

The question is about when and how to do 1) ‘all or not’ transaction; 2) object activation and pooling.
EnableCommit() cast a 'go' vote for transaction yet maintain the object as activated so C will be the best answer.

No comments: