使用 GUID 的无状态调用之间的 DBTransactions

发布于 2024-08-24 15:59:23 字数 1460 浏览 5 评论 0原文

我希望为我的数据库引擎添加事务支持,并提供抽象事务处理,以通过数据库操作命令传递Guids。数据库引擎的运行方式类似于:

private static Database DB;
public static Dictionary<Guid,DBTransaction> Transactions = new ...()
public static void DoDBAction(string cmdstring,List<Parameter> parameters,Guid TransactionGuid)
{
  DBCommand cmd = BuildCommand(cmdstring,parameters);
  if(Transactions.ContainsKey(TransactionGuid))
    cmd.Transaction = Transactions[TransactionGuid];
  DB.ExecuteScalar(cmd);
}
public static BuildCommand(string cmd, List<Parameter> parameters)
{
   // Create DB command from EntLib Database and assign parameters
}
public static Guid BeginTransaction()
{
   // creates new Transaction adding it to "Transactions" and opens a new connection
}
public static Guid Commit(Guid g)
{
   // Commits Transaction and removes it from "Transactions" and closes connection 
}
public static Guid Rollback(Guid g)
{
   // Rolls back Transaction and removes it from "Transactions" and closes connection
}

调用系统的运行方式类似于:

Guid g
try
{
  g = DBEngine.BeginTransaction()
  DBEngine.DoDBAction(cmdstring1, parameters,g)
  // do some other stuff
  DBEngine.DoDBAction(cmdstring2, parameters2,g)
  // sit here and wait for a response from other item
  DBEngine.DoDBAction(cmdstring3, parameters3,g)
  DBEngine.Commit(g)
}
catch(Exception){ DBEngine.Rollback(g);}

这是否会干扰 .NET 连接池(除了连接意外保持打开状态之外)?

EntLib 会保持连接打开直到提交或回滚吗?

I'm looking to add transactional support to my DB engine and providing to Abstract Transaction Handling down to passing in Guids with the DB Action Command. The DB engine would run similar to:

private static Database DB;
public static Dictionary<Guid,DBTransaction> Transactions = new ...()
public static void DoDBAction(string cmdstring,List<Parameter> parameters,Guid TransactionGuid)
{
  DBCommand cmd = BuildCommand(cmdstring,parameters);
  if(Transactions.ContainsKey(TransactionGuid))
    cmd.Transaction = Transactions[TransactionGuid];
  DB.ExecuteScalar(cmd);
}
public static BuildCommand(string cmd, List<Parameter> parameters)
{
   // Create DB command from EntLib Database and assign parameters
}
public static Guid BeginTransaction()
{
   // creates new Transaction adding it to "Transactions" and opens a new connection
}
public static Guid Commit(Guid g)
{
   // Commits Transaction and removes it from "Transactions" and closes connection 
}
public static Guid Rollback(Guid g)
{
   // Rolls back Transaction and removes it from "Transactions" and closes connection
}

The Calling system would run similar to:

Guid g
try
{
  g = DBEngine.BeginTransaction()
  DBEngine.DoDBAction(cmdstring1, parameters,g)
  // do some other stuff
  DBEngine.DoDBAction(cmdstring2, parameters2,g)
  // sit here and wait for a response from other item
  DBEngine.DoDBAction(cmdstring3, parameters3,g)
  DBEngine.Commit(g)
}
catch(Exception){ DBEngine.Rollback(g);}

Does this interfere with .NET connection pooling (other than a connection be accidently left open)?

Will EntLib keep the connection open until the commit or rollback?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

锦上情书 2024-08-31 15:59:23

连接将保持打开状态,直到提交或回滚。正是事务使连接保持打开状态。

它不会影响连接池,只是事务持有的连接不会返回到连接池。

我建议您查看.net TransactionScope。这也许能够满足您的需求,而无需您编写任何此类自定义代码。

The connection will be kept open until a commit or rollback. It is the transaction that is keeping the connection open.

It will not affect connection pooling, other than a connection held by a transaction will not be returned to the connection pool.

I would recommend that you look at the .net TransactionScope. This may be able to meet your needs, without you writing any of this custom code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文