locked
Who closed the connection? (WF?Enterprise Lib 3.1) RRS feed

  • Question

  •  

    Hi Guys:

    I had one workflow that ran with enterprise lib 2.0 before, and it worked fine. Today I align the enterprise lib to 3.1, and workflow did not have any change, I got one exception " DeriveParameters requires an open and available Connection. The connection's current state is closed."

     

    It is strange, because the error is made by the 3rd activity but the first 2 activities also connected database. So that means the first 2 activities can connect to database, but the 3rd activity cannot.

     

    When I rollback the changes done for enterprise lib, the workflow work.

     

    Anyone can tell me why? And how can I solve the problem?

     

    Regards

    Carol

    Friday, August 31, 2007 3:05 AM

Answers

  • If you take workflow out of the picture are you able to make all three calls without getting the error?  Has the connection been closed for some reason after activity 2 but before activity 3?

    Saturday, September 1, 2007 12:20 AM
  •  

    Yes. I found the root cause.

    My error is made by 2 reasons:

    1.Connection actually is closed by our code, because our guy wrongly used the method "ConnectionWrapper GetOpenConnection()".

    (It was due to we usually ask our members to write the code like this:

    using(DbConnection conn = ....)

     

    So he wrote the code:

    using(DbConnection conn = GetOpenConnection().Connection)

    )

     

    I am sorry for that.

     

    2. But GetOpenConnection is a new method in enterlib 3.0. It called static class TransactionScopeConnections that stored the connection in static member. So GetOpenConnection() always return the connection whatever its State is.

     

    So when, in same transaction, the 2nd activity closed the connection, the 3rd activity would try to call GetOpenConnection() that TransactionScopeConnections will return the closed the connection back and did not check the connection's state.

    Wednesday, September 26, 2007 10:19 AM

All replies

  • And my workflow is state machine workflow and hosted in IIS with WCF.

     

    Friday, August 31, 2007 3:07 AM
  • If you take workflow out of the picture are you able to make all three calls without getting the error?  Has the connection been closed for some reason after activity 2 but before activity 3?

    Saturday, September 1, 2007 12:20 AM
  •  

    Yes. I found the root cause.

    My error is made by 2 reasons:

    1.Connection actually is closed by our code, because our guy wrongly used the method "ConnectionWrapper GetOpenConnection()".

    (It was due to we usually ask our members to write the code like this:

    using(DbConnection conn = ....)

     

    So he wrote the code:

    using(DbConnection conn = GetOpenConnection().Connection)

    )

     

    I am sorry for that.

     

    2. But GetOpenConnection is a new method in enterlib 3.0. It called static class TransactionScopeConnections that stored the connection in static member. So GetOpenConnection() always return the connection whatever its State is.

     

    So when, in same transaction, the 2nd activity closed the connection, the 3rd activity would try to call GetOpenConnection() that TransactionScopeConnections will return the closed the connection back and did not check the connection's state.

    Wednesday, September 26, 2007 10:19 AM
  • But anyway, i think TransactionScopeConnections did not check the connection's state is a bug, because i found the following code has problem.

     

    using (TransactionScope trans = new TransactionScope())
    {

    Database database = DatabaseFactory.CreateDatabase();

        try
        {
            DbCommand cmd1 = database.GetSqlStringCommand("select * from xxx where app_id=1"); //table "xxx" did not exist
            database.ExecuteReader(cmd1);//encounter error; enterprise lib will colse connection.
        }
        catch
        {
            //not throw
        }

     

        DbCommand cmd2 = database.GetSqlStringCommand("select * from t_ic_app where app_id=2");
        database.ExecuteNonQuery(cmd2);//Get the exception that connection has closed.


        trans.Complete();
    }

    Wednesday, September 26, 2007 10:30 AM
  • Hi,
    could anyone tell me why enlib will close the connection when ExecuteReader encountered exception? Could i change the code so that it never will close exception?

    Refer to the following code snippet:

            public virtual IDataReader ExecuteReader(DbCommand command)
            {
                ConnectionWrapper wrapper = GetOpenConnection(false);

                try
                {
                    //
                    // JS-L: I moved the PrepareCommand inside the try because it can fail.
                    //
                    PrepareCommand(command, wrapper.Connection);

                    //
                    // If there is a current transaction, we'll be using a shared connection, so we don't
                    // want to close the connection when we're done with the reader.
                    //
                    if (Transaction.Current != null)
                        return DoExecuteReader(command, CommandBehavior.Default);
                    else
                        return DoExecuteReader(command, CommandBehavior.CloseConnection);
                }
                catch
                {
                    wrapper.Connection.Close();-->i want to remove the code of this line
                    throw;
                }
            }

    Regards
    Carol

    Monday, March 9, 2009 9:56 AM