请问TransactionScope是不是只是对数据库操作的事务处理?
例如如下代码:
petshop4.0中OrderProcessor
using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required, tsTimeout)) {
// Receive the orders from the queue
for (int j = 0; j < batchSize; j++) { try {
//only receive more queued orders if there is enough time
if ((elapsedTime + queueTimeout + transactionTimeout) < tsTimeout.TotalSeconds) {
queueOrders.Add(order.ReceiveFromQueue(queueTimeout));
}
else {
j = batchSize; // exit loop
} //update elapsed time
elapsedTime = new TimeSpan(DateTime.Now.Ticks).TotalSeconds - datetimeStarting.TotalSeconds;
}
catch (TimeoutException) { //exit loop because no more messages are waiting
j = batchSize;
}
} //process the queued orders
for (int k = 0; k < queueOrders.Count; k++) {
order.Insert((OrderInfo)queueOrders[k]);
processedItems++;
totalOrdersProcessed++;
} //batch complete or MSMQ receive timed out
ts.Complete();
}
这里的事务处理中对queueOrders.Add(order.ReceiveFromQueue(queueTimeout));这句话起作用吗?这句代码是对队列的操作并不是对数据库的操作,但是在事务处理里面。
例如如下代码:
petshop4.0中OrderProcessor
using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required, tsTimeout)) {
// Receive the orders from the queue
for (int j = 0; j < batchSize; j++) { try {
//only receive more queued orders if there is enough time
if ((elapsedTime + queueTimeout + transactionTimeout) < tsTimeout.TotalSeconds) {
queueOrders.Add(order.ReceiveFromQueue(queueTimeout));
}
else {
j = batchSize; // exit loop
} //update elapsed time
elapsedTime = new TimeSpan(DateTime.Now.Ticks).TotalSeconds - datetimeStarting.TotalSeconds;
}
catch (TimeoutException) { //exit loop because no more messages are waiting
j = batchSize;
}
} //process the queued orders
for (int k = 0; k < queueOrders.Count; k++) {
order.Insert((OrderInfo)queueOrders[k]);
processedItems++;
totalOrdersProcessed++;
} //batch complete or MSMQ receive timed out
ts.Complete();
}
这里的事务处理中对queueOrders.Add(order.ReceiveFromQueue(queueTimeout));这句话起作用吗?这句代码是对队列的操作并不是对数据库的操作,但是在事务处理里面。
DML操作之锁操作的表,不锁数据库的。如你上面所说,那当程序a和程序b都连接同一个数据库,程序a进行DML操作并且没有提交,此时程序b应该还是能进行DML操作的吧,只是需要等待程序a的操作结束(commit 或 rollback)。
所以只对你操作的表有影响。
并且影响取决于你设置的 事务等级。默认是Srexxx..什么的,就是只能读取,不能修改,不能添加。
TransactionScope是隐式事务,是System.Transaction下的一个类,比传统的ADO.NET事务的优点就在于
1.创建方便,管理方便,耗费资源少。它是由LTM自动管理的。
2.可以扩展到分布式事务。楼主你用TransactionScope,他有一个作用域,就在这个using块中,只有事务的全体对象和资源都成功Commit(),才会成功,否则自动Rollback()