spring.net 中的事务范围
如果我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种选择是实现一个简单的“事务范围”类,该类显式开始 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.