Question

I started using nhibernate the same time I started using castle activerecord so I have those mixed up questions sometimes

I was wondering when activerecord (or nhibernate) closes a connection when the following code runs:

Dim entity = TABLE_Y.TryFind(id)
if not entity is nothing then
    'long running process here
    response.redirect("...")
end if

I'm asking this because this long running process takes more than one hour to finish and whenever the code redirects to another page I get an ORA-03135 (connection lost contact) telling me the connection was lost, this other page has the following active record query:

Dim entity = TABLE_X.FindAll(Order.Desc(...), _
    Expression.Eq(...) And _
    Expression.Eq(...)).FirstOrDefault

which then returns the ora-03135

so I was thinking if couldn't be any connection from activerecord that didn't close before the long running process

this long running process is literally another process started by the application which wait for it to end before redirecting to another page, so even if the other process uses active records it doesn't use the same connection string or whatsoever

it's funny because I'm starting a new query on a completely different table, is activerecord trying to reuse an existing connection that timed out? I tried to add "Pooling=False" and "Validate Connection=true" with no luck

thanks in advance

Était-ce utile?

La solution

The connection will be closed when the session is disposed of, where this happens depends on your application. If you're using the module that comes with ActiveRecord then it will happen when the Application.EndRequest event fires (ie. at the end of your request), if you aren't, then you need to see where a SessionScope or TransactionScope is created and disposed (the dispose is where the connection is closed).

If you want to start a long running task and redirect before it's complete you will need to start it in another thread (eg. using ThreadPool or Tasks). You will also need to configure ActiveRecord to use the HybridWebThreadScopeInfo so that it will store the session in a thread local when the HttpContext is unavailable (which is what will happen in your background thread).

<activerecord threadinfotype="Castle.ActiveRecord.Framework.Scopes.HybridWebThreadScopeInfo, Castle.ActiveRecord">

Then in your task, wrap it in a TransactionScope or SessionScope (I prefer the former):

using(var trans = new TransactionScope()) {
   // do your stuff here...
   trans.VoteCommit();
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top