spring.net 中的事务范围

发布于 2024-08-01 12:50:36 字数 511 浏览 10 评论 0原文

如果我在 Spring.Net 中使用基于属性的事务,看起来像

[Transaction()]
public void MyBusinessMethod()
{
   // doing some in memory calculation, may take some time
   Prepare();

   // actual db access code
   DbAccess();
}

在该方法中,Prepare() 将做一些准备工作,而不涉及数据库层。 为了获得最佳性能,应在 DbAccess() 之前打开数据库连接。 但是,由于我使用 AOP 进行事务并且它应用于方法级别,这是否意味着调用方法 MyBusinessMehtod 时将打开连接? 如果是的话有什么办法可以改善吗? 我能想到的一种方法是将 DbAccess 分解为它自己的方法。 除此之外,还有其他好的推荐吗?

If I am using attribute based transaction in Spring.Net, something looks like

[Transaction()]
public void MyBusinessMethod()
{
   // doing some in memory calculation, may take some time
   Prepare();

   // actual db access code
   DbAccess();
}

In the method, Prepare() will do some preparation work without involving the database layer. For optimum performance, the db connection should be opened just before DbAccess(). However, since I am using AOP to do transaction and it applies at method level, does it means that the connection will be opened when the method MyBusinessMehtod is called? If it is, is there any way to improve that? One way I can think of is to factor the DbAccess out to its own method. Besides that, any other good recommendations?

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

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

发布评论

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

评论(1

铁轨上的流浪者 2024-08-08 12:50:36

一种选择是实现一个简单的“事务范围”类,该类显式开始 Spring 事务(使用编程事务管理)。 这还允许对事务会话进行额外的管理,例如触发验证逻辑等。

另一个(更简单的)选项是重构您的方法,以便从特定于事务的方法调用 DbAccess(您可能在那里分组了其他事务工作)也)或将属性移至 DbAccess。

One option is to implement a simple "transaction scope" class that explicitly begins the Spring transaction (using programmatic transaction management). This would also allow additional management of the transactional session, such as triggering validation logic, etc.

Another (simpler) option is to either refactor your method such that DbAccess is called from a transaction-specific method (you may have other transactional work grouped there as well) or move the attribute to DbAccess.

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