Question

I've been stuck on this for a few days now, and still can't figure out why :

I need to update a record in my Oracle database, before executing the Update query, I want to lock the data with a "SELECT x FOR UPDATE NOWAIT", but when calling the ExecuteNonQuery, my webservice hangs.

Executing the sql statement directly against the database works instantly. Here is the code :

                    OracleTransaction transact = OraDCS.BeginTransaction();
                    OracleCommand cmd = OraDCS.CreateCommand();
                    cmd.CommandText = string.Format("UPDATE FLIGHT_MANIFEST_PASSENGER SET ETAT_PASSAGER = '{2}',GATE_BOARDING = {0} WHERE REF_DOSSIER = '{1}'", ConfigurationManager.AppSettings["CodeMoyen"].ToString(), ref_dossier, etatPax);

                    OracleCommand blq = OraDCS.CreateCommand();
                    blq.CommandText = string.Format("SELECT * FROM FLIGHT_MANIFEST_PASSENGER WHERE REF_DOSSIER = '{0}' FOR UPDATE NOWAIT", ref_dossier);

                    bool blqOK = false;
                    try
                    {
                        blq.ExecuteNonQuery();
                        blqOK = true;
                    }
                    catch (Exception ex)
                    {
                        LogThreadSafe.Instance.Info("LanceSQL Error : " + blq.CommandText + " " + ex.Message);
                    }

                    if (blqOK)
                    {
                        try
                        {
                            cmd.ExecuteNonQuery();
                            transact.Commit();
                        }
                        catch (Exception ex)
                        {
                            transact.Rollback();
                            LogThreadSafe.Instance.Info("LanceSQL Error : " + blq.CommandText + " " + ex.Message);
                        }

                    }
                    else
                        transact.Rollback();

The lock is actually performed on the databse, but the program still hangs at this line :

blq.ExecuteNonQuery();

EDIT : Even after linking the transaction to the command I still experience the freeze. However, when I deploy my webservice on the server, and test it "live" rather than in debug on my dev machine, it works, any ideas?

Was it helpful?

Solution

You need to associate the commands with the transaction:

cmd.Transaction = transact;
blq.Transaction = transact;

I also highly recommend you don't build your query text by string concatenation, but use parameters instead in order to prevent SQL injection. For example:

blq.CommandText = "SELECT * FROM FLIGHT_MANIFEST_PASSENGER WHERE REF_DOSSIER = :pDossier FOR UPDATE NOWAIT";
blq.Parameters.Add(new OracleParameter("pDossier", ref_dossier));

OTHER TIPS

i usually do if i must use a transaction:

OracleCommand cmd = OraDCS.CreateCommand();
OracleTransaction transact=cmd.Connection.BeginTransaction();
cmd.Transaction=transact;
cmd.CommandText = string.Format("UPDATE FLIGHT_MANIFEST_PASSENGER SET ETAT_PASSAGER = '{2}',GATE_BOARDING {0}WHERE REF_DOSSIER = '{1}'", ConfigurationManager.AppSettings["CodeMoyen"].ToString(), ref_dossier, etatPax);

maybe your transaction doesnt have a connection

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top