LINQ to SQL Delayed Evaluation Exception Handling - linq-to-sql

I have a LINQ to SQL/Unity/ASP.NET MVC/SQL Server application hosted on Azure. I am using Lazy Evaluation throughout the whole site. The Application is using a TCP connection to SQL Server and every so often I get a "A transport-level error has occurred when sending the request to the server." SqlException.
Well since the the query is getting materialized when it is called later in code, I can't just wrap a specific piece of code with a try/catch.
Is there a way that I can handle this exception by implementing an interface or attaching a delegate to the DataContext?

If you use the results from linq as-is, then you have to handle the error in the code that materializes the data. However, instead of simply returning the linq query results directly, you could wrap it in an IEnumerable implementation of your own design, which is free to implement whatever error handling you like. This way the code that consumes the IEnumerable doesn't have to handle the exception.

I think that the only way to fix this is to wrap the code that is triggering (materializing) the call to the SQL server in a try catch.

Related

Errors thrown within a flow are returned to the rpc client as an InternalNodeException, how do we improve error reporting?

We are trying to implement better error reporting coming out of our Corda flows, but we're seeing that any errors thrown within a flow are returned to the RPC-client as an InternalNodeException.
Is there a recommended way to return more details to the client when we need it?
To improve the information provided back to the RPC-Client make the exception type implement ClientRelevantError.
This is a feature that we plan to delete: https://r3-cev.atlassian.net/browse/CORDA-2740

Meteor throws throwIfSelectorIsNotId exception

When running some code Meteor throws a throwIfSelectorIsNotId exception. I have two clients running the same code and the exception is thrown when the second client is running the same pice of code.
Cant figure out what this exception means and why it is thrown. Hopefully someone will be able to explain it.
For certain operations on the client (since version 0.57 I think it was). When doing an update operation e.g
MyCollection.update({name:"John Doe"},{$set:{age:50}});
You need to split it into two parts, on the client. (Only on the client).
var doc_id = MyCollection.findOne({name:"John Doe"})._id;
MyCollection.update({_id:doc_id,{$set:{age:50}});
You need to find the document by the _id first then update that document. The selector can only be an _id for update & remove operations.
This is because of a security risk with meteor's design, if there were to be a client side mongodb database it could arbitrarily get information from the server on other operations while determining on whether to allow the update or not. It was introduced in Meteor 0.57.

PetaPoco Should I use MultipleActiveResultSets=True?

From time to time we receive the following database connection error from PetaPoco in an ASP.NET MVC 4 app:
There is already an open DataReader associated with this Command which must be closed first.;
System.Data; at System.Data.SqlClient.SqlInternalConnectionTds.ValidateConnectionForExecute(SqlCommand command)...
It seems like this happens as we get more load to the system.
Some suggestions we found as we researched were:
Do a PetaPoco Fetch instead of a Query
Add MultipleActiveResultSets=True to our connection string
Can someone with PetaPoco experience verify that these suggestions would help?
Any other suggestions to avoid the Exception would be appreciated.
Update 06/10/2013 We changed the Query to a Fetch and we have seen some improvement however we still sometimes see the error.
Does anyone know what drawbacks changing the connection string to MultipleActiveResultSets=True might have?
Be sure that you are creating the PetaPoco DB per request (not a static).
See: how to create a DAL using petapoco
Update 06/10/2013 All Fetch methods calls the Query method (see the source)
So changing one for the other has no effect on the error.
The drawbacks are listed on the MSDN and includes warnings with:
Statement Interleaving
Session Cache
Thread Safety
Connection Pooling
Parallel Execution
I have tried it personally and didn't got any drawbacks (depends on your app), but didn't get rid of the errors also.
The only thing that you can do to remove the error, it's follow your request code to find where in the code the statement is called twice, and then use other DB connection in that function.
Also, you can catch the error and then create a new db connection and try with that new one.
Sorry but not magic bullet here.

Can I reuse the connection from DataContext in Linq to Sql?

DataContext has Connection property of type DbConnection and I was wondering if I could re-use it. I've tried to use it creating a command using CreateCommand and using a reader off of it but I've gotten errors saying there is a pending transaction or something similar.
Basically I'm trying to find out if there is a best practice or guidance on re-using that connection object.
The connection string itself doesn't really help since I can't create a new connection object with the abstract type and I don't want the code to know the specific provider type either.
One alternative approach I'm thinking of doing is having my DataContext derived type to have members that provide a factory method to create a new connection using the same connection information.
FYI, this inquiry stems from the need to invoke an ad-hoc stored procedure through DataContext, not the design-time ExecuteMethodCall variety. I didn't know about ExecuteQuery and for now that suffices. But for other situations where ExecuteQuery is inadequate, I'd need the low-level data access using connection/command etc.
Why not turn it around, and instead supply connections to the datacontext by using the constructor that takes a connection as a parameter? That way you can control when connections are created and disposed, opened and closed, and can reuse them for other purposes without having to worry about the internal behavior of the L2S datacontext...
I can't say definitely, but a connection is a connection. Until it's been closed (or returned to the connection pool), it should be viable to use, whether via LINQ or other means.

Use single Elmah.axd for multiple applications with single DB log

We have a single SQL Log for storing errors from multiple applications. We have disabled the elmah.axd page for each one of our applications and would like to have a new application that specifically displays errors from all of the apps that report errors to the common SQL log.
As of now, even though the application for all errors is using the common SQL log, it only displays errors from the current application. Has anyone done this before? What within the elmah code might need to be tweaked?
I assume by "SQL Log" you mean MSSQL Server... If so, probably the easiest way of accomplishing what you want would be to edit the stored procedures created in the SQL Server database that holds your errors.
To get the error list, the ELMAH dll calls the ELMAH_GetErrorsXML proc with the application name as a parameter, then the proc filters the return with a WHERE [Application] = #Application clause.
Just remove the WHERE clause from the ELMAH_GetErrorsXML proc, and all errors should be returned regardless of application.
To get a single error record properly, you'll have to do the same with the ELMAH_GetErrorXML proc, as it also filters by application.
This, of course, will affect any application retrieving errors out of this particular database, but I assume in your case you'll only ever have the one, so this should be good.
CAVEAT: I have not tried this, so I can't guarantee the results...
It's not a problem to override the default Elmah handler factory so that it will filter Elmah logs by applications. I wrote a sample app that shows how to do it with MySql: http://diagnettoolkit.codeplex.com/releases/view/103931. You may as well check a post on my blog where I explain how it works.
Yes, it easily works. However you can't see app name in Elmah/Default.aspx. I haven't found if it is confugurable - just display one column more.