Does LinqMetaData dispose of IDataAccessAdapter?

Posts   
 
    
Aeon
User
Posts: 24
Joined: 21-Jul-2008
# Posted on: 19-Nov-2008 21:16:29   

How bad of a practice would it be to declare as a page-level property:

LinqMetaData _linqMetaData = new LinqMetaData(new DataAccessAdapter(false));

so at any point (or at multiple points) in the page, I can do something like: int orderCount = this._linqMetaData.Orders.Count();

When does the adapter get disposed of in this approach?

If I do not manually start a transaction with the adapter, how long is a transaction opened for something like the above count query?

Would it make a difference if i set keepConnectionOpen in the CTOR to true?

Would it make a difference if I handled the unload / descructor of the page and did: this._linqMetaData.Adapter.Dispose(); ?

Thanks, and sorry for the bombardment. -Ian

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 20-Nov-2008 10:50:46   

How bad of a practice would it be to declare as a page-level property: LinqMetaData _linqMetaData = new LinqMetaData(new DataAccessAdapter(false)); so at any point (or at multiple points) in the page, I can do something like: int orderCount = this._linqMetaData.Orders.Count();

The best practice is to use something like the following to insure the adapter gets disposed after being used.

using(DataAccessAdapter adapter = new DataAccessAdapter())
{
    LinqMetaData metaData = new LinqMetaData(adapter);
}

When does the adapter get disposed of in this approach?

When it goes out of scope. i.e. when the page is disposed.

If I do not manually start a transaction with the adapter, how long is a transaction opened for something like the above count query?

No transaction is opened for the above code, only the database default implicit transaction.

Would it make a difference if i set keepConnectionOpen in the CTOR to true?

Not a recommended practice, only do this when you have multiple consecutive commands to execute.

Would it make a difference if I handled the unload / descructor of the page and did: this._linqMetaData.Adapter.Dispose(); ?

I think it won't make a difference.

Aeon
User
Posts: 24
Joined: 21-Jul-2008
# Posted on: 24-Nov-2008 16:33:23   

Hello:

Thanks for the response. Please allow me to ask a followup.

The developer express LinqServerModeDataSource.Selecting event allows one to assign an IQueryable, which because of deferred execution does not permit a using statement since the query will be executed at a later time.

If I declare the adapter in the scope of the event handler, it works (apparently because garbage collection recognizes that it is needed by linqMetaData, which is needed by the page-scoped LinqServerModeDataSource control).

In this case, what would the recommended practice be? I suppose that the adapter would be disposed of when the LinqServerModeDataSource is destructed.

Also, just to reiterate, does the linqMetaData dispose of its adapter when it is destructed?

Thanks, -Ian

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 25-Nov-2008 11:26:27   

Also, just to reiterate, does the linqMetaData dispose of its adapter when it is destructed?

The adpater is consumed by the linqMetaData and could also be consumed elsewhere, so the linqMetaData should not disopse the adapter. This will be very restricitve in cases where developers want to re-use the same adapter in other consecutive queries.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39859
Joined: 17-Aug-2003
# Posted on: 25-Nov-2008 12:10:10   

To elaborate a bit more on what Walaa said: the adapter only needs to dispose the connection object. It by default closes a connection after an action, which gives the underlying connection object back to the pool. The adapter will dispose the active connection object when it closes it, it also disposes parameters etc. if required.

I.o.w.: you're fine: the Dispose() call on DataAccessAdapter has no work left as the used connection has been disposed already.

Frans Bouma | Lead developer LLBLGen Pro