oracle ExecuteScalar in parallel programming somet

2019-09-18 01:43发布

问题:

I used parallel programming to perform two parallel oracle database queries. Sometimes the "res" object returns as null. Any idea why? Note that this is a count query of the same table and nothing is modifying this table so the database table and the query are not changing.

string query = "select count(*) from SALES_ADVENTUREWORKS2012.SALESORDERDETAIL where PRODUCTID=709";
tasks[0] = Task.Factory.StartNew(() => db.ExecScalarQuery(query));
tasks[1] = Task.Factory.StartNew(() => db.ExecScalarQuery(query));
Task.WaitAll(tasks);
//---------------
public void ExecScalarQuery(String query)
{
    OracleConnection conn = new OracleConnection(connectionString);
    try
    {
        conn.Open();
        OracleCommand cmd = new OracleCommand();
        cmd.Connection = conn;
        cmd.CommandText = query;// "select count(*) from SALES_ADVENTUREWORKS2012.SALESORDERDETAIL where PRODUCTID=709";
        cmd.CommandType = CommandType.Text;
        cmd.CommandTimeout = QUERY_TIMEOUT;
        Object res cmd.ExecuteScalar();
    }
    finally
    {
        conn.Close();
    }
}

回答1:

As I known that if you close a connection in JDBC, its depending res would be closed as well.