TransactionScope 函数

发布于 2024-11-30 04:45:55 字数 1179 浏览 2 评论 0 原文

从事 .net 中的事务处理。有关于通过子功能流动交易的问题。如果对象上下文在子方法中是通用的,我是否需要使用依赖事务?

例如,在下面的代码中 - 我在类的构造函数中声明对象上下文(不确定这是否是最佳实践)

public class EmployeeRepository
{
    private EmployeeContext ec;        
    public EmployeeRepository()
    {
        objectContext = new EmployeeContext();            
    }       
    public InitTransaction(EmployeeEntity emp1)
    {
         using (TransactionScope transaction = new TransactionScope())
        {
            try
            {   ec.employees.AddObject(emp1);
                SubFunction1();
                ec.SaveChanges();                               
            }
            catch
            {
                //catch 
            }
        }
        //commit the transaction here 
        ec.AcceptAllChanges();
    }

    public SubFunction1()
    {
        //some processing 
        //using same object context
        ec.someother.AddObject(someobject);
        ec.SaveChanges();
    }
}

我希望子函数也成为事务的一部分? 在这种情况下,即使我使用相同的对象上下文,我是否应该在 SubFunction1 中使用依赖事务?或者我应该在 SubFunction1 中添加一个

using (TransactionScope transaction = new TransactionScope());

。正确方向的指针将不胜感激。

Working on Transactions in .net. Had a question on flowing transactions through sub functions. Do I need to use dependent transactions if the object context is common across the sub - methods?

For example, in the following code - I declare the object context in the constructor of my class (not sure if this is best practice)

public class EmployeeRepository
{
    private EmployeeContext ec;        
    public EmployeeRepository()
    {
        objectContext = new EmployeeContext();            
    }       
    public InitTransaction(EmployeeEntity emp1)
    {
         using (TransactionScope transaction = new TransactionScope())
        {
            try
            {   ec.employees.AddObject(emp1);
                SubFunction1();
                ec.SaveChanges();                               
            }
            catch
            {
                //catch 
            }
        }
        //commit the transaction here 
        ec.AcceptAllChanges();
    }

    public SubFunction1()
    {
        //some processing 
        //using same object context
        ec.someother.AddObject(someobject);
        ec.SaveChanges();
    }
}

I want the subfunctions to be a part of the transactions as well?
In this case should I use a dependent transaction within SubFunction1 even though I am using the same object context? Or Should I add a

using (TransactionScope transaction = new TransactionScope());

within SubFunction1. Pointers in the right direction would be greatly appreciated.

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

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

发布评论

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

评论(2

终陌 2024-12-07 04:45:55

事务范围可以嵌套(它们的工作方式类似于 SQL @@TRANCOUNT 机制),因此理论上您可以在存储库中使用 TransactionScopes,例如保持父:子表关系 ACID,而且也可以在业务/服务层中使用(例如,跨多个实体、可能跨多个数据库、甚至跨其他资源(例如消息队列和事务性文件系统

。 com/b/dbrowne/archive/2010/05/21/using-new-transactionscope-considered-harmful.aspx" rel="nofollow noreferrer">默认隔离级别TransactionScope 是可串行读取的 - 这可能会导致锁定/死锁。

Transaction Scopes can be nested (they work similar to the SQL @@TRANCOUNT mechanism), so you could in theory use TransactionScopes in your Repository, e.g. to keep parent : child table relationships ACID, but also in your Business / Service layers as well (e.g. to have a Distributed Transaction across multiple entities, possible across multiple Databases, and even across other resources such as Message Queues and Transactional file systems.

Note that the default isolation level of TransactionScope is Read Serializable - this can lead to locking / deadlocks.

御弟哥哥 2024-12-07 04:45:55

您可以考虑使用依赖注入来传递相同的ObjectContext,这样就可以避免TransactionScope

不是在存储库中创建Context,而是通过构造函数注入它。

public class EmployeeRepository
{
    private EmployeeContext ec;        
    public EmployeeRepository(EmployeeContext objectContext)
    {
        ec = objectContext;            
    }       

}

看看 这个答案

You can consider using Dependency Injection to pass around the same ObjectContext so you can avoid the TransactionScope.

Instead of creating Context inside the Repository inject it through constructor.

public class EmployeeRepository
{
    private EmployeeContext ec;        
    public EmployeeRepository(EmployeeContext objectContext)
    {
        ec = objectContext;            
    }       

}

Take a look at this answer

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