you must close the connection even is broken.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
if (!General.db.IsConnected)
{
General.log("[write2db] dbase is not connected");
if (General.db != null)
{
General.log("[write2db] dbase is not null");
try
{
//https://stackoverflow.com/a/31174539/1320686
General.db.GetConnection().Close();
}
catch (Exception x) {
General.log("[write2db] dbase is not null, tried to close but error occurred : " + x.Message);
}
}
General.log("[write2db] trying to open dbase");
/* READ CONFIG + CONNECT */
string db_res = connect2db();
if (db_res.Length > 0)
{
General.log("[write2db] trying to open dbase but error occurred : " + db_res);
MessageBox.Show(db_res, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
Environment.Exit(-1);
return;
}
/* READ CONFIG + CONNECT */
General.log("[write2db] connection success!");
}
//proper way to check if is connected
public bool IsConnected
{
//src - https://stackoverflow.com/a/34357667/1320686
if (objConn == null | !objConn.State.HasFlag(ConnectionState.Open)) //INVALID ==> .State != ConnectionState.Open)
{
return false;
}
else
{
return true;
}
}
Possible values for ConnectionState
js //https://msdn.microsoft.com/en-us/library/system.data.connectionstate(v=vs.110).aspx Broken - The connection to the data source is broken. This can occur only after the connection has been opened. A connection in this state may be closed and then re-opened. Closed - The connection is closed. Connecting - The connection object is connecting to the data source. Executing - The connection object is executing a command. Fetching - The connection object is retrieving data. Open - The connection is open.
origin - http://www.pipiscrew.com/?p=13292 mysql-max-pool-size-was-reached