PDA

View Full Version : Cancel a query



juflo
11-08-2007, 12:39 PM
Hi all

I was looking for a way for canceling a long-run query. I've first thought that it was not possible easily.
Then, I look into OracleCommand (yes, I'm using Oracle, through NHibernate) and see that there was a Cancel() method.
Then I was also hapilly surprised that there was a CancelQuery() on NHibernate.ISession.

So, the question is, is there a way to call one of these methods ?
Btw, I'm using NHibernateTemplate and TransactionInterceptor.

I will try the following for testing :
SessionFactoryUtils.GetSession(...).CancelQuery()

What do you think about creating an easy way for this in Spring ? Maybe by cancelling the current transaction (smth on TransactionInterceptor) ?

Thx.

Mark Pollack
11-12-2007, 03:45 PM
Hi,

I'd imagine that you will need to call the CancelSession method from another thread. If so when that 'monitor' thread is created you can give it access to the session you might like to cancel. In this case calling the method to cancel the session is straightforward.

Alternatively, if there is a callback function (delegate) on the DAO object, then you could set up an async callback to that method (based on a timer), passing in the active session as one of the arguments. The callback would then be handed the correct session to cancel when it gets invoked.

Something along the lines of the second approach I mention could probably be encapsulated a little bit more, though my opinion atm is that it would be best to make people aware of possible approaches and leave the implementation up to the developer. Let us know how your experiments turn out so we can see if some general pattern can be harvested and encapsulated into the library.

Cheers,
Mark

juflo
11-20-2007, 12:58 PM
Thanks Mark.

Yes, it's quite simple, as long as you have the Session object to call Cancel() on.

To be more precise, my case is a Winform app, using BackgroundWorker. I would like to be able to cancel the request by calling a Cancel() method from the UI, without dealing with Session on the UI layer.
ATM, I'm a bit stuck on how to "nicely" reuse the session created by TransactionInterceptor. Using a callback may be the best solution for that. I'll let you know.

All ideas are welcome.

Thanks.