gpt4 book ai didi

c# - SQL Server 2005 上的 ADO.NET 2.0 事务是否在出现异常时自动回滚?

转载 作者:太空宇宙 更新时间:2023-11-03 22:27:30 24 4
gpt4 key购买 nike

如果我们开始一个事务,然后执行一些数据库操作,比如插入,然后发生异常,如果我不在捕获/异常处理程序中调用 transaction.rollback(),事务会自动回滚吗?

即使事务被回滚,是否会导致内存泄漏,即事务对象和连接对象以其原始状态在内存中徘徊,直到垃圾收集器启动?


CREATE TABLE [Friend]

(
[ID] [int] IDENTITY(1,1) NOT NULL,
[FullName] [varchar](50) NOT NULL,
[Phone] [varchar](50) NULL,
)

客户:

namespace Client
{
class Program
{
static void Main(string[] args)
{
DaContract Impl = new Impl();
Impl.AddFriend(new Friend("Someone", "100"));
Impl.AddFriend(new Friend("Someone else"));
Console.ReadLine();
}
}
}

服务器:

using System;
using MyLib.DataAccess;
using System.Data;

namespace TestTransactionAndConnectionStateOnException
{
public class Friend
{
public string FullName;
public string Phone;

public Friend(string fullName): this(fullName, null) {}
public Friend(string fullName, string phone)
{
this.FullName = fullName;
this.Phone = phone;
}
}

public interface DaContract
{
int AddFriend( Friend f );

int UpdatePhone(string fullName, string phone);
}

public class Impl: DaContract
{
Mediator _m;
public Impl() { this._m = new Mediator(); }

public int AddFriend( Friend f )
{
int ret = 0;

try
{
ret = this._m.AddFriend( f );
}
catch(Exception ex)
{
HandleException(ex);
}

return ret;
}

public int UpdatePhone(string fullName, string phone)
{
int ret = 0;

try
{
ret = this._m.UpdatePhone(fullName, phone);
}
catch(Exception ex)
{
HandleException(ex);
}

return ret;
}

public void HandleException(Exception ex)
{
/* see the transaction state */

/* see connection state */

/* do nothing and let the client call another method to initiate a new
* transaction and a new connection */

}
}

public class Mediator
{
private string _connectionString = "Data Source=localhost;Integrated Security=SSPI;Initial Catalog=MyFriends";
private Manager _m = new Manager();

public int AddFriend( Friend f )
{
int ret = 0;
using (ISession session = SessionFactory.Create(SessionType.Ado, this._connectionString))
{
session.BeginTransaction();
ret = this._m.AddFriend(f, session);
session.CommitTransaction();
}

return ret;
}

public int UpdatePhone(string fullName, string phone)
{
int ret = 0;
using (ISession session = SessionFactory.Create(SessionType.Ado, this._connectionString))
{
session.BeginTransaction();
ret = this._m.UpdateFriend(fullName, phone, session);
session.CommitTransaction();
}

return ret;
}
}

public class Manager
{
public int AddFriend(Friend f, ISession session) { return Handler.Instance.AddFriend(f, session); }
public int UpdateFriend(string fullName, string phone, ISession session) { return Handler.Instance.UpdatePhone(fullName, phone, session); }
}

public class Handler
{
private static Handler _handler = null;
private Handler() {}
public static Handler Instance
{
get
{
if (_handler == null)
_handler = new Handler();

return _handler;
}
}

public int AddFriend( Friend f, ISession session )
{
/* check session, transaction and connection states here */
Console.WriteLine(session.Connection.State.ToString());

int ret = 0;

/* Add the friend */
IDbCommand command = session.CreateCommand();
command.CommandType = CommandType.Text;
command.CommandText = string.Format("INSERT INTO Friend(FullName, Phone) values('{0}', '{1}')", f.FullName, f.Phone);
ret = command.ExecuteNonQuery();

/* throw an exception just for the heck of it (don't dispose off ISession yet) */
if (string.Compare(f.FullName, "Someone", true) == 0)
throw new Exception("Fake exception. Friend value can't be 'Someone'");

return ret;
}

public int UpdatePhone(string fullName, string phone, ISession session )
{
return 0;
}

}
}

由于字数限制,我无法在评论部分发布代码,因为它搞砸了所有格式。

最佳答案

如果事务没有提交就被释放,事务将被回滚。所以只要你执行一个 using() 来开始你的事务,或者有一个调用 Dispose() 的 finally block ,它就会在异常情况下自动回滚。

如果您不处理事务/连接,它最终会被垃圾收集器回收,但会一直保留在内存中直到那时。 (事实上​​,托管的 SqlTransaction 对象将在内存中徘徊,直到 GC 无论如何启动;但处置确保非托管事务/连接资源的早期清理,释放服务器端资源并释放连接以供重用。)

关于c# - SQL Server 2005 上的 ADO.NET 2.0 事务是否在出现异常时自动回滚?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/790105/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com