@@ -62,18 +62,28 @@ public function save(Key $key)
6262 // prevent concurrency within the same connection
6363 $ this ->getInternalStore ()->save ($ key );
6464
65- $ sql = 'SELECT pg_try_advisory_lock(:key) ' ;
66- $ result = $ this ->conn ->executeQuery ($ sql , [
67- 'key ' => $ this ->getHashedKey ($ key ),
68- ]);
65+ $ lockAcquired = false ;
6966
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+ ] );
7572
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+ }
7787 }
7888
7989 throw new LockConflictedException ();
@@ -84,18 +94,28 @@ public function saveRead(Key $key)
8494 // prevent concurrency within the same connection
8595 $ this ->getInternalStore ()->saveRead ($ key );
8696
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+ ]);
91104
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 );
97110
98- return ;
111+ $ lockAcquired = true ;
112+
113+ return ;
114+ }
115+ } finally {
116+ if (!$ lockAcquired ) {
117+ $ this ->getInternalStore ()->delete ($ key );
118+ }
99119 }
100120
101121 throw new LockConflictedException ();
0 commit comments