简体   繁体   English

与MySQL和ASP.NET MVC合并

[英]Pool with MySQL and ASP.NET MVC

I am developing a web application with ASP.NET and MySQL, but I am not using the SqlClient provider. 我正在使用ASP.NET和MySQL开发Web应用程序,但是我没有使用SqlClient提供程序。

I read somewhere (I do not remember where) that if I need to establish a connection pool I need to add some lines to file web.config . 我读过某处(我不记得在哪里),如果我需要建立连接池,则需要在文件web.config添加一些行。

To make a connection, defined in each model there is a connection string, something like: 为了建立连接,每个模型中都定义了一个连接字符串,例如:

string sqlConnection = 
    "server=localhost; user id=root; password=***; database=test; pooling=true;Min Pool Size=0; Max Pool Size=60;";

And to connect and do query I do the following: 要进行连接并进行查询,请执行以下操作:

MySqlConnection conection;
conection = new MySqlConnection(sqlConnection);
string query = "SELECT COUNT(*) FROM documents WHERE user_id = ?user_id;
MySqlCommand cmd = new MySqlCommand(query, conection);`

cmd.Connection.Open();
cmd.Prepare();
cmd.Parameters.Add("?user_id", userID);
cmd.ExecuteReader();
...
cmd.Connection.Close();

But I want to open a connection pool and use pooled connections, I do not want to open and close a connection every time. 但是我想打开一个连接池并使用池化的连接,我不想每次都打开和关闭一个连接。

I put this in the web.config file: 我把它放在web.config文件中:

<connectionStrings>
    <remove name="LocalMySqlServer"/>
    <add name="LocalMySqlServer" 
         connectionString="Data Source=localhost; userid=root;  password=***; database=test; Pooling=true; Min Pool Size=50; Max Pool Size=100;"  
         providerName="MySql.Data.MySqlClient"/>
</connectionStrings>

But when I start the development server, I do not see any connections in MySQL Workbench . 但是,当我启动开发服务器时,在MySQL Workbench中看不到任何连接。

This only happens when I call the model and open a connection. 这仅在我调用模型并打开连接时发生。

What is the way to have connection pooling at startup time? 在启动时如何建立连接池?

I think you're misunderstanding the concept of connection pooling. 我认为您误解了连接池的概念。

Connection pooling is a mechanism provided by the database driver or provider. 连接池是数据库驱动程序或提供程序提供的一种机制。 You still have to ask for a connection but instead of the driver/provider creating a whole new connection each time you create a connection, it maintains a pool of ready made warmed up objects and hands you one of them. 您仍然需要请求连接,但是驱动程序/提供程序无需在每次创建连接时都创建一个全新的连接,而是维护一个现成的预热对象池,并让您将其中的一个交给您。

This all happens silently under the covers. 这一切都在幕后悄悄进行。

Update: 更新:

To consume a connection string from your web.config : 要使用web.config的连接字符串:

string sqlConnection = 
   ConfigurationManager.ConnectionStrings["LocalMySqlServer"].ConnectionString;

You might have to add a reference to System.Configuration : 您可能必须添加对System.Configuration的引用:

在此处输入图片说明

And add a: 并添加:

using System.Configuration;

To the top of your class file. 在类文件的顶部。

That's for a .NET 4.0 project, but the principle is the same with .NET 2.0. 那是一个.NET 4.0项目,但是原理与.NET 2.0相同。

You can try to change Min Pool Size=0 to something larger. 您可以尝试将“最小池大小” = 0更改为更大的值。 This should ensure that always X number of connections are available in the pool. 这应确保池中始终有X个连接可用。

尝试:

System.Configuration.ConfigurationManager.ConnectionStrings["LocalMySqlServer"]

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

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