C#中的MySQL连接池

C#中的MySQL连接池

本文介绍了C#中的MySQL连接池的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 C#多线程应用程序,并使用的MySQL 与到整个应用程序的单一连接。但是,当两个或多个线程试图同时访问数据库,然后我得到以下错误: -

I am having C# multi threading application and using MySQL with single connection to whole application. But when two or more thread try to access database at the same time, then i get below error :-

There is already an open DataReader associated with this Connection which must be closed first.

我的连接code低于

My Connection code is below

public static _connectionSetup = new MySqlConnection("Server=server ; Database=database;User ID=user;Password=pass;Pooling=true;");

,当我需要使用连接我使用跌破code: -

and when i need to use connection i am using below code :-

   using (MySqlConnection connection =_connectionSetup )
            {
                using (MySqlCommand command = new MySqlCommand("proc", connection))
                {
                         ....
                }
            }

我试图用池= TRUE 和我有创建两个分离的连接,以及两个不同的线程,但还是我收到上述错误。我失去了一些东西?

i tried used pooling=true and i have create two separated connection as well for two different thread, but still i am getting above error.Am i missing something ?

我如何实现连接池,这样所有的线程将使用单独的连接并不会造成什么问题?

How can i implement connection pool so that all thread will use separate connection and won't cause any issue?

感谢

推荐答案

池是默认打开的,这样你就不会需要一个连接字符串参数。

Pooling is turned on by default, so you don't need that connection string parameter.

不共享的MySqlConnection 实例。就是这样。

Don't share MySqlConnection instances. That's it.

池是不是在你的code实现,它为你做了通过ADO.NET。

Pooling is not something you implement in your code, it's done for you by ADO.NET.

这篇关于C#中的MySQL连接池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 18:39