Permalink
Browse files

Fixed some problems updating sessions Expiration Date and incorporate…

…d changes to the MongoDB driver
  • Loading branch information...
1 parent e41d766 commit 2789440ee7c15b0144d4ffe58d8bee3e31487e61 @Sedward Sedward committed Apr 7, 2010
@@ -75,11 +75,12 @@ public override bool SetItemExpireCallback(SessionStateItemExpireCallback expire
public override void SetAndReleaseItemExclusive(HttpContext context, string id, SessionStateStoreData item, object lockId, bool newItem)
{
- byte[] serializedItems = Serialize((SessionStateItemCollection)item.Items);
- Binary sessionItems = new Binary(serializedItems);
-
- try
+ try
{
+
+ byte[] serializedItems = Serialize((SessionStateItemCollection)item.Items);
+ Binary sessionItems = new Binary(serializedItems);
+
if (newItem)
{
// Delete an existing expired session if it exists.
@@ -4,6 +4,7 @@
using MongoDB.Driver;
using MongoDB.Driver.Configuration;
using MongoDB.Driver.Connections;
+using MongoDB.Driver.Serialization;
using System.Text;
namespace MongoSessionStore
@@ -13,7 +14,7 @@ public sealed class SessionStore
static MongoConfiguration config = (MongoConfiguration)System.Configuration.ConfigurationManager.GetSection("Mongo");
static Connection conn = ConnectionFactory.GetConnection(config.Connections["mongoserver"].ConnectionString);
- static Database db = new Database(conn, "SessionTest");
+ static MongoDatabase db = new MongoDatabase(SerializationFactory.Default,conn, "SessionTest");
static IMongoCollection sessions = db.GetCollection("sessions");
public SessionStore()
@@ -76,7 +77,7 @@ public static void UpdateSession(string id, int timeout, Binary sessionItems, st
{
Document selector = new Document() { { "SessionId", id }, { "ApplicationName", applicationName },{"LockId",lockId} };
- Document session = new Document() { { "$set", new Document() { { "LockDate", DateTime.Now.AddMinutes((double)timeout) },{"Timeout",timeout},{"Locked",false},{ "SessionItems", sessionItems },{"SessionItemsCount",sessionItemsCount}} } };
+ Document session = new Document() { { "$set", new Document() { { "Expires", DateTime.Now.AddMinutes((double)timeout) },{"Timeout",timeout},{"Locked",false},{ "SessionItems", sessionItems },{"SessionItemsCount",sessionItemsCount}}} };
conn.Open();
sessions.Update(session, selector, 0, false);
}
@@ -92,8 +93,21 @@ public static void UpdateSession(string id, int timeout, Binary sessionItems, st
public static void UpdateSessionExpiration(string id, string applicationName, double timeout)
{
- Document selector = new Document() { { "SessionId", id}, { "ApplicationName", applicationName} };
- Document sessionUpdate = new Document() { { "$set", new Document() { { "Expires", DateTime.Now.AddMinutes(timeout)} }}};
+ try
+ {
+ Document selector = new Document() { { "SessionId", id }, { "ApplicationName", applicationName } };
+ Document sessionUpdate = new Document() { { "$set", new Document() { { "Expires", DateTime.Now.AddMinutes(timeout) } } } };
+ conn.Open();
+ sessions.Update(sessionUpdate, selector, 0, false);
+ }
+ catch (MongoException ex)
+ {
+ throw new Exception("Could not update Session Expiration", ex);
+ }
+ finally
+ {
+ conn.Close();
+ }
}
public static void EvictSession(Session session)
@@ -0,0 +1,16 @@
+<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="LoadTestOne.aspx.cs" Inherits="MongoSessionTest.LoadTest" %>
+
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+
+<html xmlns="http://www.w3.org/1999/xhtml" >
+<head runat="server">
+ <title></title>
+</head>
+<body>
+ <form id="form1" runat="server">
+ <div>
+
+ </div>
+ </form>
+</body>
+</html>
@@ -0,0 +1,19 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Web;
+using System.Web.UI;
+using System.Web.UI.WebControls;
+
+namespace MongoSessionTest
+{
+ public partial class LoadTest : System.Web.UI.Page
+ {
+ protected void Page_Load(object sender, EventArgs e)
+ {
+ MockUser user = new MockUser();
+ Session.Add("User", user);
+ Response.Redirect("LoadTestTwo.aspx");
+ }
+ }
+}

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -0,0 +1,16 @@
+<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="LoadTestTwo.aspx.cs" Inherits="MongoSessionTest.LoadTestTwo" %>
+
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+
+<html xmlns="http://www.w3.org/1999/xhtml" >
+<head runat="server">
+ <title></title>
+</head>
+<body>
+ <form id="form1" runat="server">
+ <div>
+
+ </div>
+ </form>
+</body>
+</html>
@@ -0,0 +1,18 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Web;
+using System.Web.UI;
+using System.Web.UI.WebControls;
+
+namespace MongoSessionTest
+{
+ public partial class LoadTestTwo : System.Web.UI.Page
+ {
+ protected void Page_Load(object sender, EventArgs e)
+ {
+ MockUser user = (MockUser)Session["User"];
+ Session.Abandon();
+ }
+ }
+}

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -54,17 +54,56 @@
</ItemGroup>
<ItemGroup>
<Content Include="Default.aspx" />
+ <Content Include="RedirectedPage.aspx" />
<Content Include="Web.config" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="App_Code\MockUser.cs" />
+ <Content Include="LoadTestOne.aspx" />
+ <Content Include="LoadTestTwo.aspx" />
+ <Content Include="Test.aspx" />
<Compile Include="Default.aspx.cs">
<SubType>ASPXCodeBehind</SubType>
<DependentUpon>Default.aspx</DependentUpon>
</Compile>
<Compile Include="Default.aspx.designer.cs">
<DependentUpon>Default.aspx</DependentUpon>
</Compile>
+ <Compile Include="LoadTestOne.aspx.cs">
+ <DependentUpon>LoadTestOne.aspx</DependentUpon>
+ <SubType>ASPXCodeBehind</SubType>
+ </Compile>
+ <Compile Include="LoadTestOne.aspx.designer.cs">
+ <DependentUpon>LoadTestOne.aspx</DependentUpon>
+ </Compile>
+ <Compile Include="LoadTestTwo.aspx.cs">
+ <DependentUpon>LoadTestTwo.aspx</DependentUpon>
+ <SubType>ASPXCodeBehind</SubType>
+ </Compile>
+ <Compile Include="LoadTestTwo.aspx.designer.cs">
+ <DependentUpon>LoadTestTwo.aspx</DependentUpon>
+ </Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="RedirectedPage.aspx.cs">
+ <DependentUpon>RedirectedPage.aspx</DependentUpon>
+ <SubType>ASPXCodeBehind</SubType>
+ </Compile>
+ <Compile Include="RedirectedPage.aspx.designer.cs">
+ <DependentUpon>RedirectedPage.aspx</DependentUpon>
+ </Compile>
+ <Compile Include="Test.aspx.cs">
+ <DependentUpon>Test.aspx</DependentUpon>
+ <SubType>ASPXCodeBehind</SubType>
+ </Compile>
+ <Compile Include="Test.aspx.designer.cs">
+ <DependentUpon>Test.aspx</DependentUpon>
+ </Compile>
+ </ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\MongoSessionStore\MongoSessionStore.csproj">
+ <Project>{354D379B-E16C-40DE-B26F-8CCF2EDE94D8}</Project>
+ <Name>MongoSessionStore</Name>
+ </ProjectReference>
</ItemGroup>
<ItemGroup>
<Folder Include="App_Data\" />
@@ -1,7 +1,7 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ProjectExtensions>
<VisualStudio>
- <FlavorProperties GUID="{349c5851-65df-11da-9384-00065b846f21}" xmlns="">
+ <FlavorProperties GUID="{349c5851-65df-11da-9384-00065b846f21}">
<WebProjectProperties>
<StartPageUrl>
</StartPageUrl>
@@ -5,7 +5,7 @@
</configSections>
<Mongo>
<connections>
- <add key="mongoserver" connectionString="Server=localhost:27017"/>
+ <add key="mongoserver" connectionString="Server=localhost:27017;MaximumPoolSize=1;MinimumPoolSize=1;ConnectionTimeout=60;ConnectionLifetime=50"/>
</connections>
</Mongo>
</configuration>
@@ -3,6 +3,7 @@
using System.Text;
using System.Configuration;
using MongoDB.Driver;
+using MongoDB.Driver.Serialization;
using MongoDB.Driver.Connections;
using MongoDB.Driver.Configuration;
using NDesk.Options;
@@ -12,7 +13,7 @@ namespace MongoStoreInspector
class Program
{
private static Connection conn;
- private static Database db;
+ private static MongoDatabase db;
private static IMongoCollection sessions;
static void Main(string[] args)
@@ -39,7 +40,7 @@ static void InspectSessions()
{
MongoConfiguration config = (MongoConfiguration)System.Configuration.ConfigurationManager.GetSection("Mongo");
conn = ConnectionFactory.GetConnection(config.Connections["mongoserver"].ConnectionString);
- db = new Database(conn, "SessionTest");
+ db = new MongoDatabase(SerializationFactory.Default,conn, "SessionTest");
try
{
conn.Open();
@@ -78,7 +79,7 @@ static void FlushExpiredSession()
{
MongoConfiguration config = (MongoConfiguration)System.Configuration.ConfigurationManager.GetSection("Mongo");
conn = ConnectionFactory.GetConnection(config.Connections["mongoserver"].ConnectionString);
- db = new Database(conn, "SessionTest");
+ db = new MongoDatabase(SerializationFactory.Default,conn, "SessionTest");
try
{
conn.Open();
@@ -102,7 +103,7 @@ static void AddIndexes()
{
MongoConfiguration config = (MongoConfiguration)System.Configuration.ConfigurationManager.GetSection("Mongo");
conn = ConnectionFactory.GetConnection(config.Connections["mongoserver"].ConnectionString);
- db = new Database(conn, "SessionTest");
+ db = new MongoDatabase(SerializationFactory.Default,conn, "SessionTest");
try
{
conn.Open();
@@ -10,6 +10,7 @@
using System.Text;
using NUnit.Framework;
using MongoDB.Driver;
+using MongoDB.Driver.Serialization;
using MongoDB.Driver.Connections;
using MongoDB.Driver.Configuration;
@@ -19,7 +20,7 @@ namespace SessionStoreTest
public class SessionOpsTest
{
Connection conn;
- Database db;
+ MongoDatabase db;
IMongoCollection sessions;
string ApplicationName = "TestApp";
SessionStateStoreData item;
@@ -30,7 +31,8 @@ public void SetUp()
{
MongoConfiguration config = (MongoConfiguration)System.Configuration.ConfigurationManager.GetSection("Mongo");
conn = ConnectionFactory.GetConnection(config.Connections["mongoserver"].ConnectionString);
- db = new Database(conn, "SessionTest");
+
+ db = new MongoDatabase(SerializationFactory.Default, conn, "SessionTest");
sessions = db.GetCollection("sessions");
SessionStateItemCollection sessionItems = new SessionStateItemCollection();
@@ -114,7 +116,7 @@ public void UpdateOrInsertSession()
{"SessionItems",sessionItems},{"SessionItemsCount",item.Items.Count},{"Flags",0}};
Document selector = new Document(){{"SessionId",sessionID}};
- sessions.Update(session,selector,1,false);
+ sessions.Update(session,selector,0,false);
//Document updatedSession = sessions.FindOne(session);
Oops, something went wrong.

0 comments on commit 2789440

Please sign in to comment.