This project is read-only.

When to initialize shapes with data from DB?

Topics: Core, General
Sep 7, 2014 at 2:35 AM
I stuck with an exception in NGM.OpenAuthentication module. They set some data taken from DB into a Shape:
public class Shapes : IShapeTableProvider {
    public void Discover(ShapeTableBuilder builder) {
               .OnDisplaying(displaying => {
                   var clientsData = _openAuthAuthenticationClients
                            .Select(client => _orchardOpenAuthClientProvider.GetClientData(client.ProviderName))
                            .Where(x => x != null)
                    var shape = displaying.Shape;
                    var metadata = displaying.ShapeMetadata;

                    shape.ClientsData = clientsData;

                    metadata.Type = "OpenAuthLogOn";
With this code I am getting random exceptions like
System.AggregateException: One or more errors occurred. ---> NHibernate.ADOException: While preparing select ###### an error occurred ---> System.InvalidOperationException: Prepare requires an open and available Connection. The connection's current state is closed.
   at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async)
   at System.Data.SqlClient.SqlCommand.Prepare()
   at NHibernate.SqlAzure.ReliableSqlCommand.Prepare()
The author of the module put the code of accessing DB into OnDisplaying(), initially the clientsData was initialized in the beginning of Discover() method. I tried to play around and put the code into shapes OnCreated() method, but got a bit different exception:
System.AggregateException: One or more errors occurred. ---> NHibernate.ADOException: While preparing select ###### an error occurred ---> System.InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first.
   at System.Data.SqlClient.SqlInternalConnectionTds.ValidateConnectionForExecute(SqlCommand command)
When I put the code with clientsData back to the beginning of Discover() it gives sometimes exceptions too.

Overall it feels that there must be some other way of accessing DB used in order to initialize a shape with data. Because when we try to do this during work of ShapeTableBuilder the DB connection is closed or not available some times.

The question is what would be the default approach in cases like this? How and when it's properly to access DB repositories during shapes initialization.

Thanks for any help!
Sep 7, 2014 at 1:27 PM
Hi, I am the Author of that module. What version of Orchard are you using?

I have not seen that error with this module before. Have you pull the latest code?
Sep 7, 2014 at 1:37 PM
Edited Sep 7, 2014 at 1:37 PM
Hi Nick,
I never bothered you with this but I am periodically getting the same error with 1.8.1 code with last NGM.OpenAuthentication.
And I think this error has been here since a long time.
Sep 7, 2014 at 4:31 PM
Hmmm looks like someone has reported it.

It's interesting, as this module is used on the WebLogs site. I will do some digging. I have also pushed some updates today that might help, including fixing it to the tip of 1.x and also some caching fixes.
Sep 7, 2014 at 7:46 PM
Hey Jetski! Thanks for responding! I am the same guy who posted the bug at github. I use the latest version of Orchard 1.8.1 (but started from 1.8 too) and the latest version of the NGM.OpenAuth module. The exceptions never appear at the local environment but only at the production servers published at Azure. I got a cheap account there, so the web-server is being stopped after some idle time, and mentioned exceptions appear when the web-server starts on clients requests.

I still think that maybe mixing code of loading DB data with Shape builder may be not the best approach in Orchard's lifecycle. Although I am new to Orchard so I can't say for sure. It seems that building shapes tree goes later from the general Http Request processing, this is why the DB connection sometimes (means bad timing on during web-server starting) is not available.
Sep 10, 2014 at 11:22 PM
Hey Jetski,

I tested your recent updates for last couple of days and I can confirm that the exception doesn't appear any longer under the mentioned circumstances. It seems caching Providers and then accessing the cached data to init Shapes helps. Thanks for you fixes!

I think this issue may be closed as resolved now.

P.S. Although there are couple of side-effects appeared after your fixes. I will created bugs on github to keep things organized.