在组件之间实现事务和异步提交事务(NET2.0)
生活随笔
收集整理的這篇文章主要介紹了
在组件之间实现事务和异步提交事务(NET2.0)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
假設(shè)要Insert1個(gè)用戶和該用戶對(duì)應(yīng)角色,而這2個(gè)Insert方法已經(jīng)封裝到2個(gè)dll中了,現(xiàn)在要在這個(gè)2個(gè)方法中實(shí)現(xiàn)事務(wù),代碼很簡(jiǎn)單,如下(需先添加對(duì)System.Transactions程序集的引用)
?1public?void?Insert(string?UserName,ArrayList?alRoles)
?2{
?3using?(TransactionScope?_ts?=?new?TransactionScope())
?4{
?5?????//插入用戶
?6??????new?user().Insert(UserName);
?7
?8?????//??插入角色
?9???????new?Role().Insert(UserName,alRoles);
10
11??????_ts.Complete();
12}
13}
?2????????{
?3????????????Transaction?_old?=?Transaction.Current;
?4????????????CommittableTransaction?_newCommit?=?new?CommittableTransaction();
?5????????????Transaction.Current?=?_newCommit;
?6
?7????????????try
?8????????????{
?9????????????????_newCommit.BeginCommit(OnCommit,?null);
10????????????}
11????????????finally
12????????????{
13????????????????Transaction.Current?=?_old;
14????????????}
15????????}
16????????void?OnCommit(IAsyncResult?asy)
17????????{
18????????????CommittableTransaction?_commit;
19????????????_commit?=?asy?as?CommittableTransaction;
20
21????????????try
22????????????{
23????????????????using?(_commit)
24????????????????{
25????????????????????SqlConnection?_cnn?=?new?SqlConnection("");
26????????????????????SqlCommand?_cmd?=?new?SqlCommand();
27
28
29????????????????????_cnn.Open();
30
31????????????????????_cnn.EnlistTransaction(_commit);????//??利用事務(wù)
32
33????????????????????_cmd.Connection?=?_cnn;
34
35????????????????????_cmd.CommandText?=?".";
36????????????????????_cmd.ExecuteNonQuery();
37
38????????????????????_cmd.CommandText?=?"";
39????????????????????_cmd.ExecuteNonQuery();
40
41????????????????????_commit.EndCommit(asy);
42
43????????????????????_cnn.Close();
44????????????????}
45????????????}
46????????????catch?(Exception?e)
47????????????{
48
49????????????}
50????????}
上面我們用TransactionScope和CommittableTransaction。
TransactionScope是隱示的,是由系統(tǒng)自動(dòng)管理的,所以它沒有什么RollBack方法。
CommittableTransaction是顯示的,它有commit和RollBack方法。
------------------------------------------
以上都是個(gè)人的看法和體會(huì),如有不妥處,還請(qǐng)大家多多指點(diǎn),謝謝!
?1public?void?Insert(string?UserName,ArrayList?alRoles)
?2{
?3using?(TransactionScope?_ts?=?new?TransactionScope())
?4{
?5?????//插入用戶
?6??????new?user().Insert(UserName);
?7
?8?????//??插入角色
?9???????new?Role().Insert(UserName,alRoles);
10
11??????_ts.Complete();
12}
13}
異步提交事務(wù)的代碼也很簡(jiǎn)單:
?1????????public?void?Work()?2????????{
?3????????????Transaction?_old?=?Transaction.Current;
?4????????????CommittableTransaction?_newCommit?=?new?CommittableTransaction();
?5????????????Transaction.Current?=?_newCommit;
?6
?7????????????try
?8????????????{
?9????????????????_newCommit.BeginCommit(OnCommit,?null);
10????????????}
11????????????finally
12????????????{
13????????????????Transaction.Current?=?_old;
14????????????}
15????????}
16????????void?OnCommit(IAsyncResult?asy)
17????????{
18????????????CommittableTransaction?_commit;
19????????????_commit?=?asy?as?CommittableTransaction;
20
21????????????try
22????????????{
23????????????????using?(_commit)
24????????????????{
25????????????????????SqlConnection?_cnn?=?new?SqlConnection("");
26????????????????????SqlCommand?_cmd?=?new?SqlCommand();
27
28
29????????????????????_cnn.Open();
30
31????????????????????_cnn.EnlistTransaction(_commit);????//??利用事務(wù)
32
33????????????????????_cmd.Connection?=?_cnn;
34
35????????????????????_cmd.CommandText?=?".";
36????????????????????_cmd.ExecuteNonQuery();
37
38????????????????????_cmd.CommandText?=?"";
39????????????????????_cmd.ExecuteNonQuery();
40
41????????????????????_commit.EndCommit(asy);
42
43????????????????????_cnn.Close();
44????????????????}
45????????????}
46????????????catch?(Exception?e)
47????????????{
48
49????????????}
50????????}
上面我們用TransactionScope和CommittableTransaction。
TransactionScope是隱示的,是由系統(tǒng)自動(dòng)管理的,所以它沒有什么RollBack方法。
CommittableTransaction是顯示的,它有commit和RollBack方法。
------------------------------------------
以上都是個(gè)人的看法和體會(huì),如有不妥處,還請(qǐng)大家多多指點(diǎn),謝謝!
轉(zhuǎn)載于:https://www.cnblogs.com/BearsTaR/archive/2006/04/17/Transactions.html
總結(jié)
以上是生活随笔為你收集整理的在组件之间实现事务和异步提交事务(NET2.0)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: windows2003中mssql连接的
- 下一篇: 让我们努力从“不可救药的乐观主义者”--