Skip to content

Commit 1bb290d

Browse files
committed
Merge pull request couchbaselabs#16 from couchbaselabs/make-cluster-static
Make cluster and bucket static
2 parents abaea25 + 82254f2 commit 1bb290d

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

Couchbase.AspNet/SessionState/CouchbaseSessionStateProvider.cs

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ namespace Couchbase.AspNet.SessionState
1212
/// </summary>
1313
public class CouchbaseSessionStateProvider : SessionStateStoreProviderBase
1414
{
15-
private ICluster _cluster;
16-
private IBucket _bucket;
15+
private static ICluster _cluster;
16+
private static IBucket _bucket;
1717
private static bool _exclusiveAccess;
1818
private int _maxRetryCount = 5;
19+
private static object _syncObj = new object();
1920

2021
/// <summary>
2122
/// Required default ctor for ASP.NET
@@ -79,17 +80,23 @@ public override void Initialize(string name, NameValueCollection config)
7980
// Initialize the base class
8081
base.Initialize(name, config);
8182

82-
if (_cluster == null)
83+
lock (_syncObj)
8384
{
84-
// Create our Cluster based off the CouchbaseConfigSection
85-
_cluster = ProviderHelper.GetCluster(name, config);
86-
}
87-
if (_bucket == null)
88-
{
89-
// Create the bucket based off the name provided in the
90-
_bucket = ProviderHelper.GetBucket(name, config, _cluster);
85+
if (_cluster == null)
86+
{
87+
// Create our Cluster based off the CouchbaseConfigSection
88+
_cluster = ProviderHelper.GetCluster(name, config);
89+
}
90+
if (_bucket == null)
91+
{
92+
// Create the bucket based off the name provided in the
93+
_bucket = ProviderHelper.GetBucket(name, config, _cluster);
94+
}
95+
else
96+
{
97+
ProviderHelper.GetAndRemove(config, "bucket", false);
98+
}
9199
}
92-
93100
// By default use exclusive session access. But allow it to be overridden in the config file
94101
var exclusive = ProviderHelper.GetAndRemove(config, "exclusiveAccess", false) ?? "true";
95102
_exclusiveAccess = (string.Compare(exclusive, "true", StringComparison.OrdinalIgnoreCase) == 0);
@@ -121,10 +128,13 @@ public override void Initialize(string name, NameValueCollection config)
121128
/// </summary>
122129
public override void Dispose()
123130
{
124-
if (_cluster != null)
131+
lock (_syncObj)
125132
{
126-
_cluster.Dispose();
127-
_cluster = null;
133+
if (_cluster != null)
134+
{
135+
_cluster.Dispose();
136+
_cluster = null;
137+
}
128138
}
129139
}
130140

0 commit comments

Comments
 (0)