본문 바로가기

프로그래밍/ASP NET

Session 관리 ( Inproc,StateServer,SQL Server )

Session 관리

 

1. 세션 상태 모드

 

1) InProc ( Default )

 

- 세션 정보를 웹서버 메모리에 저장

- 데이터베이스 이용하는 것보다 성능 우월

- 웹 서비스 재부팅시 모든 세션 사라짐

 

 

2) StateServer

 

- 윈도우 서버 관리도구에 있는 ASP.NET State Service 라는 서비스 데몬의 메모리 이용

- IIS 재부팅 되어도 세션 정보 유지

- 하나의 서비스 데몬이므로 여러개의 웹서버에서 하나의 세션관리 가능해짐

- 윈도우 시작시 수정 시작 설정되어 있음 ( StateServer 를 하려면 자동 고려 하거나 서비스 시작 시켜야 함 )

- 웹서버 재부팅시에는 세션 사라짐

 

 

3) SQL Server

 

- 세션 정보를 SQL Server 에서 저장 관리

- 웹서버 재부팅되어도 세션 유지

- 여러개의 웹서버에서 하나의 세션관리 가능

- 물리적인 디스크 I/O 가 발생되어 가장 느린 상테 관리 방법

 

 

2. 세션 상태 모드 설정

 

1) web.config 에서 설정

 

 

2) 각 소스상에서 System.Web.SessionState.HttpSession-State.Mode 속성으로 셋팅

 

 

 

[SQL Server Settion 설정 방법]

 

1. DB 에 ASPState DB 생성

 

< DB 서버 cmd 창에서 아래의 경로로 aspnet_regsql 실행 >

 

C:\Windows\Microsoft.NET\Framework\v4.0.30319>aspnet_regsql -S "ServerHost" -U sa -P [sa password] -ssadd -sstype c -d "ASPState"

(ex: C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727>aspnet_regsql -S localhost -U sa -P your-password -ssadd -sstype p)

 

위 명령어를 치면 아래 < 그림1 > 의 ASPState 라는 DB 및 관련 SP 들이 생김

 

<주의사항>

Password 등의 글자에 "&" 등이 있으면 전체 명령어를 인식하는것이 아닌 "&" 앞까지 인식.

따라서 암호에 "&" 가 있으면 aspnet_regsql -S localhost -U sa -ssadd -sstype p 로 "-P"를 빼서 명령어를 친 다음 암호 입력창이 나오면 암호입력

 

 

 

2. Web.config 설정

 

- 사용하고자 하는 User ID의 DB 계정은 ASPState에 대해 db_owner 이거나 아니면 최소한 db_datareader, db_datawriter 역할과 자동 생성되는 Stored procedure에 대해 실행 권한이 있어야 함.

 

 

< 그림1 >

 

 

 

 

< aspnet_regsql 파라미터 >

 

Parameter

Description

-S [Server Name]  Sql Server name or IP address, where sessions will be stored
-E   Windows authentication is used to access SQL Server
-U [User Name]  User name, if SQL authentication is used
-P [Password]  Password, if SQL authentication is used
-D [Database Name]  Database name. This parameter is used if parameter -sstype is equal to "c", which means that custom database is used as session storage.
-ssadd  Abbreviation of "Session State Add". This option adds session state database.
-ssremove   This parameter removes existing session state database.
-sqlexportonly [Script File Name or Full Path]

 If this parameter is used, program will not execute any SQL, but just create T-SQL script which can be executed later on server(s). This is useful in shared hosting scenario.

Type of session state storage. There are three possible values:

-sstype [t|p|c]  t, means temporary storage. Tables are created in temdb database. In this case session is lost if SQL Server restarts,
 p, represents persistent storage. Session state tables are created in AspState database,
 c, custom storage. In this case, you specify database name where sessions will be stored. Database name is set using -D parameter.

 

 

< ASPStateTempSessions 테이블, ASPStateTempApplications 테이블 내용 >

 

- 사이트에 접속된 사용자가 있으면 ( 처음 이거나, 세션기간만료 사용자 ) ASPStateTempSessions 테이블에 Row 생성 ( 만료일자: web.config 값에 따라 셋팅 )