@@ -46,7 +46,7 @@ public class CosmosDBSessionStateProviderAsync : SessionStateStoreProviderAsyncB
46
46
} ;
47
47
48
48
#region CosmosDB Stored Procedures
49
- private const string CreateSessionStateItemSPID = "CreateSessionStateItem " ;
49
+ private const string CreateSessionStateItemSPID = "CreateSessionStateItem2 " ;
50
50
private const string GetStateItemSPID = "GetStateItem2" ;
51
51
private const string GetStateItemExclusiveSPID = "GetStateItemExclusive" ;
52
52
private const string ReleaseItemExclusiveSPID = "ReleaseItemExclusive" ;
@@ -55,7 +55,7 @@ public class CosmosDBSessionStateProviderAsync : SessionStateStoreProviderAsyncB
55
55
private const string UpdateSessionStateItemSPID = "UpdateSessionStateItem" ;
56
56
57
57
private const string CreateSessionStateItemSP = @"
58
- function CreateSessionStateItem (sessionId, timeout, lockCookie, sessionItem, uninitialized) {
58
+ function CreateSessionStateItem2 (sessionId, timeout, lockCookie, sessionItem, uninitialized) {
59
59
var collection = getContext().getCollection();
60
60
var collectionLink = collection.getSelfLink();
61
61
var response = getContext().getResponse();
@@ -74,10 +74,22 @@ function CreateSessionStateItem(sessionId, timeout, lockCookie, sessionItem, uni
74
74
ttl: timeout, locked: false, sessionItem: sessionItem, uninitialized: uninitialized };
75
75
collection.createDocument(collectionLink, sessionStateItem,
76
76
function (err, documentCreated) {
77
- if (err) {
77
+ if (err)
78
+ {
79
+ // When creating an uninitialized item, we are doing so just to make sure it gets created.
80
+ // If another request has done the same, it doesn't matter who won the race. Read/Write
81
+ // locked/exclusive access is determined later via a separate GetStateItem call.
82
+ if (uninitialized && err.number == 409) // message: 'Resource with specified id or name already exists.'
83
+ {
84
+ response.setBody({message: 'Document already exists. No need to recreate uninitialized document.', error: err});
85
+ }
86
+
78
87
throw err;
79
88
}
80
- response.setBody({documentCreated: documentCreated});
89
+ else
90
+ {
91
+ response.setBody({documentCreated: documentCreated});
92
+ }
81
93
});
82
94
}" ;
83
95
@@ -208,6 +220,7 @@ function tryGetStateItemExclusive(continuation) {
208
220
doc.lockAge = 0;
209
221
doc.lockCookie += 1;
210
222
doc.locked = true;
223
+ // CosmosDB sprocs are 'atomic' so no need to worry about a race in between the initial query and this update.
211
224
var isAccepted = collection.replaceDocument(doc._self, doc,
212
225
function(err, updatedDocument, responseOptions) {
213
226
if (err)
0 commit comments