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