65.9K
CodeProject is changing. Read more.
Home

How to Implement a Custom Oracle Session Provider

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.57/5 (3 votes)

Jun 27, 2008

CPOL

1 min read

viewsIcon

34870

downloadIcon

1033

Oracle session provider

Introduction

This tutorial is intended to be a beginner's introduction to custom session providers. Since the introduction of ASP.NET 2.0, Microsoft has allowed you to tap into its session-state store provider via the SessionStateStoreProviderBase class. The session-state store inherits from the abstract SessionStateStoreProviderBase which is made up of the following 12 methods:

  • CreateNewStoreData
  • CreateUninitializedItem
  • Dispose
  • GetItem
  • GetItemExclusive
  • Initialize
  • InitializeRequest
  • ReleaseItemExclusive
  • RemoveItem
  • ResetItemTimeout
  • SetAndReleaseItemExclusive
  • SetItemExpireCallback

You can read more about these methods here.

Issue

One of the requirements of my most recent projects was for session data to be stored in Oracle. While this is relatively easy to implement using the built in SQL server provider, it is less obvious how to do this with Oracle. After an extensive web search, I found little information on this subject. Neither Microsoft nor Oracle provided working examples of using Oracle as your session data store. However, Microsoft did have a decent example on how to implement a custom session provider using an Access database. This example was my starting point, and the basic template I used for this article.

Using the Code

In the Web.config:

  1. Add the Oracle database connection string.
  2. Add the custom s<place w:st="on"><placename w:st="on">ession-s<placetype w:st="on">tate section.
    <configuration>
       <connectionStrings>
         <add name="COMPANY" connectionString="Data Source=spindev;Persist Security Info=True;
         User ID=username;Password=password;Min Pool Size=3;" 
         providerName="System.Data.OracleClient"/>
       </connectionStrings>
    </configuration>
    
    <system.web>
      <sessionState mode="Custom" customProvider="CustomSessionData" 
      cookieless="false" timeout="1">
        <providers>
          <add name="CustomSessionData" type="CustomSessionData" />
        </providers>
      </sessionState>
    </system.web>
  3. Add the custom OracleSessionProvider to your app_code folder.

Note: At least one session variable must be set for the provider to be initialized.

Summary

I have provided you with an example of the basics of implementing a custom session-state provider using an Oracle DB. There are other issues involved in managing session (i.e., session_end), but this should get you started.