Question

I know how I do this, I have actually done it before, but the following code HANGS (not throw an exception) in this update statement below, and I don't see a reason for it. Can anyone see why?

I don't think is important but id is the primary key of Person and belongs is a foreign key.

Before any sugest concurrence problem, I am the only person working on the tables. The creation of the command is so:

public static String upDatePersonBelonging(Int32 personId,Int32 groupId)
{
    String error;
    if ((error = openConnection()) != "")
        return error;

    OracleCommand command = 
      new OracleCommand("UPDATE person SET belongs = :belongs where id = :id ",
                         connection);

    addParameter(command, "belongs", OracleDbType.Int32, groupId);
    addParameter(command, "id", OracleDbType.Int32, personId);

    return runCommand(command);
}

The execution is so:

private static String runCommand(OracleCommand command)
{
    String error = "";

    try
    {
        command.ExecuteNonQuery(); // here it hangs
    }
    catch (Exception e)
    {
        error = e.Message;
    }
    finally
    {
        connection.Close();
    }

    return error;
}

The opening function is the following:

private static String openConnection()
{
    try
    {
        // create an open the connection          
        connection = new OracleConnection(_connStr);

        // open the connection
        connection.Open();
    }
    catch(Exception e)
    {
        return e.Message;
    }

    return "";
}

add parameter code:

private static void addParameter(OracleCommand command, String name, OracleDbType type, Object value)
{
    command.Parameters.Add(name, type);
    command.Parameters[command.Parameters.Count-1] = new OracleParameter(name, value);
}
Was it helpful?

Solution

Code seems to be correct.

I have 3 ideas:

  1. Update can be very long operation if there are many rows in the table and simply you have to wait (you can do this query using some other code/tool and compare times).
  2. Show as addParameter method.
  3. Maybe some other process is working on this table and locks it.

EDIT

Your addParameter method is strange. First you add parameter and then create new one. I will try rather something like this (not tested - I have no possibility):

private static void addParameter(OracleCommand command, String name,
  OracleDbType type, Object value)
{
    OracleParameter p = new OracleParameter(name, value);
    p.DbType = type;
    command.Parameters.Add(p);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top