@@ -46,7 +46,6 @@ QgsPostgresFeatureIterator::QgsPostgresFeatureIterator( QgsPostgresFeatureSource
46
46
else
47
47
{
48
48
mConn = source->mTransactionConnection ;
49
- mConn ->lock ();
50
49
mIsTransactionConnection = true ;
51
50
}
52
51
@@ -227,6 +226,8 @@ bool QgsPostgresFeatureIterator::fetchFeature( QgsFeature& feature )
227
226
{
228
227
QString fetch = QString ( " FETCH FORWARD %1 FROM %2" ).arg ( mFeatureQueueSize ).arg ( mCursorName );
229
228
QgsDebugMsgLevel ( QString ( " fetching %1 features." ).arg ( mFeatureQueueSize ), 4 );
229
+
230
+ lock ();
230
231
if ( mConn ->PQsendQuery ( fetch ) == 0 ) // fetch features asynchronously
231
232
{
232
233
QgsMessageLog::logMessage ( QObject::tr ( " Fetching from cursor %1 failed\n Database error: %2" ).arg ( mCursorName , mConn ->PQerrorMessage () ), QObject::tr ( " PostGIS" ) );
@@ -257,6 +258,7 @@ bool QgsPostgresFeatureIterator::fetchFeature( QgsFeature& feature )
257
258
getFeature ( queryResult, row, mFeatureQueue .back () );
258
259
} // for each row in queue
259
260
}
261
+ unlock ();
260
262
}
261
263
262
264
if ( mFeatureQueue .empty () )
@@ -319,13 +321,28 @@ bool QgsPostgresFeatureIterator::prepareOrderBy( const QList<QgsFeatureRequest::
319
321
return mOrderByCompiled ;
320
322
}
321
323
324
+ void QgsPostgresFeatureIterator::lock ()
325
+ {
326
+ if ( mIsTransactionConnection )
327
+ mConn ->lock ();
328
+ }
329
+
330
+ void QgsPostgresFeatureIterator::unlock ()
331
+ {
332
+ if ( mIsTransactionConnection )
333
+ mConn ->unlock ();
334
+ }
335
+
322
336
bool QgsPostgresFeatureIterator::rewind ()
323
337
{
324
338
if ( mClosed )
325
339
return false ;
326
340
327
341
// move cursor to first record
342
+
343
+ lock ();
328
344
mConn ->PQexecNR ( QString ( " move absolute 0 in %1" ).arg ( mCursorName ) );
345
+ unlock ();
329
346
mFeatureQueue .clear ();
330
347
mFetched = 0 ;
331
348
mLastFetch = false ;
@@ -338,16 +355,14 @@ bool QgsPostgresFeatureIterator::close()
338
355
if ( !mConn )
339
356
return false ;
340
357
358
+ lock ();
341
359
mConn ->closeCursor ( mCursorName );
360
+ unlock ();
342
361
343
362
if ( !mIsTransactionConnection )
344
363
{
345
364
QgsPostgresConnPool::instance ()->releaseConnection ( mConn );
346
365
}
347
- else
348
- {
349
- mConn ->unlock ();
350
- }
351
366
mConn = nullptr ;
352
367
353
368
while ( !mFeatureQueue .empty () )
@@ -598,15 +613,17 @@ bool QgsPostgresFeatureIterator::declareCursor( const QString& whereClause, long
598
613
if ( !orderBy.isEmpty () )
599
614
query += QString ( " ORDER BY %1 " ).arg ( orderBy );
600
615
616
+ lock ();
601
617
if ( !mConn ->openCursor ( mCursorName , query ) )
602
618
{
603
-
619
+ unlock ();
604
620
// reloading the fields might help next time around
605
621
// TODO how to cleanly force reload of fields? P->loadFields();
606
622
if ( closeOnFail )
607
623
close ();
608
624
return false ;
609
625
}
626
+ unlock ();
610
627
611
628
mLastFetch = false ;
612
629
return true ;
0 commit comments