How to use the transaction to avoid misuse distributed transaction (System.Transactions.TransactionScope)

1: Local Affairs DbTransaction and distributed transactions TransactionScope the difference:

1.1:System.Data.Common.DbTransaction:

Local transactions: this is nothing to say, that is a single transaction, each database has its own implementation, the depth of the connotation of affairs can search to see related articles, not the focus of this paper.

1.2:System.Transactions.TransactionScope:

Distributed transaction, you need to add a reference to System.Transactions, at the same time enable the MSDTC Distributed Transaction Service : commonly used method is:

  the using  (TS = System.Transactions.TransactionScope  new new  System.Transactions.TransactionScope ())
 {
                 // code block A
                
// code block B
                ts.Complete (); // commit the transaction
 }

Distributed Transaction essentially on the introduction of a third-party referee to find a way to monitor multiple local transaction succeed or fail, here to share some knowledge:

A: If the code block in. If there are two or more database links DbConnection, you need to start the Microsoft Distributed Transaction MSDTC service.

Use the command line to start or stop services:

 

B: If the code block, only one database link DbConnection, it is really just a local transaction processing, distributed transaction even if the MSDTC service did not start, it will not error.

C: For block TransactionScope contained essentially in monitoring the number of code blocks DbConnection database link, if there are a plurality of different objects, the introduction of MSDTC referee, while the actual implementation of each local transaction or transactions.

D: MSDTC is not always stable, together with my two simple transaction before the test, press F5 to refresh constantly, even able to MSSQL services are to hang, and even cross-database with local transaction would not be a problem.

 

So it is not necessarily the case, as far as possible not to introduce a distributed transaction, you should avoid using TransactionScope to contain the impulse to block the transaction


2: can be solved with local affairs, avoid the use of distributed transaction scenarios:

2.1: Only one database, this is the most should be avoided, the transaction between multiple tables, the range is entirely local affairs can be handled:

Problem: a code block in the N operation hybridization entity class, the entity class with each link various databases, which triggered the MSDTC .

Can: a common DbConnection objects to avoid enable MSDTC.

 

2.2: Project multiple databases, if between databases using the same account and password to access, this situation can be avoided:

Each database has its own solution: like MSSQL, cross-database processing as long as the prefix (dbname..tablename) can, therefore, make use of only local transactions will be processed.

 

3: Review my previous project scenario:

3.1: The figure is from my 08 years in the project field: There are 19 databases, for database link, the only difference being only the name of the database:

 

Evolution: Can you keep only one, the name of the database to dynamically switch to another by changing the connection string?

 

3.2: At that time, the architecture is still very new to me, by CodeSmith generate a lot of code files:

Entity projects, plant projects, interface item, data manipulation, and a large number of stored procedures CRUD, just to basic CRUD and only for MSSQL.

随便展开一个项目都会看到大量的文件夹,里面有大量的文件,而这些东东,都是代码生成器的杰作:

 

19个数据库啊,NN个表,光生成这些基本的增删改查,整个项目就好像高端大气上档次了。

进化:能不能消灭这些大量的文件,简单是我们不断追求的目标。

 

3.3:这么多数据库,如何跨数据库事务?

对于生成的大量的实体,每个表的操作都是一个新的链接,单库间的事务都必须MSDTC了,更别说19个库间的跨库事务了。

进化:能不能本地事务搞定这些,这是每个ORM可以思考的方向。

 

4:CYQ.Data 提供的解决方案: 

为了消灭上述的那些大量的生成文件,我在后续新的公司写了传统的ORM框架:XQData(这个框架一直没露过面,也只是支持MSSQL,用反射消灭了好多层,只留下实体层)。

对于框架的演进,多数都来源于项目中遇到的问题,或复杂的场景,需要解决或者简化,才有了不断升级的可能,并不是无中生有,因此,一个框架,如果不能不断在在项目中实战,那么很多问题和细节等可能都无法发现,更新也会遥遥无期。 

而CYQ.Data的不断升级,说明我一直在奋战在一线的编码生涯中和网友各大项目中的实战反馈中。 

 

下面针对最近的优化调整,演示下CYQ.Data是怎么解决上面提到的几个问题:

4.1:多个数据库的数据库链接切换:

A:先用配置工具生成针对多数据库的枚举文件,和成demo和test两个数据库:

两个数据库就生成两次了。

B:配置一个默认的链接字符串,到Demo数据库:

<add name= " Conn " connectionString= " server=.;database=demo;uid=sa;pwd=123456 "/>

C:打印操作Test数据库表的链接字符串:

using (MAction action =  new MAction(TestEnum.Users))
            {
                Console.WriteLine(action.ConnectionString);
            }

输出:

 

这里自动切换了数据为名称为新的链接,而我动手脚的地方就是在枚举的名称了。

4.2:本地多数据库跨事务,示例:

            AppDebug.OpenDebugInfo =  true;
            AppDebug.Start();
             using (MAction action =  new MAction(DemoEnum.Users))
            {
                 action.BeginTransation();//开启事务
                 action.Fill( 12);
                Console.WriteLine(action.ConnectionString);//打印数据库链接
                action.ResetTable(TestEnum.Users);//切换数据库
                Console.WriteLine(action.ConnectionString);//打印数据库链接
                action.Fill( 12);
                action.EndTransation();
            }
            Console.WriteLine (AppDebug.Info); // output all SQL statements that are executed.
            AppDebug.Stop ();

Output results:

 

Note: in a transaction, the transaction is detected when open, in order to use the local transaction, and no switching database, instead of using a prefix to perform the operation.

 

 

If the transaction code commented, is a direct link switching database, the output will be as shown below:

 

Note: If there is no open transaction, the switch directly link the database, and eliminates the eradication prefix syntax.

to sum up:

For .NET, Microsoft in addition to providing the less stable MSDTC, does not seem to find other solutions for distributed transactions, and good general project, we can handle local affairs.

Hope Microsoft can do more research, popularization and promotion on some key distributed, providing distributed solutions related projects, do not speak all day to change search on broken heart in those simple additions and deletions.

 

The new year, to re-prostitute, and intend to wandering, not the city, recommended buyer welcome everyone, thank you.

Reproduced in: https: //my.oschina.net/secyaher/blog/274232

Guess you like

Origin blog.csdn.net/weixin_33877092/article/details/91967153