查询数据时挂起

本文关键字:挂起 数据 查询 | 更新日期: 2023-09-27 17:59:29

我使用的源代码如下:

Database db1 = new Database(); //init 1 db connection
db1.BeginTransaction();
//this function used to check exist customer
//in this function, I also use Database db2 = new Database(); db2.Close();
CheckExistCustomer(); 
InsertCustomer(db1, strInsert); //this function worked correct, use db1
if(iErrorCode == ErrorStatus.SUCCESSED)
   db1.CommitTransaction(); 
else db1.RollbackTransaction();

如您所见,我有2个数据库连接。我可以用它们吗?当db2.Close()时,它不会影响到当前的db1,对吧?我应该只使用1 db连接(db1)吗?

当我运行CheckExistCustomer()时,程序会挂起。我不知道为什么。有线索吗?

请告知。

我很感激你的帮助。

查询数据时挂起

您在这段代码中进行事务管理。在事务中,您正在对两个DB执行操作,这称为分布式事务,我猜BeginTransaction()方法不支持分布式事务,请使用TransactionScope类

TransactionScope:避免分布式事务