Пишу простой опросник тсп портов.
Но вот стала валиться ошибка System.Exception: Timeout while getting a connection from pool.
либо же
A timeout has occured. If you were establishing a connection, increase Timeout value in ConnectionString. If you were executing a command, increase the CommandTimeout value in ConnectionString or in your NpgsqlCommand object.
Причем то все ок, то ошибка валится. Кто сталкивался?
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.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
{
try
{
while (doWork)
{
// Create a TcpClient.
// Note, for this client to work you need to have a TcpServer
// connected to the same address as specified by the server, port
// combination.
TcpClient client = new TcpClient(Server, Port);
// Translate the passed message into ASCII and store it as a Byte array.
Byte[] data = Encoding.UTF8.GetBytes(msg);
// Get a client stream for reading and writing.
NetworkStream stream = client.GetStream();
// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);
// Receive the TcpServer.response.
// Buffer to store the response bytes.
data = new Byte[256];
// String to store the response ASCII representation.
// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(data, 0, data.Length);
value = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
value = value.Substring(0, value.Length - 1);
stream.Close();
client.Close();
if (value[0] == '$' && value.Length <= 13)
{
NpgsqlConnection conn_ = new NpgsqlConnection(("Server=" + list[0] + ";Port=" + list[1] + ";User Id=" + list[2] + ";Password=" + list[3] + ";Database=" + list[4] + ";Timeout=300; CONNECTIONLIFETIME=300; CommandTimeout = 300 "));
if (conn_.State == System.Data.ConnectionState.Closed) conn_.Open();
if (ParseGlob != "") Mystr = ("INSERT INTO " + list[5] + "(\"F_TagName_ID\"," + list[9] + ",\"F_Date\") VALUES (" + IdCom.ToString() + ",(SELECT * FROM " + ParseGlob + "(\'" + value + "\')),\'" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\' )");
else Mystr = ("INSERT INTO " + list[5] + "(\"F_TagName_ID\"," + list[9] + ",\"F_Date\") VALUES (" + IdCom.ToString() + ",\'" + value + "\',\'" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\' )");
logger.Trace(Mystr);
NpgsqlCommand command = new NpgsqlCommand(Mystr, conn_);
command.CommandTimeout = 20;
int rowsaffected;
try
{
rowsaffected = command.ExecuteNonQuery();
}
catch (Exception msg2)
{
logger.Trace(msg2);
}
if (conn_.State == System.Data.ConnectionState.Open)
conn_.Close();
wh.WaitOne();
}
else
{
logger.Debug("port_DataReceived " + value);
value = "";
return;
}
}
}
catch (Exception msg)
{
logger.Error(msg);
return;
}
}