I'm developing a web application using ASP.NET MVC 3 and Entity Framework Code First with SQL Server Express on the back end.
SQL Server Express falls asleep by design to conserve system resources. I'm fine with that behavior, but unfortunately the first time a page is hit after SQL Server Express has been idle for a long time, Entity Framework times out waiting for the DB to return from idle to active state.
Is there a global means to tell Entity Framework to wait longer for the database before throwing an Exception?
I'm not sure if DbContext API exposes any property to override default command timeout which is probably 30s for SQL Server provider. You can try to convert DbContext to ObjectContext and set the timeout there:
var ctx = ((IObjectContextAdapter) dbContext).ObjectContext;
ctx.CommandTimeout = 120;
Related
I'm developing WPF - EF Core desktop application for multiple users. I have to connect to a MySql server with a limited number of connections. Testing with a single desktop client i see my connections grows 3-4 instances so i'm worry worried about it.
I really dont understand why because my code only calls one instance at the same time.
How i could decrease these numbers?
May be MySql maintains a minimun opened connections pool ?
Can i force to EF Core to use only one instance for a desktop application instance?
Edit:
It's an Azure MySql database (limited opened connections per instance). I attach an active connections graph. First graphic's part (range values between 4-7) is when i'm using a single desktop user test, then i stop and connections come back to 4.
All my calls are synchronous and with this structure:
using(var context = database.getContext())
{
//Calls to database
db.Savechanges(); // if needed
}
Have you tried adding the pooling option to your connection string: pooling=false
var connectionString = "Server=server;Database=database;User ID=user;Password=pass;Pooling=false;";
I'm creating a web application that uses SQLAlchemy to connect to a MySQL database. This may be a newbie question, but when is the proper time to call Engine.connect()? Should I be:
Calling Engine.connect() every time a web request is made, and closing the connection once the request is processed? OR
Calling Engine.connect() several times in the very beginning of my application to create several connections. Then every time I process a request, I use one of the connections, and never close them?
I'm also not exactly sure I understand the concept of a Connection Pool in general. Does the connection pool maintain a series of open connections to the database, and then hand a connection off whenever Engine.connect() is called? Or, does the pool establish a new connection every time Engine.connect() is called? What happens when Connection.close() is called?
I have a asp.net mvc application which is used to validate the data that is in the database using a SQL Server stored procedure. If the data is over a million records, it takes more than 40 minutes to process them and gives the validation results back to the .net mvc application. I have programmed such a way to sustain the .net mvc application browser session for one hour. But after 20 minutes a time out exception is being raised, as there won't be any response from the sql server until it finishes the SP processing. Is there any way to hold the session of sql server response call in .net mvc? or can I send any acknowledgements from sql in the middle of Stored Procedure call process?
Well, you can raise all timeouts that are involved here. Identify what component caused a timeout (ADO.NET or ASP.NET) and adjust the value.
The problem with that is that 40min long HTTP requests are quite unreliable. Any temporary problem such as a small network disruption or a router terminating idle connections can kill the connection. Also, you will sometimes need to restart the server (deployments, reboots, crashes).
This is not a good architecture.
Maybe you can split the work into smaller parts of, say, 10 seconds?
I have a WCF Data Service that uses a DBML file to generate all the code required for my DataContext. My database is running on the SQL Azure Business tier (so still using the shared model) and I am using the Transient Fault Handling Application Block to wrap all my calls.
The problem I'm seeing is that I'm still getting a number of SqlExceptions around "timeout expired". My retry policy hooks into the Retrying event to log any retries but i never see anything in my logs except the timeout exception.
From my research it looks like the Retry block only retries the query and assumes it has a reliable connection. However, since i'm using a DataContext I don't actually have control of setting up that connection and since all my existing code is Linq2Sql I don't want to switch it.
Am I missing something simple? There doesn't seem to be any way to tell the DataService that the CurrentDataSource should be a reliable connection or anyway to use the RetryManager to use a policy on the connection itself.
Here's an example of one of my ServiceOperations:
[WebGet]
public MyTable GetDetailsById(string id)
{
try
{
var detail = retryPolicy.ExecuteAction<MyTable>(() =>
CurrentDataSource.MyTable
.Where(l => l.id == id)
.FirstOrDefault());
return detail;
}
catch (Exception ex)
{
Trace.TraceError(ex.Message);
}
}
Any ideas?
Update: My query doesn't take longer than 30 seconds.
Command timeouts occur when the client is not getting a response from the server within x amount of seconds. The is not a transient error and hence not handled by the retry logic. The general recommendation is to change the command timeout to at least 30 seconds for Azure SQL Database. In addition, if you have long running queries, you should consider to increase the timeout for these queries beyond your application wide default.
Documentation on how to set the command timeout can be found here.
I am using MySQL database and hibernate and JSP.using hibernate select database store value and prepared view and display using Ajax.i am polling database every 1 seconds using Java script timer that called a ajax function and return the new responds,it result me an error
JDBCExceptionReporter:78 - Data source rejected establishment of connection, message from server: "Too many connections"".
Help me to sort-out the above define problem.
Make sure you close the session (and connection) after using it
Make sure the maximum connection configured for mysql is sufficient
Use of some caching layer. It is insane to hit the database every 1 second from each user.
If you are making some chat application, consider comet and server-side pub-sub solutions (jmx for example).