简体   繁体   English

何时关闭从 Asp.net 到 oracle 数据库的连接

[英]When to close connection to an oracle db from Asp.net

I'm using a asp.net application to retrive my data from oracle db, & return them as json to use them in my android application.我正在使用 asp.net 应用程序从 oracle db 检索我的数据,并将它们作为 json 返回以在我的 android 应用程序中使用它们。

I was wondering if I have to open , close my connection to the db everytime I do a request or if it's better to open my connection once and keep it opened because the application that I'm programming will be Opened for like 24h/7d .我想知道我是否必须在每次发出请求时都打开、关闭与数据库的连接,或者最好打开一次连接并保持打开状态,因为我正在编程的应用程序将像 24h/7d 一样打开。

I've read something about connection pool, but it's a little bit confusing me,我读过一些关于连接池的东西,但它让我有点困惑,

because I don't know if the connection pool will be used as I use a mobile application to ask my asp.net application.因为我不知道是否会使用连接池,因为我使用移动应用程序询问我的 asp.net 应用程序。

Thank you in advance for your replies.预先感谢您的回复。

You have 3 options.您有 3 个选择。

  1. Open and and close the database on every request.在每次请求时打开和关闭数据库。
  2. Open the database on Initialize, close it on Page_UnLoad or on OnError在 Initialize 上打开数据库,在Page_UnLoadOnError上关闭它
  3. Open the database for ever, close it on recycle.永远打开数据库,在回收时关闭它。

We start with the (1), we then optimize if we can with (2), and we avoid the (3).我们从 (1) 开始,如果可以的话,我们会优化 (2),然后避免 (3)。

Why we avoid the 3d option, because we probably may end with conflicts on call if we have many pools, or many threads, and also probably left with many open connections end up with out any free other to use.为什么我们避免使用 3d 选项,因为如果我们有许多池或许多线程,我们可能会以调用冲突结束,并且还可能留下许多打开的连接,最终没有任何可用的其他可用连接。

We need the (1) by default because we always need some extra call for ask something from the database -默认情况下我们需要(1),因为我们总是需要一些额外的调用来从数据库中询问一些东西 -

And we optimize with (2) because at page render, we need usually more than one database call - one shared open connection only for one call can increase the speed at little bit.我们用(2)进行优化,因为在页面渲染时,我们通常需要多个数据库调用 - 一个共享打开连接仅用于一个调用可以稍微提高速度。

I'm assuming your service are stateless.我假设您的服务是无状态的。 Then the connections are automatically terminated after every request and everything that is still in memory is cleared (as long it is not static).然后在每次请求后自动终止连接,并清除仍在内存中的所有内容(只要它不是静态的)。 But nevertheless it would be better if you wrapped everything you do against the database with usings.但是,如果您将针对数据库所做的一切都用 using 包裹起来会更好。 This will automatically close all connections and clear everything.这将自动关闭所有连接并清除所有内容。 eg =>例如 =>

using (System.Data.IDbConnection con = DBFactory.CreateDbConnection(Session))
{
    using (System.Data.IDbCommand cmd = DBFactory.CreateDbCommand(con))
    {

You can even do this when you have multiple queries.当您有多个查询时,您甚至可以这样做。 If you want to connect them , you have to do it with a transaction.如果你想连接它们,你必须通过一个事务来完成。 A transaction keeps the connection open even if you close it.即使您关闭它,事务也会保持连接打开。 You don't have to worry about the speed, because .Net is running a connection pooling in the background and doesn't really close the connections.您不必担心速度,因为 .Net 在后台运行连接池,并没有真正关闭连接。

I hope that helps!我希望这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM