Permalink
Browse files

updated to use Mongo driver .82.2

  • Loading branch information...
1 parent cdc8c71 commit 4761795d9371764b069adfdb0c11a6888843a4c7 @Sedward Sedward committed May 25, 2010
@@ -31,9 +31,9 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
- <Reference Include="MongoDB.Driver, Version=1.0.3729.17015, Culture=neutral, processorArchitecture=MSIL">
+ <Reference Include="MongoDB, Version=0.82.2.0, Culture=neutral, PublicKeyToken=f61bd00ba2535278, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
- <HintPath>..\..\mongo\mongodb-csharp\MongoDBDriver\bin\Debug\MongoDB.Driver.dll</HintPath>
+ <HintPath>..\..\mongo\mongodb-csharp\source\MongoDB\bin\Debug\MongoDB.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.configuration" />
@@ -8,8 +8,8 @@
using System.Configuration;
using System.Configuration.Provider;
using System.Web.SessionState;
-using MongoDB.Driver;
-using MongoDB.Driver.Configuration;
+using MongoDB;
+using MongoDB.Configuration;
namespace MongoSessionStore
@@ -80,15 +80,16 @@ public override void SetAndReleaseItemExclusive(HttpContext context, string id,
byte[] serializedItems = Serialize((SessionStateItemCollection)item.Items);
Binary sessionItems = new Binary(serializedItems);
+ var sessionStore = SessionStore.Instance;
if (newItem)
{
// Delete an existing expired session if it exists.
- SessionStore.EvictExpiredSession(id, _applicationName);
+ sessionStore.EvictExpiredSession(id, _applicationName);
// insert new session item.
Session session = new Session(id, this._applicationName, item.Timeout, sessionItems, item.Items.Count,0);
- SessionStore.Insert(session);
+ sessionStore.Insert(session);
}
else
{
@@ -170,7 +171,7 @@ public override void SetAndReleaseItemExclusive(HttpContext context, string id,
else if(session.Expires < DateTime.Now)
{
locked = false;
- SessionStore.EvictSession(session);
+ SessionStore.Instance.EvictSession(session);
}
else if (session.Locked)
@@ -300,9 +301,10 @@ public override void ReleaseItemExclusive(HttpContext context, string id, object
public override void RemoveItem(HttpContext context, string id, object lockId, SessionStateStoreData item)
{
+ var sessionStore = SessionStore.Instance;
try
{
- SessionStore.EvictSession(id, this._applicationName, lockId);
+ sessionStore.EvictSession(id, this._applicationName, lockId);
}
catch (Exception e)
{
@@ -324,7 +326,7 @@ public override void CreateUninitializedItem(HttpContext context,string id,int t
try
{
- SessionStore.Insert(session);
+ SessionStore.Instance.Insert(session);
}
catch (Exception e)
{
@@ -3,8 +3,8 @@
using System.Collections.Generic;
using System.Web.SessionState;
using System.Text;
-using MongoDB.Driver;
-using MongoDB.Driver.Configuration;
+using MongoDB;
+using MongoDB.Configuration;
namespace MongoSessionStore
{
@@ -23,7 +23,7 @@ public class Session
private int _flags;
- private OidGenerator oGen;
+ //private OidGenerator oGen;
public Session() { }
@@ -1,44 +1,61 @@
using System;
using System.Collections.Generic;
using System.Configuration;
-using MongoDB.Driver;
-using MongoDB.Driver.Configuration;
-using MongoDB.Driver.Connections;
-using MongoDB.Driver.Serialization;
+using MongoDB;
+using MongoDB.Configuration;
+using MongoDB.Connections;
using System.Text;
namespace MongoSessionStore
{
public sealed class SessionStore
{
+ static MongoConfigurationBuilder configure = new MongoConfigurationBuilder();
+ static MongoConfiguration config;
+ private static volatile SessionStore instance;
+ private static object syncRoot = new Object();
- static MongoConfiguration config = (MongoConfiguration)System.Configuration.ConfigurationManager.GetSection("Mongo");
- static Connection conn = ConnectionFactory.GetConnection(config.Connections["mongoserver"].ConnectionString);
- static MongoDatabase db = new MongoDatabase(SerializationFactory.Default,conn, "SessionTest");
- static IMongoCollection sessions = db.GetCollection("sessions");
+ private SessionStore()
+ {
+ configure = new MongoConfigurationBuilder();
+ configure.ConnectionStringAppSettingKey("mongoserver");
+ config = configure.BuildConfiguration();
+ }
- public SessionStore()
+ public static SessionStore Instance
{
+ get
+ {
+ if (instance == null)
+ {
+ lock (syncRoot)
+ {
+ if (instance == null)
+ instance = new SessionStore();
+ }
+ }
+ return instance;
+ }
}
- public static void Insert(Session session)
+
+ public void Insert(Session session)
{
Document newSession = new Document() { { "SessionId",session.SessionID }, {"ApplicationName",session.ApplicationName},{"Created",session.Created},
{"Expires",session.Expires},{"LockDate",session.LockDate},{"LockId",session.LockID},{"Timeout",session.Timeout},{"Locked",session.Locked},
{"SessionItems",session.SessionItems},{"SessionItemsCount",session.SessionItemsCount},{"Flags",session.Flags}};
try
{
- conn.Open();
- sessions.Insert(newSession);
+ using (var mongo = new Mongo(config))
+ {
+ mongo.Connect();
+ mongo["session_store"]["sessions"].Insert(newSession);
+ }
}
catch (MongoException ex)
{
throw new Exception("Could not insert a new session", ex);
}
- finally
- {
- conn.Close();
- }
}
@@ -48,8 +65,15 @@ public static Session Get(string id, string applicationName)
Session session;
try
{
- conn.Open();
- Document sessionDoc = sessions.FindOne(selector);
+
+ Document sessionDoc;
+
+ using (var mongo = new Mongo(config))
+ {
+ mongo.Connect();
+ sessionDoc = mongo["session_store"]["sessions"].FindOne(selector);
+ }
+
if (sessionDoc == null)
{
session = null;
@@ -58,37 +82,34 @@ public static Session Get(string id, string applicationName)
{
session = new Session(sessionDoc);
}
-
+
}
catch (MongoException ex)
{
throw new Exception("Could not insert a new session", ex);
}
- finally
- {
- conn.Close();
- }
- return session;
+
+ return session;
}
public static void UpdateSession(string id, int timeout, Binary sessionItems, string applicationName, int sessionItemsCount, object lockId)
{
try
{
- Document selector = new Document() { { "SessionId", id }, { "ApplicationName", applicationName },{"LockId",lockId} };
- 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);
+ Document selector = new Document() { { "SessionId", id }, { "ApplicationName", applicationName }, { "LockId", lockId } };
+ Document session = new Document() { { "$set", new Document() { { "Expires", DateTime.Now.AddMinutes((double)timeout) }, { "Timeout", timeout }, { "Locked", false }, { "SessionItems", sessionItems }, { "SessionItemsCount", sessionItemsCount } } } };
+ using (var mongo = new Mongo(config))
+ {
+ mongo.Connect();
+ mongo["session_store"]["sessions"].Update(session, selector, 0, false);
+ }
}
catch (MongoException ex)
{
throw new Exception("Could not insert a new session", ex);
}
- finally
- {
- conn.Close();
- }
+
}
public static void UpdateSessionExpiration(string id, string applicationName, double timeout)
@@ -97,112 +118,108 @@ public static void UpdateSessionExpiration(string id, string applicationName, do
{
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);
+ using (var mongo = new Mongo(config))
+ {
+ mongo.Connect();
+ mongo["session_store"]["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)
+ public void EvictSession(Session session)
{
- Document selector = new Document() { { "SessionId", session.SessionID }, { "ApplicationName", session.ApplicationName },{"LockId",session.LockID} };
+ Document selector = new Document() { { "SessionId", session.SessionID }, { "ApplicationName", session.ApplicationName }, { "LockId", session.LockID } };
try
{
- conn.Open();
- sessions.Delete(selector);
+ using (var mongo = new Mongo(config))
+ {
+ mongo.Connect();
+ mongo["session_store"]["sessions"].Remove(selector);
+ }
}
catch (MongoException ex)
{
throw new Exception("There was a problem when evicting the session with SessionId:" + session.SessionID, ex);
}
- finally
- {
- conn.Close();
- }
+
}
- public static void EvictSession(string id, string applicationName, object lockId)
+ public void EvictSession(string id, string applicationName, object lockId)
{
- Document selector = new Document() {{"SessionId", id }, { "ApplicationName", applicationName }, { "LockId", lockId}};
+ Document selector = new Document() { { "SessionId", id }, { "ApplicationName", applicationName }, { "LockId", lockId } };
try
{
- conn.Open();
- sessions.Delete(selector);
+ using (var mongo = new Mongo(config))
+ {
+ mongo.Connect();
+ mongo["session_store"]["sessions"].Remove(selector);
+ }
}
catch (MongoException ex)
{
throw new Exception("There was a problem when evicting the session with SessionId:" + id, ex);
}
- finally
- {
- conn.Close();
- }
}
- public static void EvictExpiredSession(string id, string applicationName)
+ public void EvictExpiredSession(string id, string applicationName)
{
Document selector = new Document() { { "SessionId", id }, { "ApplicationName", applicationName },
{"Expires",new Document(){{"$lt",DateTime.Now}} }};
try
{
- conn.Open();
- sessions.Delete(selector);
+ using (var mongo = new Mongo(config))
+ {
+ mongo.Connect();
+ mongo["session_store"]["sessions"].Remove(selector);
+ }
}
catch (MongoException ex)
{
throw new Exception("There was a problem when evicting the session with SessionId:" + id, ex);
}
- finally
- {
- conn.Close();
- }
}
public static void LockSession(Session session)
{
- Document selector = new Document() {{"SessionId", session.SessionID }, {"ApplicationName", session.ApplicationName}};
+ Document selector = new Document() { { "SessionId", session.SessionID }, { "ApplicationName", session.ApplicationName } };
Document sessionLock = new Document() { { "$set", new Document() {{"LockDate", DateTime.Now },
{"LockId", session.LockID }, {"Locked", true }, {"Flags",0} } } };
try
{
- conn.Open();
- sessions.Update(sessionLock, selector, 0, false);
+ using (var mongo = new Mongo(config))
+ {
+ mongo.Connect();
+ mongo["session_store"]["sessions"].Update(sessionLock, selector, 0, false);
+ }
}
catch (MongoException ex)
{
throw new Exception("There was a problem when locking the session with SessionId:" + session.SessionID, ex);
}
- finally
- {
- conn.Close();
- }
+
}
public static void ReleaseLock(string id, string applicationName, object lockId, double timeout)
{
- Document selector = new Document() { { "SessionId", id }, { "ApplicationName", applicationName},{"LockId",lockId }};
- Document sessionLock = new Document() { { "$set", new Document() {{"Expires", DateTime.Now.AddMinutes(timeout)}, {"Locked", false }}}};
+ Document selector = new Document() { { "SessionId", id }, { "ApplicationName", applicationName }, { "LockId", lockId } };
+ Document sessionLock = new Document() { { "$set", new Document() { { "Expires", DateTime.Now.AddMinutes(timeout) }, { "Locked", false } } } };
try
{
- conn.Open();
- sessions.Update(sessionLock, selector, 0, false);
+ using (var mongo = new Mongo(config))
+ {
+ mongo.Connect();
+ mongo["session_store"]["sessions"].Update(sessionLock, selector, 0, false);
+ }
}
catch (MongoException ex)
{
throw new Exception("There was a problem when releasing the lock for the session with SessionId:" + id, ex);
}
- finally
- {
- conn.Close();
- }
}
}
}
Oops, something went wrong.

0 comments on commit 4761795

Please sign in to comment.