与数据库保持一种连接或根据需要打开关闭 [英] keeping one connection to DB or opening closing per need

查看:76
本文介绍了与数据库保持一种连接或根据需要打开关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在以下情况下寻求最佳实践,并给出每种方法都更好的原因.

I looking for a best practice in following case, with reasons why each way is better.

我有一个数据库,其中约有10〜20个客户端应用程序连接到一台主数据库服务器.

I have one DB with about 10~20 client applications that connecting to one main DB server.

在极少数情况下,每分钟从一个客户端到数据库的呼叫大约为200.

There can be about 200 calls from one client to the DB per minute, in really rare cases.

该应用程序是多线程的,每个应用程序大约20个线程.

The application are multithreaded, about 20 threads per application.

这里的最佳实践是,每个应用程序仅与数据库保持一个连接,然后每个应用程序重新使用它.或根据需要的呼叫打开新的连接并快速关闭它们.

What will be best practice here to keep only one connection to the DB per application and reuse it per application . OR opening new connections per needed call and close them fast.

我们正在使用oracle和sql-server.

We are working with oracle and sql-server.

推荐答案

.NET oracle提供程序具有内置的连接池功能.每当您需要数据库连接时,创建一个新的数据库即可完成工作并立即释放它.连接池将负责有效地重用连接.

The .NET oracle provider has built-in connection-pooling capabilities. Whenever you need a DB connection, create a new one do the work and release it immediately. The connection pooling will take care of reusing connections efficiently.

释放连接的最佳方法是通过using构造,即使发生异常,该构造也可以确保释放连接.

The best way to release the connection is through the using construct which will ensure that the connection is disposed, even if exceptions occur.

using(OracleConnection connection = ConnectionFactory.Create())
{
    connection.DoStuff();

} //connection.Dispose() called here.

这篇关于与数据库保持一种连接或根据需要打开关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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