DuckDuckGoose
DuckDuckGoose

Reputation: 226

A connection attempt failed because the connected party did not properly respond after a period of time. when calling Stored Procedure from C#

I'm trying to call a stored procedure from my mysql database below is the code from my C# program and i keep getting the same error

SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

looking in the Database the Stored Procedure is working correctly, so is there a way of checking when the procedure is done (First time asking a question on stack overflow I hope I provided enough info, if theres anything else let me know)

here's my C# code

 public void BasicCveInfoFill()
        {
            MySqlTransaction transaction = null;
            try
            {
                using(MySqlConnection conn = GetConnection())
                {
                    conn.Open();
                    using(MySqlCommand cmd = new MySqlCommand())
                    {
                        cmd.Connection= conn;
                        cmd.Transaction = transaction;                     
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandText = @"cveTableFill";                        
                        var rows = cmd.ExecuteNonQuery();                        
                    }
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

the GetConnection method (switched to random database)

public MySqlConnection GetConnection()
        {
            string connString = @"server=localhost;user=root;database=test;password=";
            MySqlConnection conn = new MySqlConnection(connString);
            return conn;
        }

and this is the code of the stored procedure

DROP PROCEDURE IF EXISTS cveTableFill;
DELIMITER ;;
CREATE PROCEDURE cveTableFill()
BEGIN
DECLARE n INT DEFAULT 0;
DECLARE i INT DEFAULT 0;
SELECT COUNT(*) FROM cpe INTO n;
SET i=0;
WHILE i<n DO 
  INSERT IGNORE INTO cve(cve_name, cpeMatchCount) SELECT c.cveMatch, COUNT(c.cveMatch) FROM cpe c GROUP BY c.cveMatch HAVING COUNT(c.cveMatch);
  SET i = i + 1;
END WHILE;

End;;
DELIMITER ;

this is the full exception

MySql.Data.MySqlClient.MySqlException (0x80004005): Fatal error encountered during command execution. ---> MySql.Data.MySqlClient.MySqlException (0x80004005): Fatal error encountered attempting to read the resultset. ---> MySql.Data.MySqlClient.MySqlException (0x80004005): Reading from the stream has failed. ---> System.IO.IOException: Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.. ---> System.Net.Sockets.SocketException (10060): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 count) --- End of inner exception stack trace --- at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 count) at MySql.Data.MySqlClient.TimedStream.Read(Byte[] buffer, Int32 offset, Int32 count) at MySql.Data.MySqlClient.MySqlStream.ReadFully(Stream stream, Byte[] buffer, Int32 offset, Int32 count) at MySql.Data.MySqlClient.MySqlStream.LoadPacket() at MySql.Data.MySqlClient.MySqlStream.LoadPacket() at MySql.Data.MySqlClient.MySqlStream.ReadPacket() at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId) at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32& affectedRows, Int64& insertedId) at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force) at MySql.Data.MySqlClient.MySqlDataReader.NextResult() at MySql.Data.MySqlClient.MySqlDataReader.NextResult() at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior) at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior) at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader()
at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery() at VulnerabilityDB.SighburDatabaseConnect.BasicCveInfoFill() in C:\Dev\pcs\PcsProjects\SbomArenaC\VulnerabilityDB\SighburDatabaseConnect.cs:line 65

Upvotes: 3

Views: 24446

Answers (2)

zfeld
zfeld

Reputation: 36

Try removing the timeout from you command

cmd.CommandTimeout = 0;

The command has a default timeout of 30 seconds

https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlcommand.commandtimeout

The time in seconds to wait for the command to execute. The default is 30 seconds.

Upvotes: 1

check the connection state of your ole connection

myConnection.State == ConnectionState.Closed  

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

The server ip address can be accessed by client. The server may not be listening on the port

Quote: Check that the hostname/port is correct, server is online, firewall isn't dropping packets etc.

Error Connection timed out. A connection attempt failed because the connected party did not properly respond after a period of time, or the established connection failed because the connected host has failed to respond.

Upvotes: 1

Related Questions