views:

362

answers:

2

We have 2 databases, say DB1 and DB2.
DB1 contains all the stored procedures which access also data in DB2.
DB1 uses synonyms to access the tables in DB2.
(Using synonyms is a requirement in our situation)

This works perfectly fine in all situations with SQL Server 2005 Developer Edition.

However in the Express Edition, we get an exception when we do the following:
1 Restart SQL Server
2 Execute the following code within DB1:

set transaction isolation level snapshot
begin transaction
declare @sQuery varchar(max)
set @sQuery = 'Select * from synToSomeTableInDB2'
exec (@sQuery)
commit transaction

This will result in the following error:

Snapshot isolation transaction failed in database '...' because the database was not recovered when the current transaction was started. Retry the transaction after the database has recovered.

The same select query passes fine when used without the EXEC or when run on the Developer Edition.
Restarting the server in step 1 is important as once a connection was made to DB2, the code runs also fine on SQL Server Express Edition.

Does anyone have an idea what this is? We need to be able to use EXEC for some dynamic queries. We've already checked MSDN, searched Google, ... Any help is greatly appreciated.

--- Edit: March 10 09
As discussed with Ed Harper below, I've filed a bug report for this.
See https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=422150

+1  A: 

The error message suggests that the query fails because SQL server is still recovering the database following the service restart when you execute your query.

Does the error always occur on the first attempt to run this code, regardless of the time elapsed since the service was restarted?

Can you confirm from the SQL Server log that the database is recovering correctly after the restart?

Ed Harper
The restart is only required in order to make sure no connection to DB2 was done before. The error happens regardless of the time elapsed, as long as no connection to DB2 has been made before.
Marc
I've checked the Server log: There's no error. It says "Recovery is complete" and "Starting up DB1/DB2". Seems to be all fine.
Marc
Sounds like a bug in Express. Can you work around it by having a failing transaction run after the database service is restarted.
Ed Harper
I'm sorry I don't understand what you mean with failing transaction. I can't work around this problem at all. The only fixes I've found is using Developer Edition or changing the transaction isolation level to not use Snapshot or to make a dummy connection to DB2 first before executing this code.
Marc
I don't consider any of these 3 fixes a solution, except maybe the third but it's still strange this is needed.
Marc
It was the third option I meant - sorry not to be clearer
Ed Harper
I'm fairly sure it's a bug; it seems as if, with the combination of features you're using, something prevents the cross-database synonyms from properly detecting the target database is started, until the first (failing) statement has been executed.
Ed Harper
OK thanks a lot for your help. I'll see how I can file a bug report for MS. From what I read via Google this seems to be almost impossible... Maybe we have something via MSDN.
Marc
I've created a bug report via Microsoft Connect: https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=422150
Marc
Microsoft replyed in MS Connect. The problem was with the AUTO_CLOSE option. See my answer to this question. Thanks again for your help!
Marc
+1  A: 

As found out via Microsoft Connect, the problem is that by default on SQL Server Express Edition the AUTO_CLOSE option is set on true.
Changing this option to false fixes the problem.

Marc