Mysqlconnection Problems with concurrent connections

Source: Internet
Author: User
Tags mysql in

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.