Dispose()和事务 [英] Dispose() and transactions

查看:149
本文介绍了Dispose()和事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这看起来不错吗?我想成为一个好孩子并处理所有事情。

我正在努力确保我的生命周期正确支持

交易部分。


使用(SqlConnection cn = new SqlConnection(" blah blah")

{

cn.open();

使用(SqlTransaction tran = cn.BeginTransaction())

使用(SqlCommand cm = new SqlCommand())

{

cm.Connection = cn;

cm.Transaction = tran;

try

{

//做一些东西

cm.ExecutNonQuery();

tran.Commit();

}

catch(Exception ex )

{

tran.RollBack();

抛出ex;

}

}

解决方案

嗯...如何在你的try块中添加一个finally {}块来关闭你的

连接并将其设置为null。现在你没有处理任何东西,

当你的ref是超出范围。


希望这会有所帮助。


Nick Harris,MCP,CNA,MCSD

董事软件服务
http://www.VizSoft.net

Daniel Billingsley <分贝********** @ NO.durcon.SPAAMM.com>在消息中写道

news:uF ************** @ tk2msftngp13.phx.gbl ...

这看起来不错吗?我想成为一个好孩子并处理所有
并且我正在努力确保我的生命周期正确支持
交易部分。

使用(SqlConnection) cn = new SqlConnection(blah blah)
{
cn.open();
使用(SqlTransaction tran = cn.BeginTransaction())
使用(SqlCommand cm =新的SqlCommand())
{
cm.Connection = cn;
cm.Transaction = tran;
尝试
{
//做一些事情
cm.ExecutNonQuery();
tran.Commit();
}
catch(Exception ex)
{
tran.RollBack();
抛出ex;
}
}



" Daniel Billingsley"< db **** ******@NO.durcon.SPAAMM.com>写信息

news:uF ************** @ tk2msftngp13.phx.gbl。 ..

这看起来是对的吗?我想成为一个好孩子并处理掉所有
而我正试图制作su我有正确的生命周期支持
交易部分。




丹尼尔,这几乎就是我的做法。我的结构有点不同,而b $ b不同。你也不需要前锋。我拿了你的例子

并根据我的方式重新编写它。


使用(SqlConnection cn = new SqlConnection(" blah blah")) {

cn.open();

使用(SqlTransaction tran = cn.BeginTransaction()){

试试{

使用(SqlCommand cm = new SqlCommand()){

cm.Connection = cn;

cm.Transaction = tran;

//做一些东西

cm.ExecutNonQuery();

tran.Commit();

}

} catch {

tran.RollBack();

throw;

}

}

}


- Alan


Nick Harris< nh ***** @ VizSoft.net>写道:

嗯...如何在你的try块中添加一个finally {}块来关闭你的
连接并将其设置为null。现在你没有处理任何东西,
当你的裁判超出范围时,你的GC就会这样做。




否 - 请注意&no吨;使用"块,*执行隐式执行Dispose。

连接,事务和命令都被处理掉了。设置

将变量设置为null也是不必要的,因为它们无论如何都会落在

范围内。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复群组,请不要给我发邮件


Does this look right? I want to be a good boy and dispose of everything and
I''m trying to make sure I have the lifecycles right to support the
transactional part.

using (SqlConnection cn = new SqlConnection("blah blah")
{
cn.open();
using (SqlTransaction tran = cn.BeginTransaction())
using (SqlCommand cm = new SqlCommand())
{
cm.Connection = cn;
cm.Transaction = tran;
try
{
// do some stuff
cm.ExecutNonQuery();
tran.Commit();
}
catch(Exception ex)
{
tran.RollBack();
throw ex;
}
}

解决方案

umm... how about adding a finally{} block to your try block to close your
connection and set it to null. Right now your not disposing of anything,
your letting GC do it when your ref''s are out of scope.

Hope this helps.

Nick Harris, MCP, CNA, MCSD
Director of Software Services
http://www.VizSoft.net
"Daniel Billingsley" <db**********@NO.durcon.SPAAMM.com> wrote in message
news:uF**************@tk2msftngp13.phx.gbl...

Does this look right? I want to be a good boy and dispose of everything and I''m trying to make sure I have the lifecycles right to support the
transactional part.

using (SqlConnection cn = new SqlConnection("blah blah")
{
cn.open();
using (SqlTransaction tran = cn.BeginTransaction())
using (SqlCommand cm = new SqlCommand())
{
cm.Connection = cn;
cm.Transaction = tran;
try
{
// do some stuff
cm.ExecutNonQuery();
tran.Commit();
}
catch(Exception ex)
{
tran.RollBack();
throw ex;
}
}



"Daniel Billingsley" <db**********@NO.durcon.SPAAMM.com> wrote in message
news:uF**************@tk2msftngp13.phx.gbl...

Does this look right? I want to be a good boy and dispose of everything and I''m trying to make sure I have the lifecycles right to support the
transactional part.



Daniel, this is almost exactly how I do it. Mine is structured a little bit
differently. Also you don''t need the ex on the throw. I took your example
and redid it based on the way that I do it.

using (SqlConnection cn = new SqlConnection("blah blah") {
cn.open();
using (SqlTransaction tran = cn.BeginTransaction()) {
try {
using (SqlCommand cm = new SqlCommand()) {
cm.Connection = cn;
cm.Transaction = tran;
// do some stuff
cm.ExecutNonQuery();
tran.Commit();
}
} catch {
tran.RollBack();
throw;
}
}
}

-- Alan


Nick Harris <nh*****@VizSoft.net> wrote:

umm... how about adding a finally{} block to your try block to close your
connection and set it to null. Right now your not disposing of anything,
your letting GC do it when your ref''s are out of scope.



No - note the "using" blocks, which *do* perform a Dispose implicitly.
The connection, transaction and command are all being disposed. Setting
the variables to null would be unnecessary, too, as they''d fall out of
scope anyway.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


这篇关于Dispose()和事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆