Question

I have a windows service that is polling a database. I am using EF6 and linq to do my queries and updates, etc.

The polling needs to be as often as possible, probably every 2 seconds or something in that area.

My gut tells me to have one connection and keep it open while my service is running, however something else tells me to open and close the connection every time. I feel that the latter will slow it down (will this really slow it down this much?).

What are the best practices when it comes to polling a database within a windows service? Should I really be polling my database so often?

Was it helpful?

Solution

I think you should dispose of the context frequently and create a new one every time you poll the database.

The main reason is that unless you disable object tracking (really only suitable for read only operation), the context gets bigger and bigger over time, with each successive polling operation loading more data into the context's cache. As well as the increase in memory this causes, SaveChanges() gets slower as the ObjectContext then looks for changes in the objects which are attached to it.

If the connection is lost for any reason, you'll also have a hard time associating a new connection with the context. Regardless, based on my own experience, it won't slow anything down, it's quick to construct any EF context objects after the first one, because the model is cached on first load.

I wouldn't worry about every polling every 2 seconds. That seems totally reasonable to me.

As an aside, if you're using SQL Server, you can use Sql Dependency to fire an event when data changes, but polling is the most reliable option.

http://msdn.microsoft.com/en-us/library/62xk7953(v=vs.110).aspx

Alternatively, if you're dead set against polling, you could look at using a Message Broker system like RabbitMQ and updating your apps to use it, but be prepared to lose a couple of weeks implementing the infrastructure.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top