Peta poco - ExecuteReader requires an open and available Connection - database

In rare situations I experience some issues with petapoco.
Sometimes I get the following exception:
System.InvalidOperationException: ExecuteReader requires an open and available Connection. The connection's current state is connecting.
at System.Data.SqlClient.SqlConnection.GetOpenConnection(String method)
at System.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(String method, SqlCommand command)
at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader()
at PetaPoco.Database.<Query>d__44`1.MoveNext()
I really dont know what to do about it. Has anyone seen this before? Any suggestions on why it happens and what to do about it?
I cannot reproduce it since it happens rarely(maybe 2-3% of the db calls)
I am using version 5.0.1 of petapoco.
Thanks! :)
EDIT:
I am using the following constructor to instantiate the Database:
public Database(string connectionString, string providerName)
{
_connectionString = connectionString;
_providerName = providerName;
CommonConstruct();
}
I am instantiating it as a singleton using dependency injection like below:
Container.Register(Component.For<IDatabase>().ImplementedBy<Database>().UsingFactoryMethod(() => new Database(configuration.ConnectionString, configuration.DbFactoryProvider)).LifestyleSingleton());

When I first read the post, I thought ah a threading bug. However, I didn't simply want to write it off, as such, without an explanation. FYI, any bug that is inconsistent like this in a web app, is most likely a threading bug.
Ok so why is it a threading bug. You're using PetaPoco as a singleton instance, which means one instance for the whole application. It works, mostly, because PetaPoco has internal smarts to know when to open/close a shared connection, and just by luck itself, the usages of PetaPoco aren't colliding, except 2-3% of the time.
In addition, there's an internal counter (smarts), which when zero will either create a new connection or close the existing one depending on the operation. Now given PetaPoco is not thread-safe, this internal counter could suffer form a threading related issue too, if two or more threads increment/de-increment the counter at the same time etc.
TDLR; In dotnet the general rule is to assume/make static methods thread-safe and everything else not thread-safe, excepted where clearly marked and where it makes sense. Your usage of PetaPoco would work if PetaPoco was thread-safe, but it is not. To fix this issue, create a new instance of PetaPoco for each request; a fairly cheap operation (more so with fluent configuration).
I see you're using PetaPoco 5.0.1, I suggest looking at the latest release as there's a new FluentConfiguration feature. And it just so happens that the documentation page for this feature, at the bottom of the page, has a sample container setup. I would suggest you check this out. Documentation
Happy PetaPoco'er'ing

Related

DotNetNuke Exception in DotNetNuke.Entities.Users.UserInfo.get_Social()

just recently started getting a "System.ArgumentException: An item with the same key has already been added." with the following stack trace.
System.ArgumentException: An item with the same key has already been added
at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
at DotNetNuke.Entities.Users.UserInfo.get_Social()
at DotNetNuke.Entities.Users.UserInfo.get_Roles()
at DotNetNuke.Entities.Users.UserInfo.IsInRole(String role)
at DotNetNuke.HttpModules.Membership.MembershipModule.AuthenticateRequest(HttpContextBase context, Boolean allowUnknownExtensinons)
at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
All I did was install and update Extensions and recycle the application pool. It appears on different pages where I have a custom module that has a search form on it and some DevExpress components.
DNN Version is 7.3.2
Kind regards
UPDATE:
Seems to be a bug in DNN https://dnntracker.atlassian.net/browse/DNN-6990
I think you are seeing a race condition where two threads are trying to access the Social property of the UserInfo:
public UserSocial Social
{
get
{
if (this._social == null)
{
this._social = new Dictionary<int, UserSocial>();
}
if (!this._social.ContainsKey(this.PortalID))
{
this._social.Add(this.PortalID, new UserSocial(this));
}
return this._social[this.PortalID];
}
}
The only way for the error to happen would be for two threads to check the presence of the portal id key and both trying to add the key to the dictionary at the same time.
The Social property is called in many places (14 according to ILSpy in a v7.2.2), so it is hard to pinpoint where the problem could be coming from; if you note that the problem occurs only with your custom module you may want to investigate the methods that may call this code

Silverlight -How to handle nested .ExecuteAsync With DevForce

I have a situation as below which gives me error and looks like timeout.
Its missing some insert of records.
the error is as below:
IdeaBlade.EntityModel.AsyncProcessor1.<>c__DisplayClass2.<.ctor>b__0(TArgs args)
at IdeaBlade.EntityModel.AsyncProcessor1.Signal()
at IdeaBlade.EntityModel.AsyncProcessor`1.b__5(Object x)
InnerException:
[HttpRequestTimedOutWithoutDetail]
Arguments:
Debugging resource strings are unavailable. Often the key and arguments provide sufficient information to diagnose the problem. See http://go.microsoft.com/fwlink/?linkid=106663&Version=5.0.10411.00&File=System.ServiceModel.dll&Key=HttpRequestTimedOutWithoutDetail
at IdeaBlade.EntityModel.EntityServerProxy.<>c_DisplayClass14.b_13()
at IdeaBlade.EntityModel.EntityServerProxy.ExecFunc[T](Func1 func, Boolean funcWillHandleException)
at IdeaBlade.EntityModel.EntityServerProxy.ExecuteOnServer[T](Func1 func, Boolean funcWillHandleException)
at IdeaBlade.EntityModel.EntityServerProxy.InvokeServerMethod(SessionBundle sessionBundle, ITypeWrapper entityManagerType, String typeName, String methodName, Object[] args)
at IdeaBlade.EntityModel.EntityMa
Any Idea how to handle it?
Thx:)
......
.ExecuteAsync(op =>
{
var cust =Customers.Where(p => p.IsSelected).ToList();
..........................
Ships.ForEach(.......
...........
EntityManager.SalesGetSalesQuery(
..............
.ExecuteAsync(opn =>
{
................
});
p.UpdateOrders(copyOrders);
Orders.Add(copyOrders);
Save();
});
A timeout can happen at several places, so you will want to increase all possible timeout values.
In this case, you should be looking at increasing the query (CommandTimeout and Transaction), communication, and IIS executionTimeout.
DevForce has a documentation page that talks about troubleshooting timeouts. It's at http://drc.ideablade.com/devforce-2012/bin/view/Documentation/understand-timeouts.
I noticed that your nested query ("SalesGetSalesQuery") is a StoredProcQuery. There is an outstanding bug where StoredProcQueries are not respecting the Transaction timeout value, if different than the default. (120 seconds) We are working on a fix, but unfortunately there's no workaround in the meantime.
If it's not the StoredProcQuery that's timing out, then the link above will help you resolve it.
Job number 1 is to increase the timeout period while you figure out what is taking so long.
This will help https://stackoverflow.com/questions/4877315/silverlight-4-ria-services-timeout-issues
I don't think that the issue is in the fact that the async calls are nested. Remember that the second (i.e. nested) async call will only be executed once the first completed.
What async call is timing out exacly? Is it the StoredProcQuery? (any of them since you're calling them in a loop) If yes, then it's an outstanding bug that we are working on fixing. Like I mentioned in the previous post, there is no workaround. However, since this particular storedProc takes a date range as arguments, one possibility would be 'breaking' this date range in smaller date ranges and issuing multiple async calls. (perhaps in a parallel coroutine) Not that this 'workaround' is not fail proof since all orders could be in a small range period and the async call for that particular range would still timeout.
sbelini.

NHibernate with custom sql-insert vs catching procedure raised exceptions and out params

I have mapped classes with custom sql (insert, delete, update) through procedure calls. But, I noticed that when my insert procedure fails raising exception, the GenericAdoException from NHibernate doesn't have my message raised from the procedure.
But, all raised exceptions from procedures for delete and update is catched well, only the insert procedure hasn't its exception message catched.
Is that a limitation or a bug of NHibernate 3.2.4 when we use "native" generator for ids combined with custom sql ?
I'm searching also ways to get some out parameters from that procedures like a timestamp to each event (insert, delete and update), the timestamp is generated inside procedures.
EDIT: OUT PARAMs - I found the "generated" option over properties mapping options which we can ask to NHibernate to get params from procedures. This means that these properties have genarated values. So I tried to use generated="always" and works for insert, update and delete operations. Example: <property name="MyProp" generated="always"/>
I found that sql server driver doesn't put the messages raised by stored procedures into the SqlException when you run these stored procedures with ExecuteReader(). On the other hand NHibernate executes the custom sql-insert with ExecuteReader() (I debbuged its source code) and I guess it's right and necessary to get the key when it's mapped with native (or identity), my case.
Well, and now what to do ? I found also (hard to found) that the SqlConnection has a event called "InfoMessage" in which you can receive (catch) all messages sent from your stored procedures (raiserror). Now this is possible to "catch" these messages, but how to make them cross NHibernate core and be received by our application when we insert something session.save() ?
Altough we have access to session and so to the connection (SqlConnection) the messages was already lost, because them are only received by the delegate assigned to the event SqlConnection.InfoMessage before of its occurrence.
To solve this, I tried two approaches:
In the first I projected a way to register the delegate inside DriverConnectionProvider.GetConnection() and this delegate would store the messages on the thread context associating it with the connection, so these messages could be getted later.
In the second and the one choosed, I implemented IDbConnection and IDbCommand wrapping inside them the SqlConnection and SqlCommand (but I think the NHibernate has a bug because in some places it references DbConnection instead IDbConnection - like in ManagedProviderConnectionHelper, so I had to extend from DbConnection and DbCommand instead).
Inside my CustomSqlConnection I register the delegate and store the messages for later use.
This is working ! Work as standalone driver (ADO) either as a NHibernate driver.
The idea is:
public class CustomSqlConnection : DbConnection, IDbConnection {
private SqlConnection con;
private StringBuilder str = new StringBuilder(0);
public CustomSqlConnection() {
con = new SqlConnection();
con.InfoMessage += OnInfoMessage;
}
private void OnInfoMessage(object sender, SqlInfoMessageEventArgs e) {
if (str.Length > 0) {
str.Append("\n");
}
str.Append(e.Message);
}
public string FetchMessage() {
string msg = Message;
str.Clear();
return msg;
}
...
...
}
EDIT: The hard step is to implement all operations from DdConnection and Dbcommand, repassing the call to the sql instance (look the field con above), so:
...
public override void Open() {
con.Open();
}
...

Can't add related entities in CRM 3.0

The story so far:
A couple weeks ago, a field completely disappeared from the AccountExtensionBase table but remained in the Account and FilteredAccount views. This caused the Main Info form in CRM itself (which draws from that table) to stop working completely.
To fix the above, I manually removed said field from the definitions of said views, and I edited the Main Info form to no longer reference the missing field.
That left in more-or-less working condition. The only thing that's still broken is adding related entities from an already-open Main Info form. When I try, it throws exception 0x80044150. After learning this, I manually removed the field from the METABASE database and manually removed all references to it in the OrganizationUIBase table. Nothing. CRM won't let me just recreate the field, and I also can't seem to convince it to actually write to the trace log so I can get any more information about what's going on. I know this sometimes comes up when you exceed the 2155 option limit for picklist fields within an entity, but I don't think that's it, because the first thing I tried when the original issue manifested itself was to remove two entire picklist fields that were no longer needed, and that clearly hasn't helped.
So any ideas about what could be causing this, or what I can do to fix it, or even what a possible next step might be? I'm kind of stumped here.
Edit: got the trace logging to work...
at ErrorInformation.LogError()
at ErrorInformation..ctor(Exception exception, Uri requestUrl)
at MainApplication.Application_Error(Object sender, EventArgs e)
at EventHandler.Invoke(Object sender, EventArgs e)
at HttpApplication.RaiseOnError()
at HttpApplication.RecordError(Exception error)
at HttpApplication.ResumeSteps(Exception error)
at HttpApplication.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
at HttpRuntime.ProcessRequestInternal(HttpWorkerRequest wr)
at HttpRuntime.ProcessRequest(HttpWorkerRequest wr)
at ISAPIRuntime.ProcessRequest(IntPtr ecb, Int32 iWRType)
>MSCRM Platform Error Report:
Error: Exception of type System.Web.HttpUnhandledException was thrown.
Error Number: 0x80044150
Error Message: Exception from HRESULT: 0x80044150.
Error Details: Exception from HRESULT: 0x80044150.
Source File: Not available
Line Number: Not available
Request URL: http://crmserv/userdefined/edit.aspx?_CreateFromType=1&_CreateFromId={94892C13-A23E-DB11-BBF1-0014221C4264}&etc=10059
Stack Trace Info: Exception of type System.Web.HttpUnhandledException was thrown.
at System.Web.UI.Page.HandleError(Exception e)
at System.Web.UI.Page.ProcessRequestMain()
at System.Web.UI.Page.ProcessRequest()
at System.Web.UI.Page.ProcessRequest(HttpContext context)
at System.Web.CallHandlerExecutionStep.System.Web.HttpApplication+IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
at Microsoft.Crm.Platform.ComProxy.CRMCustomizationClass.Transform(CUserAuth& Caller, CObjectName& SourceObject, Int32 TargetObjectType, Int32 TargetFieldType)
at Microsoft.Crm.Application.Platform.Entity.CreateFrom(Int32 sourceEntityType, String sourceEntityId, TransformType transformType)
at Microsoft.Crm.Application.Forms.EndUserForm.PopulateDefaultDataIfAvailable()
at Microsoft.Crm.Application.Forms.EndUserForm.RetrieveParams()
at Microsoft.Crm.Application.Forms.EndUserForm.Initialize(Entity entity)
at Microsoft.Crm.Application.Forms.CustomizableForm.Execute(Entity entity, String formId, String formType)
at Microsoft.Crm.Application.Forms.CustomizableForm.Execute(Entity entity)
at Microsoft.Crm.Application.Pages.UserDefined.DetailPage.ConfigureForm()
at Microsoft.Crm.Application.Controls.AppPage.OnPreRender(EventArgs e)
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Page.ProcessRequestMain()
Type: Platform
This might be the more useful error report, actually. The above showed up twice, but this one is getting thrown scores of times every minute:
at User.GetPrivilege(String priv, PRIVILEGE_DEPTH depth)
at User.GetPrivilege(Int32 objectType, PrivilegeId privilegeId)
at AppMenuBar.BuildNewObjectMenu(Menu menuObject, Boolean buildAsSubMenu)
at AppFormMenuBar.BuildFileMenu(String formName)
at AppFormMenuBar.Execute(PrivilegeCheck privilegeCheck, String formName)
at AppFormMenuBar.Execute(PrivilegeCheck privilegeCheck)
at AccountDetailPage.ConfigureMenus()
at AppPage.OnPreRender(EventArgs e)
at Control.PreRenderRecursiveInternal()
at Page.ProcessRequestMain()
at Page.ProcessRequest()
at Page.ProcessRequest(HttpContext context)
at CallHandlerExecutionStep.System.Web.HttpApplication+IExecutionStep.Execute()
at HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
at HttpApplication.ResumeSteps(Exception error)
at HttpApplication.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
at HttpRuntime.ProcessRequestInternal(HttpWorkerRequest wr)
at HttpRuntime.ProcessRequest(HttpWorkerRequest wr)
at ISAPIRuntime.ProcessRequest(IntPtr ecb, Int32 iWRType)
Privilege Check Failed For user: , User Does Not Have Privilege: {9f2b415e-8a0c-430c-bdd1-ad2052b86b02}
Lots of different users show up in the last line. Anything?
It sounds like you are making unsupported direct SQL modifications to the database. If that is the cause of the errors, you are likely in trouble. You say tracing will not turn on, did you add the following keys to the registry and perform IIS reset? - http://support.microsoft.com/kb/907490
EDIT:
Can you run the following query replacing the database name with that from your instance and let me know if a privilege comes up. You are likely missing that privilege for the users security role for whom it is throwing that error.
SELECT [PrivilegeId]
,[Name]
,[CanBeLocal]
,[CanBeDeep]
,[VersionNumber]
,[CanBeGlobal]
,[CanBeBasic]
,[AccessRight]
,[IsDisabledWhenIntegrated]
FROM [MyCrmDatabase_MSCRM].[dbo].[PrivilegeBase]
WHERE privilegeid='9f2b415e-8a0c-430c-bdd1-ad2052b86b02'

WPF FormattedText "The system cannot find the file specified" exception in a service

We are using the WPF FormattedText object to determine text size in a service that grabs the latest news headlines from an RSS feed. The text retrieved needs to be in a specified canvas size. The service runs the code every 10 seconds and uses up to 2 threads if one takes longer than that. I'm using TaskFactory (which I've overridden the LimitedConcurrencyLevelTaskScheduler to limit to the amount of threads I specified).
This works great, except after several days (the length is variable), we start to get the following exceptions. The same code was working fine before we started using TPL to make it mult-threaded.
I need help figuring out what this is caused by. A few thoughts I'm looking into are: thread collisions holding on to a TTF file, memory issue, the dispatcher (see the stack trace) isn't playing nicely with the TaskFactory, other??
We don't have good profiling setup, but we've looked at the TaskManager when the exception is occurring and memory usage looks normal.
My next attempt is to use the TextBlock object and see if the exception is avoided.
Error Message: The system cannot find the file specified
Error Source: WindowsBase
Error Target Site: UInt16 RegisterClassEx(WNDCLASSEX_D)
Exception Stack Trace:
at MS.Win32.UnsafeNativeMethods.RegisterClassEx(WNDCLASSEX_D wc_d)
at MS.Win32.HwndWrapper..ctor(Int32 classStyle, Int32 style, Int32 exStyle, Int32 x, Int32 y, Int32 width, Int32 height, String name, IntPtr parent, HwndWrapperHook[] hooks)
at System.Windows.Threading.Dispatcher..ctor()
at System.Windows.Threading.Dispatcher.get_CurrentDispatcher()
at System.Windows.Media.TextFormatting.TextFormatter.FromCurrentDispatcher(TextFormattingMode textFormattingMode)
at System.Windows.Media.FormattedText.LineEnumerator..ctor(FormattedText text)
at System.Windows.Media.FormattedText.DrawAndCalculateMetrics(DrawingContext dc, Point drawingOffset, Boolean getBlackBoxMetrics)
at System.Windows.Media.FormattedText.get_Metrics()
at
(my method using the FormattedText, which is in a loop)
private static Size GetTextSize(string txt, Typeface tf, int size)
{
FormattedText ft = new FormattedText(txt, new CultureInfo("en-us"), System.Windows.FlowDirection.LeftToRight, tf, (double)size, System.Windows.Media.Brushes.Black, null, TextFormattingMode.Display);
return new Size { Width = ft.WidthIncludingTrailingWhitespace, Height = ft.Height };
}
EDIT: so far I've tried placing a lock around the code that calls this function, and calling it inside the CurrentDispatcher.Invoke method like so:
return (Size)Dispatcher.CurrentDispatcher.Invoke(new Func<Size>(() =>
{
FormattedText ft = new FormattedText(txt, new CultureInfo("en-us"), System.Windows.FlowDirection.LeftToRight, tf, (double)size, System.Windows.Media.Brushes.Black, null, TextFormattingMode.Display);
return new Size { Width = ft.WidthIncludingTrailingWhitespace, Height = ft.Height };
}));
EDIT: I've found links to others having similar, but not the exact problem.
http://www.eggheadcafe.com/software/aspnet/31783898/problem-creating-an-bitmapsource-from-an-hbitmap-in-threaded-code.aspx ~having a similar problem, but no answers
System.Windows.Media.DrawingVisual.RenderOpen() erroring after a time ~having a similar problem, but no answers
http://connect.microsoft.com/VisualStudio/feedback/details/361469/net-3-5-sp1-breaks-use-of-wpf-under-iis# ~ similar exception, but we're not using 3.5SP1 or IIS 7.
I've also submitted this through the Microsoft Connect site (please vote for it if you are having a similar problem).
https://connect.microsoft.com/WPF/feedback/details/654208/wpf-formattedtext-the-system-cannot-find-the-file-specified-exception-in-a-service
EDIT: Response from Microsoft:
"WPF objects need to be created on Dispatcher threads, not thread-pool threads. We usually recommend dedicating a thread to run the dispatcher loop to service requests to create objects and return
them frozen. Thanks, WPF Team" ~ How would I implement this?
EDIT: final solution thanks to NightDweller
if(Application.Current == null) new Application();
(Size)Application.Current.Dispatcher.CurrentDispatcher.Invoke(new Func<Size>(() =>
{
...});
EDIT: When I deployed the change (new Application();), I got an error logged " Cannot create more than one System.Windows.Application instance in the same AppDomain."
Error Source: PresentationFramework
Error Target Site: Void .ctor()
A shot in the dark:
The stack trace seems to show that WPF does not find a Dispatcher in the thread executing GetTextSize, so it has to create a new one, which involves creating a handle to a window.
Calling this every 10 seconds means 8'640 threads, thus windows per day. According to Mark Russinovich, there is a limit of 32 K windows per session, which may explain the error in RegisterClassEx.
An idea to overcome this is to read the current dispatcher from your main thread and set it in your tasks.
Edit:
I had another look and it looks like one cannot set the Dispatcher of a thread (it's created automatically).
I'm sorry, I am unable to understand what is going on here.
In order to compute the text size, WPF needs a FormattedText instance, which is stored as a member of the Dispatcher class. The existing Dispatchers are stored in a list of weak references. Each one is associated with a specific thread.
Here, it looks like new Dispatcher instances are created many, many times.
So, either the calling thread is new or memory is quite low and the weak references have been discarded.
The first case (new thread) is unlikely as the task scheduler uses the thread pool, which has about 25 threads per core (if I remember correctly), which is not enough to deplete the pool of ATOMs or windows.
In the second case, the depletion of resource is unlikely as the HwndWrapper is IDisposable and the Dispose method takes care of freeing the registered class.
As you already know from the info you provided, All UI elements (FormattedText is one) have to be created on the UI thread.
The code you are looking for is:
return (Size)Application.Current.Dispatcher.CurrentDispatcher.Invoke(new Func<Size>(() =>
{
FormattedText ft = new FormattedText(txt, new CultureInfo("en-us"), System.Windows.FlowDirection.LeftToRight, tf, (double)size, System.Windows.Media.Brushes.Black, null, TextFormattingMode.Display);
return new Size { Width = ft.WidthIncludingTrailingWhitespace, Height = ft.Height };
}));
Notice the Application.Current - you want the "Application" dispatcher which is the dispatcher for the UI thread in WPF applications.
Your current code actually creates a dispatcher for the current thread so you didn't really change the executing thread (see here regarding the dispatcher)
Have you renamed anything? If yes, check that link: WPF Prism: Problem with creating a Shell

Resources