Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top