Home >Database >Mysql Tutorial >关于数据库操作及事务的处理

关于数据库操作及事务的处理

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-07 15:41:471198browse

DAL基于ADO.NET的话,那么可以为你的DAL增加一个返回DbTransaction对象的方法,然后使用类这样的工厂方法 C# code ? 1 2 3 4 5 6 7 using (DbTransactiontran=CreateMyTransaction()) { method_a(tran); method_b(tran); method_c(tran); tran.Commit(); }

DAL基于ADO.NET的话,那么可以为你的DAL增加一个返回  DbTransaction 对象的方法,然后使用类似这样的工厂方法

C# code ?

1

2

3

4

5

6

7

using(DbTransaction tran=CreateMyTransaction())

{

    method_a(tran);

    method_b(tran);

    method_c(tran);

    tran.Commit();

}



总之你的所谓DAL方法要作出修改,它如果需要访问数据库,那么从传入的tran中去取得 Connection 属性,并调用 Connection 的 CreateCommand 方法来创建 Command,来操作数据库。



事务只在同一线程的同一数据库连接下有效,
而另一方面,每一个具体的业务,会包括大量的实体类的增删改查处理,

如果由DAL控制事务,每一次都要频繁的开关数据库,有可能会很浪费资源。

事务的目的是回滚,采用DAL控制的方式无法进行回滚。
因此,事务的发起与结束只能有业务层控制,而不能由DAL控制,这就决定了数据库的开关也只能由BLL说了算。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn