如何在三层架构中使用事务和提交? [英] How to use transaction and Commit in three tier architecture?
本文介绍了如何在三层架构中使用事务和提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
任何人都可以给我链接或建议我...如何在三层asp.net架构中使用事务和提交。在哪一层我可以使用事务和提交多个插入QUERY ..
解决方案
事务应该保存在与数据库访问相同的层中,即数据访问图层(DAL)
我为所有交易使用TransactionScope
包装:
public delegate void Transaction();
private const int 重试= 3 ;
public static void TransactionWrapper(交易交易)
{
var options = new TransactionOptions
{
IsolationLevel = IsolationLevel.Snapshot,
Timeout = TimeSpan.FromSeconds( 120 )
};
for ( int x = 0 ; x < 重试; x ++)
{
尝试
{
使用( var scope = new TransactionScope(TransactionScopeOption.Required,options))
{
transaction();
scope.Complete();
}
x =重试;
}
catch (异常例外)
{
// Exception loggedException = new Exception(string.Format(Transaction attempt {0} fail,x),exception);
// Logging.Logs.LogException(loggedException);
if (x ==重试 - 1 )
throw ;
}
}
}
使用如下:
// 创建查询
对象结果;
TransactionWrapper(()= > {
result = // 运行查询
});
返回结果;
我的代码有助于在DAL上创建所有查询案例,包括多个事务,因此我可以同时在包装器中添加所有这些案例。
我希望有帮助^ _ ^
Andy
您可以一次插入多个记录而不是每次插入。
string conStr = ; // 连接字符串
使用(OleDbConnection con = new OleDbConnection(constr))
{
conStr.Open();
var trans = con.BeginTransaction(); // 开始交易
foreach (DataRow dr in dataTable.rows)
{
// < span class =code-comment>从datatable读取数据并插入
string query = ; // 查询
OleDbCommand cmd = new OleDbCommand(query,con);
cmd.Transaction = trans; // 将事务分配给oledb命令
cmd.Parameters.AddWithValue( @ Status,status);
cmd.ExecuteNonQuery();
}
trans.Commit(); // 立即提交交易。
}
Any one can give me link OR Suggest me... How to use transaction and commit in three tier asp.net architecture. And on which layer i can use transaction and commit for multiple insert QUERY ..
解决方案
Transactions should be kept in the same layer as the database access, i.e. the Data Access layer (DAL)
I use aTransactionScope
wrapper for all of my transactions:
public delegate void Transaction(); private const int Retries = 3; public static void TransactionWrapper(Transaction transaction) { var options = new TransactionOptions { IsolationLevel = IsolationLevel.Snapshot, Timeout = TimeSpan.FromSeconds(120) }; for (int x = 0; x < Retries; x++) { try { using (var scope = new TransactionScope(TransactionScopeOption.Required, options)) { transaction(); scope.Complete(); } x = Retries; } catch (Exception exception) { //Exception loggedException = new Exception(string.Format("Transaction attempt {0} fail", x), exception); //Logging.Logs.LogException(loggedException); if (x == Retries - 1) throw; } } }
Which is used as follows:
//create query object result; TransactionWrapper(()=>{ result = //run the query }); return result;
My code lends itself to creating all of the query cases on the DAL, including multiple transactions so I can add all of them in the wrapper at the same time.
I hope that helps ^_^
Andy
You can INSERT multiple records at once instead of inserting each time.
string conStr=""; //Connection string using (OleDbConnection con = new OleDbConnection(constr)) { conStr.Open(); var trans = con.BeginTransaction(); //Begin the transaction foreach (DataRow dr in dataTable.rows) { //Read the data from datatable and insert string query = ""; //Query OleDbCommand cmd = new OleDbCommand(query, con); cmd.Transaction = trans; //Assign transaction to oledb command cmd.Parameters.AddWithValue("@Status", status); cmd.ExecuteNonQuery(); } trans.Commit(); //Committing transaction at once. }
这篇关于如何在三层架构中使用事务和提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文