连接管理ASP.net [英] Connection Management ASP.net

查看:93
本文介绍了连接管理ASP.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在你的ASP.Net应用程序管理数据库连接?

How do you manage your database connections in your ASP.Net application?

我的理解告诉我最好的方法是打开一个连接,进行查询,关闭连接 - 而做到这一点多次,因为连接池,使成本可以忽略不计

My understanding tells me the "best" way is to open a connection, make a query, close the connection - and do that multiple times because connection pooling makes the cost negligable.

在问题出现时,我有一个DAL,每个方法查找自己的连接后。例如,

The problem comes when I have a DAL where each method looks after its own connection. E.G.


User x = DAL.GetUserDetails();
Customer y = DAL.GetCustomer();

这是罚款,直到我们开始谈论的TransactionScope

This is fine until we start talking about TransactionScope


using (TransactionScope t....
{
DAL.UpdateCustomer(y);
DAL.UpdateUser(x);
t.Complete();
}

ASP.Net想现在使用DTC,因为(我猜),但它牵涉到多个连接。

ASP.Net wants to now use DTC because (I'm guessing) there are multiple connections involved.

有人会说缓存连接somwhere但我需要显式地销毁,因为我是管理安全的方式连接(EXECUTE AS /复原),我不希望有使每一个电话页面这样做,因为有人会忘记拨打电话。我还可以通过连接到每一个方法,但是那并不理想,因为该页面有管理的连接。

Someone is going to say "Cache the connection somwhere" but I need to explicitly destroy the connection because of the way I am managing security (execute as / revert) and I don't want to have to make a call on every page to do that because someone will forget to make the call. I could also pass the connection into each method but thats not ideal because the page has to manage a connection.

我是不是决策意识还是我错过了一些基本的东西在这里?

Am I making sense or have I missed something fundamental here?

推荐答案

我在读书的地方,我不记得在那里,微软是要解决,当你有两个连接到同一个数据库,他们不会升级到DTC这将使这个问题消失。

I was reading somewhere and I don't remember where that Microsoft was going to address that when you have two connections to the same DB that they would not escalate to DTC which would make this problem go away.

在那之前我们所做的是为了发展我们的TransactionScope,我们达尔斯会再问问TS一个新的连接,而当我们设置在TS,将关闭连接。

Until then what we did was to develop our TransactionScope, our DALs would then ask the TS for a new connection, and when we disposed the TS it would close the connection.

的连接存储在LogicalCallContext,虽然我会看看使用HTTP上下文来代替。我离开了公司之前的应用程序去住,但是从我所听到的,他们都没有任何问题。

The connections were stored in LogicalCallContext, although I would look at using HTTP Context instead. I left the company before the app went live but from what I've heard they haven't had any issues.

所以你必须

using (CustomTS.New())
{
 CustomerDal.Update()
 userDal.Update()
}

CustomTS有一个静态方法,它会获得当前的事务和连接。

CustomTS had a static method that would get the current transaction and connection.

这篇关于连接管理ASP.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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