Simultaneous LINQ data retrieval problem - sql

I came across a very strange problem which never happened to me before.
In the login code:
var sqlLogin = db.LoginRetrieve(loginID, archived).SingleOrDefault();
//(db is the linq data context)
--problem:
If two users login at the same time, this line of code will throw an exception which is "The required column 'UserLoginID' does not exist in the results."
But if a single user logs in or two users don't click the button at the same time, it will have no exception.
Is there anyone can share some lights on this? Thanks in advance.
Han

I suspect that your DataContext is shared between requests.
Don't do that.
You should create a separate DataContext for each request.
Otherwise, you'll get nasty threading issues like this one. (DataContext isn't thread-safe)
In general, you should be very careful when sharing objects between requests (eg, statics or Application / Session state).
Unless you know specifically otherwise, you should assume that the object is not thread-safe and cannot be shared.

Related

NHibernate Lazy loading lazy=extra no session or session was closed

I have a strange behaviour which I can't find any solution for days..
The behaviour I experience is the classic LazyInitializationException with no session or session was closed
which is usual when there's no contextual session and trying to access lazy loaded properties/collections, but this in not my case because I have:
Session-per-request session bounded with Spring.NET OpenSessionInViewModule
A session opened and bounded to the context (web request)
Entities with collection as "list" (mapping list collection) with lazy="extra" or with "set" (mapping set collection) (The issue is caused mainly with lazy=extra..)
It seems that the Count statement for extra cound be done, but accessing the whole collection throws the exception
After investigating the usual issues:
no session opened
the object is not bounded to any session
a call to Session.Clear or Session.Evict was made (so any lazy for the evicted instance will work)
the Spring.NET WebSupportModule is not registered
I found no explicit reasons of why this happen..
A strange behaviour is if I profile in NHprof, I see the exception in a new session, in other words, the contextual session works correctly,
but when the exception is thrown, that exception is visualized in a secondary session.. but NO code open a session, and a single, contextual session
is opened..
Anyone knows what other reasons could cause a situation like that?
Thanks in advance
It's difficult to diagnose without seeing some code, but the problem sounds very similar to this. In the linked case OpenSessionInView was being used in addition to setting the session context in another location (similar to your first two bullet points) and WebSupportModule was not registered (as in your last point).
On the other hand, I know nothing about Spring.NET.

LINQPad Dump() method not working for Sharepoint Client Object Model

I'm using LINQPad (2.42) to test some snippets that make use of SharePoint Client Object Model.
Basically messing with SPSite, SPWeb, SPList, SPFolder and SPFile.
The problem is that LINQPad seems to dislike calling .Dump() for any of the previous objects. It just keeps "Executing" for ever without showing any results.
Does anyone experience the same problem? Any workaround or fix?
Thanks
Try calling .Dump(0) to only dump the first level of Properties, or dumping the results to a Grid.
I have not looked at the SharePoint Client Objects but if they are anything like the TFS API classes, some of the properties are lazy loaded. Calling .Dump() will walk it's way down each every single result making server calls for every property (and property of property, etc.). This is probably what's taking the time.

Item is not added into the list (from code)

We have some strange problem here. We have feature event receiver, where we are creating custom fields -> content type -> list. After that, one default item is added. On my VM it was working just fine, but after moving into pre-prod environment, we got this strange behavior with no exception or error in logs.
First thing, item was created only sometimes, with no trace what happened. Mostly it was not created. I even experienced this: when I activated feature, I went to the list and so item there, but after refresh it was gone!
We tried to put there some Thread.Sleep() cycle (while debugging, item was in Items collection, but ItemsCount property of the list was always showing 0).
Now I am out of ideas what is wrong. It's not about execution time (maybe). Looks like, for some reason, SP is killing SPItem.Update before it is created for real and we don't know why. Any help is really welcome!
When you try to access sharepoint items from code and not have admin permissions to update/ delete them then set website website.AllowUnsafeUpdates = true; property
//Set AllowUnsafeUpdates = true to update the database / sharepoint list from code.
FormWeb.AllowUnsafeUpdates = true;
NewItem.Update();
FormWeb.AllowUnsafeUpdates = false;
you code should be like this to make changes in the list.. when you adding item to list.
Use Update statement in same manner when you accessing list and updating its data.
Check whether you updating the list correctly.. There may be some SharePoint security issue.
Reference Link:
http://blogs.msdn.com/b/infopath/archive/2010/04/01/add-items-to-a-sharepoint-list-using-managed-code.aspx.
You can check this [SPSecurity.RunWithElevatedPrivileges][1]
[1]: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsecurity.runwithelevatedprivileges.aspx , link, link
you can check that what is going over there by adding your events in sharepoint..
Check this post and debug it..
http://developmentsolutionsjunction.blogspot.com/2011/06/adding-events-and-eventhandlers-in.html
so I was finally able to find out where the problem was. After deeper study and trying that and this I found out that there was a third party feature. It was adding event to each created list and was deleting everything what wasn't consistent with CT defined by company. This is weird and I don't really understand why somebody wants this. But ok, they pay, their rules.
So if you encounter such problem, try also this possibility.
However, I also leard few things during this, e.g. if you are working with SP instances from web scope, use web scoped features, not site ones, also, SP has a nasty habit to silent som exceptions. Also, if you e.g. take instance of SPWeb from event properties, it doesn't necessarily means it is already created. It takes some time, also, Update() itself is a thing that DB has to perform. Sometimes it's better to alsways check if you really have instance and if not, threadsleep for a while.
Have you used .Update() method in your code??

RavenDB Load aggregate returns an empty instance

What would I need to be doing wrong to get an aggregate to be returned from the session (loading by id) that is an empty instance of the aggregate and not the entire graph. When I hit the actual url that is requested in my browser I see the full json object, hence, why I am suspicious of my session management.
I have tried recreating the situation in unit tests but no manner of poor session management can recreate what I am seeing. I see some null instances returned but never this strange 'empty' instance - which has an id but none of the properties have been hydrated.
I am seeing this behaviour in unstable build 438.
I just checked the behaviour by executing a query and am seeing the same issue, so it is not just when calling Load()
If you are seeing symptoms similar to those that I presented in the original question - the most likely scenario is that you are loading the entity via a 'stale' session.
I discovered that due to some IoC container config flaws on my behalf (remember, singletons are bad kiddies) that I was attempting to load via a session that had been hanging around far too long. I can't determine whether it had been disposed or not (I suspect not) but it was definitely living well past its creation date.
I am not sure this exactly explains the behaviour I was seeing, but I present it here in the hope of helping others.

Unloading a data reference in Entity Framework

Currently I am struggling with a Entity Framework issue. I have a wcf service that sits on top of the ef framework, and allows queries to the framework. At some point the user is able to request a file from the framework. The files are referenced by solution entries, so when you request a file from a solution, the reference is loaded to gain access to the file store.
That all works fine, but from that point onward, whenever you do another query that returns that solution entry, the whole file gets attached to the return result. I need some way of detaching or unloading the reference, such that the result entries will only contain an unloaded reference to the file store again.
I have tried to create a new context and query that context to retrieve information from, but when I do that, the entity in the original context is also changed.
I have tried to detach the entity from the original context and then query from the new context. That does not work either.
I have found one way of doing this. For all the non file-download queries, I detach the result entity, and send that over the wire. I am not sure if that is the best way to go about it though.
I hope someone might be able to provide some insight, thanks for the effort.
The issue you are experiencing is probably do to Change Tracking, which is on by default.
Possible Solution:
Disable Change Tracking with MergeOption.NoTracking
using (MyEntities _context = new MyEntities())
{
_context.Widgets.MergeOption = MergeOption.NoTracking;
return _context.Widgets.ToList();
}
This article may help to point you in the right direction on how to handle this issue if the solution above does not work.
I struggled with a similar issue recently. The problem was the result of the context maintaining a reference to the object I was using (obviously). Every time I made a change to an object of the same type, even when obtained with a new context (so I thought), the object was being changed.
With help from one of my peers, we determined the context was hanging around due to the way I was registering it with my IoC container (lifestyle per web request). When I changed the lifestyle to transient (which definitively provided a new instance) then changes to objects of the same type were unaffected.
Hope this helps.