Asynchronous Data Access

Asynchronous Data Access

Old forum URL: forums.lhotka.net/forums/t/287.aspx


Tim FOrd posted on Tuesday, June 06, 2006

Evening All,

I'm currently using the latest version of CSLA and was wondering if it was possible to implement Asynchronous database funcationality for mulitple database executions from the business object using the DataPortal Fetch??

Would i need to extend the framwork to handle this??

Thanks, Tim.

Mark replied on Tuesday, June 06, 2006

A concrete example might help us understand your situation a bit better.  Although I don't make asynchronous database calls, I do run quite a few queries on background threads.  That doesn't require any framework modifications.

Tim FOrd replied on Tuesday, June 06, 2006

Hi Mark,

Because of the way I’m working with the business objects, for instance we have a customer which has multiple child objects as properties which need to load data from the database. We have thought about loading some of this data on demand, but there will be cases where we need to load a full object all, this is where I thought I might be about to add asynchronous data calls which would allow me to execute multiple commands against the database.

I had previously done some work with the Background Worker in VS.net 2005 and it worked really well, which could sit in the UI and work with the business object and wondered if I might be able to implement this with in the Fetch Area of the business object using _Begin & _End

Thanks, Tim.

Mark replied on Tuesday, June 06, 2006

All the processing has to be complete by the time the DataPortal Fetch method finishes executing.  Once it exits, the DataPortal assumes you're done and passes the results back to the client, so any callback methods would be for naught (although it might work -by pure chance - if you're running local.  I sincerely doubt if it would work via remoting).  So, sample code...

private void DataPortal_Fetch(Criteria crit)
{
   //Normal setup stuff for cmd1 and cmd2
   IAsyncResult result1 = cmd1.BeginExecuteReader();
   IAsyncResult result2 = cmd2.BeginExecuteReader();
   while (!result1.IsCompleted && !result2.IsCompleted)
   {
      //Kill time - sleep, whatever
   }
   using (SqlDataReader reader = cmd1.EndExecuteReader(result1))
   {
      //Populate object 1
   }
   using (SqlDataReader reader = cmd2.EndExecuteReader(result2))
   {
      //Populate object 2
   }
}

This should work, but I'm not sure if it's what you're truly after.

Tim FOrd replied on Wednesday, June 07, 2006

Hi Mark,

That does actaully clear things up for me. I'm pretty sure i could actually use the BackgroundWorker component to handle the threading of the business object from the UI and thus acheiving and Asynchronous result.

Thanks, Tim.

AKaplan replied on Friday, April 17, 2009

I would like to add to this discussion with asynchronous data calls. This is my structure of my asynchronous ado.net.

Private Sub ExecuteFetch(ByVal cn As SqlConnection)
Using cm As SqlCommand = cn.CreateCommand
cm.CommandType = CommandType.StoredProcedure
cm.CommandText = "esp_SelectTaxRatesAll"
cm.BeginExecuteReader(New AsyncCallback(AddressOf FetchResult), cm, CommandBehavior.CloseConnection).AsyncWaitHandle.WaitOne(3000, True)
End Using
End Sub

Private Sub FetchResult(ByVal ar As IAsyncResult)
Dim cm As SqlCommand = CType(ar.AsyncState, SqlCommand)
If Not ar.IsCompleted Then
Using cm.Connection
Using dr As New SmartSafeDataReader(cm.EndExecuteReader(ar))
While dr.Read()
Me.Add(TaxRate.GetTaxRate(dr))
End While
End Using
End Using
End If
End Sub

What happens in this manner is that ExecuteReader is executed and then csla continues on without the results of the DB. After the UI has binded, the callback has it's results and tries to populate the BO. I get an error of "The asynchronous callback has already completed.", and a TypeLoadException is thrown. Is there any way I can workaround this and still keep asynchronous ado, or am I just going to have to fall back to synchronous?

ajj3085 replied on Friday, April 17, 2009

Well, this is a rather old thread. The latest version of Csla has Asyncronous dataportal support, because it was required for Silverlight. There are some gotchas though.. the UI must be able to handle getting a null back instead of a child instance. Search around the forum for more details.

AKaplan replied on Friday, April 17, 2009

thanks

AKaplan replied on Friday, April 17, 2009

I'm using csla 3.5 with asp.net. Are you saying if I use csla 3.5 asynchronous data access, i have to silverlight? or can it be used with asp.net?

ajj3085 replied on Monday, April 20, 2009

No, I'm saying that if you use Silverlight, all DataPortal calls have to be asynchronous.... but that's a silverlight thing.

I don't think async would benefit much on asp.net... how would you get the result back? By then the page would be on the client's browser.

Paul Czywczynski replied on Monday, April 20, 2009

It is useful for some ASP.NET web sites but not a cure all for a badly designed application.
With asynchronous pages, a thread which assigned from the thread-pool
to process page request, is returned to the thread pool instead of
waiting for an I/O operation to complete. This means more threads
available to process incoming requests, and when I/O operation is
completed, another thread from the pool is guarantee to send back the
response.
http://blogs.microsoft.co.il/blogs/egady/archive/2008/09/29/asynchronous-asp-net-2-0-programming.aspx

RockfordLhotka replied on Monday, April 20, 2009

The async data portal feature could, technically, be used in an ASP.NET scenario, as long as you use some sort of thread synchronization primitive to block the main "UI thread" until all the async data portal calls return.

In reality, you are probably better off using sync data portal calls, and managing your own threading outside of the data portal - in the ASP.NET environment.

The async data portal works very well in Silverlight and WPF. It works ok in Windows Forms. But in the block mode world of ASP.NET or other pure server-side settings, I think you are usually better off managing your own threads.

I say this, because the async data portal can automatically do thread marshalling in SL/WPF/WinForms thanks to Microsoft providing the underlying plumbing. But in server-side scenarios Microsoft didn't build any plumbing to do the heavy lifting, and there's no way for me to predict how someone might use threading in their application, so I can't really implement that plumbing either.

So it is, in the end, up to you to decide how to synchronize your threads on the server - which means you are better off using the sync data portal and doing all the heavy lifting in your own code.

Copyright (c) Marimer LLC