Thank you Markus! Wow, the disposal order was obvious now that you mention it.
Good to know that in general the approach looks correct. As for the DB connection, I was wondering more about the impact of using a single open database connection per http request rather than retrieving a connection from the pool per database request. In any case, It does not look like the latter is possible with this approach. Thanks again. --Corey On Jan 30, 12:21 am, Markus Zywitza <[email protected]> wrote: > Hi > > you don't need any SessionScope around the DifferentDatabaseScope. But > if you wrap it in a SessionScope, you must dispose them in reverse > order of creation, which your code fails to do. > > How to handle the connection is your own responsibility. You are using > session per request, so the connection is used only for a single > request. The method called here > SqlConnection conn = MyContext.GetCurrentSQLConnection(); > must take care of managing your connections. > > -Markus > > 2009/1/29 ccaplette <[email protected]>: > > > > > I would like to use DifferentDatabaseScope on a per request basis. > > > In this web application it will be possible for a user to switch from > > a preview to a staging view. The preview and staging databases have > > identical schemas, and only differ by the data they contain. I have > > read similar threads, and it seems that using DifferentDatabaseScope > > is the way to go. This is my first experience with Castle > > ActiveRecord and I was hoping you kind folks could either validate or > > suggest improvements to the following approach. > > > SessionScope is being initialized and disposed in a custom > > HttpApplication class, the implementation closely follows this article > > which explains how to create a session per request in a web > > application. > > >http://www.castleproject.org/activerecord/documentation/trunk/usersgu... > > > I have used DifferentDatabaseScope in the OnBeginRequest and > > OnEndRequest methods. > > > public void OnBeginRequest(object sender, EventArgs e) > > { > > HttpContext.Current.Items.Add(sessionScopekey, new SessionScope > > ()); > > > SqlConnection conn = MyContext.GetCurrentSQLConnection(); > > sqlConnection.Open(); > > HttpContext.Current.Items.Add(differentDatabaseScopeKey, new > > DifferentDatabaseScope(sqlConnection)); > > } > > > public void OnEndRequest(object sender, EventArgs e) > > { > > try > > { > > SessionScope sessionScope = HttpContext.Current.Items > > [sessionScopekey] as SessionScope; > > > if (sessionScope != null) > > { > > sessionScope.Dispose(); > > } > > > DifferentDatabaseScope ddScope = HttpContext.Current.Items > > [ddScopeKey] as DifferentDatabaseScope; > > > if (ddScope != null) > > { > > ddScope.Dispose(); > > } > > } > > catch(Exception ex) > > { > > HttpContext.Current.Trace.Warn("Error", "EndRequest: " + ex.Message, > > ex); > > } > > } > > > My questions are: > > > 1) Is DifferentDatabaseScope being used in a correct manner? I was > > not sure if I still need to declare SessionScope, and if so, if I am > > doing this in the correct order. > > > 2) Should I be worried that DifferentDatabaseScope is using a single > > database connection? From my understanding, the Session will > > generally open and close connections from a connection pool as > > needed. In this case it looks like only a single connection will be > > used by DifferentDatabaseScope, which I am afraid my lead to > > performance issues if many queries are being generated in this session > > scope. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Castle Project Users" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/castle-project-users?hl=en -~----------~----~----~----~------~----~------~--~---
