In my current task, I need to take some existing legacy code (which is using raw ADO) and make it work under NHibernate. I want to keep original sql queries as is, to ensure exactly the same behavior.
This is what original code was doing:
- Copy everything from table A to temporary table #A (using "insert" statement)
- Run external BCP utility (via command line), which is copies everything from backup file (.tbl) to table A
- Copy everything from #A to A
(for sake of discussion, let's assume this is perfectly logical and this is what required to be done)
The problem:
I can't run 1 + 3 in single statement, because I have BCP call in the middle. Therefore, I forced to perform three separate calls. So, I calling 1, then 2, then 3... and I getting error about invalid object name #A.
What actually happens? Apparently, after each query execution, NHibernate recycles connection --> which making temporary table #A to be deleted right after 1 is executed (temporary tables live until connection is open).
I need somehow to overcome this problem. Ideally, I would like somehow to tell NHibernate to keep the same connection all the time during this process.
Thanks!
P.S. I tried to execute 1+2+3 in transaction. That causes deadlock with BCP (no matter which IsolationLevel)