Automatic fix for tempdb error related to 'ASPStateTempSessions'

Both answers seem valid; but with most things Microsoft, its all in the setup...

First uninstall the ASPState database by using the command:

aspnet_regsql –ssremove –E -S .

Note:

-E is to indicate you want to use integrated security connection.

-S informs what SQL server and SQL instance to use, and the "." (dot) specifies default local instance

Then re-install using the command:

aspnet_regsql –ssadd –sstype p –E -S .

Note:

The sstype has three options, t | p | c ... the first "t", tells the installer to host all stored procedures in the ASPState database, and all data in the tempdb. The second option "p" tells the installer to persist data to the ASPState database. The last option "c" allows you to specify a different 'custom' database to persist the session state data.

If you reinstall using the "-sstype p" you then need only to supply datareader/datawriter to the ASPState database for the user that's making the connection (in most cases, the application pool's identity in IIS).

The added benefit of persisting the data is that session state is retained even after a restart of the service. The only drawback is that you need to ensure the agent cleanup job is pruning old sessions regularly (it does this by default, every minute).

Important:

If you are running a cluster, you must persist session data. You're only option is to use sstype 'p' or 'c'.

Hope this sheds light on the issue!


For the record, I did find a way to do this.

The issue is that the tempdb is recreated from the model db each time the service restarts. The gist of the solution is to create a stored procedure that does the job, and then make that procedure run at startup.

Source code (credit to the link above) is as follows:

use master
go

-- remove an old version
drop proc AddAppTempDBOwner
go

-- the sp
create proc AddAppTempDBOwner as
declare @sql varchar(200)
select @sql = 'use tempdb' + char(13) + 'exec sp_addrolemember ''db_owner'', ''app'''
exec (@sql)
go

-- add it to the startup
exec sp_procoption 'AddAppTempDBOwner', 'startup', 'true'
go