保持一个连接到数据库或开扩收盘每需要 [英] keeping one connection to DB or openning closing per need

查看:105
本文介绍了保持一个连接到数据库或开扩收盘每需要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下列情况下,为何每次的方式是更好的寻找最佳实践。

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.

有可能从一个客户,每分钟的DB约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.

什么将是最好的做法在这里只保留一个到每个应用程序的数据库连接,每个aplication重用。或打开新的连接每个需要调用和关闭它们很快。

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

我们正在与Oracle和SQL服务器。

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.

要释放连接,最好的办法就是通过使用结构,这将确保连接设置,即使发生异常。

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天全站免登陆