程序在 psql delete 语句后卡住
program get stuck after psql delete statement
我的程序使用 ssh (ssh.net) 连接到主机,使用数据提供程序 npgsql 连接到 psql 数据库并删除具有给定 ID 的条目。
它按原样删除了条目,但程序在此之后至少卡住了 1 分钟。使用 using 语句,我认为在 sql 命令之后连接应该已经断开,但我也没有收到为什么需要这么长时间的错误。
有什么事情我忘记了,必须等待超时吗?
SshClient client = new SshClient(host, username, password);
尝试
{
//SSH连接
client.Connect();
如果(client.IsConnected)
{
scrLog.Content += "SSH connection successfull";
scrLog.Content += Environment.NewLine;
}
别的
{
scrLog.Content += "SSH connection failed";
scrLog.Content += Environment.NewLine;
}
//Portforwarding to Port 5432 (postgresql)
var portFwdL = new ForwardedPortLocal(psqlBoundHost, psqlUPort, psqlHost, psqlUPort);
client.AddForwardedPort(portFwdL);
portFwdL.Start();
if (portFwdL.IsStarted)
{
scrLog.Content += "Portforwarding started";
scrLog.Content += Environment.NewLine;
}
else
{
scrLog.Content += "Portforwarding failed";
scrLog.Content += Environment.NewLine;
}
//Connection to Database with a Delete Command
try
{
string connstring = String.Format("Server={0};Port={1};" +
"User Id={2};Password={3};Database={4}",
psqlBoundHost, psqlPort, sqluser, sqlpass, database);
using (NpgsqlConnection conn = new NpgsqlConnection(connstring))
{
conn.Open();
string sql1 = "delete from tblTest where testId = '" + nr + "';";
//string sql2 = "\q";
NpgsqlCommand cmd1 = new NpgsqlCommand(sql1, conn);
cmd1.ExecuteNonQuery();
scrLog.Content += sql1;
scrLog.Content += Environment.NewLine;
//NpgsqlCommand cmd2 = new NpgsqlCommand(sql2, conn);
//cmd2.ExecuteNonQuery();
//scrLog.Content += sql2;
//scrLog.Content += Environment.NewLine;
}
}
catch (Exception ex)
{
MessageBox.Show("Error with the psql-connection:"
+ Environment.NewLine
+ ex.Message);
}
client.Disconnect();
scrLog.Content += "SSH connection closed";
scrLog.Content += Environment.NewLine;
}
catch (Exception ex)
{
MessageBox.Show("Error with the ssh-connection:"
+ Environment.NewLine
+ ex.Message);
}
我尝试使用
\q
命令最后,但它总是给我错误“42601:“\”处或附近的语法错误”,我读到它不需要。
有人可以帮忙吗?
经过研究和测试不同的场景,我终于找到了超时。我需要禁用连接池,如果再次需要,连接在关闭后仍处于打开状态。
我只是用 Pooling=false
.
禁用了它
string sqlpool = "false";
string sqlconn = String.Format("Server={0};Port={1};User Id={2};Password={3};Database={4};Pooling={5};",
sqlserver, sqlport, sqluserid, sqlpass, sqldatabase, sqlpool);
我的程序使用 ssh (ssh.net) 连接到主机,使用数据提供程序 npgsql 连接到 psql 数据库并删除具有给定 ID 的条目。
它按原样删除了条目,但程序在此之后至少卡住了 1 分钟。使用 using 语句,我认为在 sql 命令之后连接应该已经断开,但我也没有收到为什么需要这么长时间的错误。
有什么事情我忘记了,必须等待超时吗?
SshClient client = new SshClient(host, username, password);尝试 { //SSH连接 client.Connect(); 如果(client.IsConnected) { scrLog.Content += "SSH connection successfull"; scrLog.Content += Environment.NewLine; } 别的 { scrLog.Content += "SSH connection failed"; scrLog.Content += Environment.NewLine; }
//Portforwarding to Port 5432 (postgresql) var portFwdL = new ForwardedPortLocal(psqlBoundHost, psqlUPort, psqlHost, psqlUPort); client.AddForwardedPort(portFwdL); portFwdL.Start(); if (portFwdL.IsStarted) { scrLog.Content += "Portforwarding started"; scrLog.Content += Environment.NewLine; } else { scrLog.Content += "Portforwarding failed"; scrLog.Content += Environment.NewLine; } //Connection to Database with a Delete Command try { string connstring = String.Format("Server={0};Port={1};" + "User Id={2};Password={3};Database={4}", psqlBoundHost, psqlPort, sqluser, sqlpass, database); using (NpgsqlConnection conn = new NpgsqlConnection(connstring)) { conn.Open(); string sql1 = "delete from tblTest where testId = '" + nr + "';"; //string sql2 = "\q"; NpgsqlCommand cmd1 = new NpgsqlCommand(sql1, conn); cmd1.ExecuteNonQuery(); scrLog.Content += sql1; scrLog.Content += Environment.NewLine; //NpgsqlCommand cmd2 = new NpgsqlCommand(sql2, conn); //cmd2.ExecuteNonQuery(); //scrLog.Content += sql2; //scrLog.Content += Environment.NewLine; } } catch (Exception ex) { MessageBox.Show("Error with the psql-connection:" + Environment.NewLine + ex.Message); } client.Disconnect(); scrLog.Content += "SSH connection closed"; scrLog.Content += Environment.NewLine; } catch (Exception ex) { MessageBox.Show("Error with the ssh-connection:" + Environment.NewLine + ex.Message); }
我尝试使用
\q
命令最后,但它总是给我错误“42601:“\”处或附近的语法错误”,我读到它不需要。
有人可以帮忙吗?
经过研究和测试不同的场景,我终于找到了超时。我需要禁用连接池,如果再次需要,连接在关闭后仍处于打开状态。
我只是用 Pooling=false
.
string sqlpool = "false";
string sqlconn = String.Format("Server={0};Port={1};User Id={2};Password={3};Database={4};Pooling={5};",
sqlserver, sqlport, sqluserid, sqlpass, sqldatabase, sqlpool);