@@ -62,18 +62,28 @@ public function save(Key $key)
62
62
// prevent concurrency within the same connection
63
63
$ this ->getInternalStore ()->save ($ key );
64
64
65
- $ sql = 'SELECT pg_try_advisory_lock(:key) ' ;
66
- $ result = $ this ->conn ->executeQuery ($ sql , [
67
- 'key ' => $ this ->getHashedKey ($ key ),
68
- ]);
65
+ $ lockAcquired = false ;
69
66
70
- // Check if lock is acquired
71
- if ( true === $ result -> fetchOne ()) {
72
- $ key -> markUnserializable ();
73
- // release sharedLock in case of promotion
74
- $ this -> unlockShared ( $ key );
67
+ try {
68
+ $ sql = ' SELECT pg_try_advisory_lock(:key) ' ;
69
+ $ result = $ this -> conn -> executeQuery ( $ sql , [
70
+ ' key ' => $ this -> getHashedKey ( $ key ),
71
+ ] );
75
72
76
- return ;
73
+ // Check if lock is acquired
74
+ if (true === $ result ->fetchOne ()) {
75
+ $ key ->markUnserializable ();
76
+ // release sharedLock in case of promotion
77
+ $ this ->unlockShared ($ key );
78
+
79
+ $ lockAcquired = true ;
80
+
81
+ return ;
82
+ }
83
+ } finally {
84
+ if (!$ lockAcquired ) {
85
+ $ this ->getInternalStore ()->delete ($ key );
86
+ }
77
87
}
78
88
79
89
throw new LockConflictedException ();
@@ -84,18 +94,28 @@ public function saveRead(Key $key)
84
94
// prevent concurrency within the same connection
85
95
$ this ->getInternalStore ()->saveRead ($ key );
86
96
87
- $ sql = 'SELECT pg_try_advisory_lock_shared(:key) ' ;
88
- $ result = $ this ->conn ->executeQuery ($ sql , [
89
- 'key ' => $ this ->getHashedKey ($ key ),
90
- ]);
97
+ $ lockAcquired = false ;
98
+
99
+ try {
100
+ $ sql = 'SELECT pg_try_advisory_lock_shared(:key) ' ;
101
+ $ result = $ this ->conn ->executeQuery ($ sql , [
102
+ 'key ' => $ this ->getHashedKey ($ key ),
103
+ ]);
91
104
92
- // Check if lock is acquired
93
- if (true === $ result ->fetchOne ()) {
94
- $ key ->markUnserializable ();
95
- // release lock in case of demotion
96
- $ this ->unlock ($ key );
105
+ // Check if lock is acquired
106
+ if (true === $ result ->fetchOne ()) {
107
+ $ key ->markUnserializable ();
108
+ // release lock in case of demotion
109
+ $ this ->unlock ($ key );
97
110
98
- return ;
111
+ $ lockAcquired = true ;
112
+
113
+ return ;
114
+ }
115
+ } finally {
116
+ if (!$ lockAcquired ) {
117
+ $ this ->getInternalStore ()->delete ($ key );
118
+ }
99
119
}
100
120
101
121
throw new LockConflictedException ();
0 commit comments