ADO.Net最佳实践 - 单个VS多个连接进行异步分贝呼叫时多个、分贝、ADO、Net

2023-09-05 00:58:03 作者:了了无期

我使用的ADO.Net连接到某些SQL Server和Oracle数据库,我要运行一些我的查询兼任。

I am using ADO.Net for connecting to some Sql Server and Oracle databases, and I want to run some of my queries concurrently.

我使用的SqlClient命名空间的类为SQL Server和ODP.Net甲骨文。对于SQL Server我说MARS选项连接字符串,并调用异步的API上的SqlCommand。 ODP.Net不提供异步API,所以我必须给我的并发Oracle命令单独的线程。

I use classes from SqlClient namespace for Sql Server and ODP.Net for Oracle. For Sql Server I added MARS option to the connection string, and call asynchronous APIs on SqlCommand. ODP.Net does not provide asynchronous API, so I have to give my concurrent Oracle commands separate threads.

我的问题是,我应该如何处理连接对象?我应该创建每个每个数据库实例一个的DbConnection和针对单个连接异步执行的命令,或者我应该给一个单独的连接对象,以我的每个并发命令?是否共享连接对象成为争点,通过它同时执行多个命令?

My question is, how should I handle connection objects? Should I create one DbConnection per each database instance and execute commands asynchronously against a single connection, or should I give a separate connection object to each of my concurrent commands? Does a shared connection object become a contention point for multiple commands executing through it simultaneously?

我会写一些比较测试,但喜欢从别人谁与异步数据库命令的经验听到。预先感谢您!

I will write some comparison tests but would love to hear from somebody who has experience with asynchronous database commands. Thank you in advance!

推荐答案

前段时间我们面对这个问题,并选择了一个单独的连接对象来处理每个并发命令。这是使得很多使用数据库的应用程序(每个页面有执行大约40查询)。我们看到,这是因为连接建立速度很慢。

Some time ago we faced this issue and chose to handle a separate connection object to each of the concurrent commands. It was an application making a lot of use of the database (for each page there were around 40 queries executed). We saw this was very slow because of the connection creation.

所以,我们把它改成使用执行的每一个命令一个连接(单身)。这个固定的问题,我们很高兴看到该应用程序响应速度更快。但是,应用程序开始种植和交易的需求是迫切的,但我们所面临的问题,这是不可能在我们的模型。我们最终采用的混合:每当需要连接的事务,我们创建一个新的,如果是不需要交易那么我们重用了已经在单身中创建一个

So, we changed it to a single connection (a singleton) used by every command executed. This fixed the issue and we were happy to see the application was responding faster. However, the application began growing and the need of transactions was urgent, but we faced the problem that this was not possible in our model. We ended using a mix: whenever the connection needed transactions we created a new one, if no transactions were needed then we reused the one that had been created in the singleton.

我现在会做的是使用单个连接,并使用内部存储过程称为交易模式;避免具有至处理事务的应用程序服务器上。

What i would do now is to use a single connection and use a transaction pattern inside the stored procedures called; avoiding having to handle the transaction on the application server.

希望这是有益的。

 
精彩推荐
图片推荐