dioptre wrote:
What's the best practice for keeping a DataAccessAdapter alive in a ServerMode application?
You shouldn't keep adapters alive. They're designed to be used and thrown away. Linq's design (MS' design, not our decision) dictates that a query contains the information necessary to execute the query as well, so an adapter has to be inside the query.
We've added an interface to our queryable's so you can inject the adapter later on, but as with a grid bound to hte queryable, that's not possible so you simply add the adapter and leave the rest to the grid.
This is not appropriate:
using (DataAccessAdapter da = new DataAccessAdapter()) {}
Because the LinqMetaData will likely call on the DA at any time in the future. And if its closed it will cause an error.
I'd also expect the database/application to kill off the connection after a set period.
Initially, there's no open connection. The adapter will open and close the connection when it needs to. Due to connection pooling, connection creation is very cheap.
At present I'm thinking that sharing the DA as a property and doing checks on it might be the best and safest way to go....?
Would it be OK to share this DA with all my queries?
NO. Never 'share' adapters among code, as it's not a threadsafe object. In a web-scenario, every request could be another thread. So no, do not share.
Will it bugger the context if the DA is re-populated?
Which context?
Will it affect performance (should I have a DA for every IQueryable/LinqMetaData) or is it multithreaded?
It doesn't cost any performance, as the adapter itself has no real overhead. Creating/opening/closing connections is cheap with connection pooling, so you won't notice it. Creating an adapter is also cheap, it doesn't really build up anything up front, as all meta-data etc. is already available in code, it doesn't have to re-parse meta-data.
I've been so used to closing the DA after every query, I'm not sure this is the best way to go...
That's the best way. So create adapter, execute query, throw away adapter. You don't even have to open connections, that's done for you.
If you have to execute queries in a loop or in the same routine after eachother, you can keep open the connection during that session and use the same adapter, but in all other cases (i.e. when you return the query, execute a single query etc.), create a new one per query. And never share an adapter among threads / pages etc.