Recently in the process of doing a project encountered a MySQL in the concurrency initialization problem, the scenario is this:
I set up multiple actions at the same point in the job to access the database update data, and the result throws the following question when creating the connection:
Note that and a DataReader is open, and the Connection is with use exclusively by. DataReader. You cannot execute any commands for the Connection, including creating another DataReader, until the original DataReader I S closed.
The code I created for the link is as follows:
public class DBConnection { private static mysqlconnection _conn; public static mysqlconnection Conn { get { if (_conn = = null) { Mysqlconnection connection = New Mysqlconnection (constvalue.dbconnectionstring); _conn = connection; } return _conn; } } public static Mysqlconnection CreateConnection () { return Conn; } }
Before using the SQL Server database connection did not have any problems, and later find the data found this is a MySQL connection bug.
The official website is described as follows: https://bugs.mysql.com/bug.php?id=7248
MySql.Data.MySqlClient.MySqlException:There is already a open DataReader associated with this Connection which must be C losed first.
That is, only one connection is allowed to be opened to read data at the same time.
The analysis on the StackOverflow is as follows:
You is using the same connection for the and the DataReader
ExecuteNonQuery
. This isn't supported, according to MSDN:
Note that and a DataReader is open, and the Connection is with use exclusively by. DataReader. You cannot execute any commands for the Connection, including creating another DataReader, until the original DataReader I S closed.
This will make it clear that we must create a connection object each time when we are creating the connection. Change the code:
public class DBConnection {public static mysqlconnection Conn { get { mysqlconnection Connection = new Mysqlconnection (constvalue.dbconnectionstring); return connection; } } public static Mysqlconnection CreateConnection () { return Conn; } }
This solves the problem of concurrent access.
Stepping on the pit, the record is to grow.
Mysqlconnection Problems with concurrent connections