@@ -156,20 +156,44 @@ void DsqlStatementCache::putStatement(thread_db* tdbb, const string& text, USHOR
156156#endif
157157}
158158
159+ void DsqlStatementCache::removeStatement (thread_db* tdbb, DsqlStatement* statement)
160+ {
161+ if (const auto cacheKey = statement->getCacheKey ())
162+ {
163+ if (const auto entryPtr = map.get (cacheKey))
164+ {
165+ const auto entry = *entryPtr;
166+
167+ entry->dsqlStatement ->resetCacheKey ();
168+
169+ if (entry->active )
170+ {
171+ entry->dsqlStatement ->addRef ();
172+ activeStatementList.erase (entry);
173+ }
174+ else
175+ {
176+ inactiveStatementList.erase (entry);
177+ cacheSize -= entry->size ;
178+ }
179+
180+ map.remove (entry->key );
181+ }
182+ }
183+ }
184+
159185void DsqlStatementCache::statementGoingInactive (Firebird::RefStrPtr& key)
160186{
161187 const auto entryPtr = map.get (key);
162188
163189 if (!entryPtr)
164- {
165- fb_assert (false );
166190 return ;
167- }
168191
169192 const auto entry = *entryPtr;
170193
171194 fb_assert (entry->active );
172195 entry->active = false ;
196+ entry->dsqlStatement ->addRef ();
173197 entry->size = entry->dsqlStatement ->getSize (); // update size
174198
175199 inactiveStatementList.splice (inactiveStatementList.end (), activeStatementList, entry);
@@ -192,6 +216,9 @@ void DsqlStatementCache::purge(thread_db* tdbb, bool releaseLock)
192216 entry.dsqlStatement ->resetCacheKey ();
193217 }
194218
219+ for (auto & entry : inactiveStatementList)
220+ entry.dsqlStatement ->resetCacheKey ();
221+
195222 map.clear ();
196223 activeStatementList.clear ();
197224 inactiveStatementList.clear ();
@@ -273,6 +300,7 @@ void DsqlStatementCache::shrink()
273300 while (cacheSize > maxCacheSize && !inactiveStatementList.isEmpty ())
274301 {
275302 const auto & front = inactiveStatementList.front ();
303+ front.dsqlStatement ->resetCacheKey ();
276304 map.remove (front.key );
277305 cacheSize -= front.size ;
278306 inactiveStatementList.erase (inactiveStatementList.begin ());
0 commit comments