codingdir logo sitemap sitemap |
Home
PHP
C#
C++
ANDROID
JAVA
JAVASCRIPT
PYTHON

MySqlDataReader close connection


By : , Category : c#

The best coding for this is the following

using(MySqlConnection connect = new MySqlConnection(connectionStringMySql))
using(MySqlCommand cmd = new MySqlCommand())
{
    string commandLine = "SELECT id,token FROM Table WHERE id = @id AND token = @token;";
    cmd.CommandText = commandLine;

    cmd.Parameters.AddWithValue("@id", id);
    cmd.Parameters.AddWithValue("@token", token);

    cmd.Connection = connect;
    cmd.Connection.Open();

    using(msdr = cmd.ExecuteReader())
    {

         //do stuff.....
    } // <- here the DataReader is closed and disposed.

}  // <- here at the closing brace the connection is closed and disposed as well the command

The using statement will keep your connection closed and disposed as well as the command object. There is no need to call explicitily Close on MySqlConnection or MySqlDataReader because the using statement will do this for you ALSO in case of Exceptions triggered in your code between the open and close of the connection

In your original code it is enough to close the connection just one time with the command, datareader or the connection itself, because they work with the same object instance, but if you have an exception then your code fails to close the connection and thus you risk the 'too many connections open' problem

ReLated :

Finally I figure out the solution to the problem Memory of the rabbitmq server was full. It started working as soon as i cleared it.

If you're sharing the connection between your threads this can cause problems. pika is not thread safe and connections shouldn't be used by different threads.

First bit of the FAQ:

Q:

Is Pika thread safe?

A:

Pika does not have any notion of threading in the code. If you want to use Pika with threading, make sure you have a Pika connection per thread, created in that thread. It is not safe to share one Pika connection across threads.

I will suggest a mix of Option 1: and Option 2:

By default the connection is closed when the script execution but is not bad idea to close the connection at the end of the script of when you think you do not need it anymore.

Also if you have a script which will take some time to finish executing you must close the connection and open it again if you need it because in heavy traffic the MySQL server will reach it's connection limit.

Serial connections (meaning the physical connection or port object) never timeout, even if you unplug the cable. However, if the serial port is a USB converter, the device can be disabled if the USB side is unplugged which will break the serial connection.

SerialPort is IDisposable, so if you dispose it, it will close.

Serial Timeouts only occur if you do a Read and there is no data. This has no affect on the SerialPort device or connection. (Serial Timeouts can also occur on Writes, but this is much less common. This only happens if handshaking is involved and the receiver is blocking reception.)

Comments


Message :
Login to Add Your Comments .
How to disable registered OpenCL platforms on Windows?
Is Observable broken in Angular 2 Beta 3?
Cross-thread operation not valid when using Invoke
How to pass an IEnumerable or queryable list of properties from Controller to View
Finding numbers after a certain keyword using Python
Pocketsphinx recognizes random phrases in a silence
Passing non-thread-safe objects through thread-safe containers
React scroll nav
BizTalk WCF-BasicHttp Adapter does not allow Empty string for Service Certificate Props
Why property ''cause" of Exception is repeating forever?
Privacy Policy 2017 © codingdir.com All Rights Reserved .