SQLite

Check-in [b9b11855e8]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:A different fix to [fc62af4523]. When changing from journal_mode=PERSIST or TRINCATE to some other rollback mode, delete the journal file only if a RESERVED lock can be obtained on the database file first.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | experimental
Files: files | file ages | folders
SHA1: b9b11855e8a9522309dd30e5256bb67d67e1353a
User & Date: dan 2010-06-17 16:44:22.000
References
2010-06-17
17:05
Apply [b9b11855e8] (the alternate fix to [fc62af4523]) to the trunk. (check-in: 9a949a3a5c user: dan tags: trunk)
Context
2010-06-17
16:44
Merge trunk change [7c3a86b9c7]. (check-in: bd7bc4e0e2 user: dan tags: experimental)
16:44
A different fix to [fc62af4523]. When changing from journal_mode=PERSIST or TRINCATE to some other rollback mode, delete the journal file only if a RESERVED lock can be obtained on the database file first. (check-in: b9b11855e8 user: dan tags: experimental)
11:36
Fix bug in journal2.test. (check-in: c1e04f1d4e user: dan tags: experimental)
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/pager.c.
5894
5895
5896
5897
5898
5899
5900










































5901
5902
5903
5904
5905
5906
5907
    */
    if( isOpen(pPager->jfd) && eMode!=PAGER_JOURNALMODE_WAL ){
      sqlite3OsClose(pPager->jfd);
    }

    /* Change the journal mode. */
    pPager->journalMode = (u8)eMode;










































  }

  /* Return the new journal mode */
  return (int)pPager->journalMode;
}

/*







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
    */
    if( isOpen(pPager->jfd) && eMode!=PAGER_JOURNALMODE_WAL ){
      sqlite3OsClose(pPager->jfd);
    }

    /* Change the journal mode. */
    pPager->journalMode = (u8)eMode;

    /* When transistioning from TRUNCATE or PERSIST to any other journal
    ** mode except WAL (and we are not in locking_mode=EXCLUSIVE) then 
    ** delete the journal file.
    */
    assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 );
    assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 );
    assert( (PAGER_JOURNALMODE_DELETE & 5)==0 );
    assert( (PAGER_JOURNALMODE_MEMORY & 5)==4 );
    assert( (PAGER_JOURNALMODE_OFF & 5)==0 );
    assert( (PAGER_JOURNALMODE_WAL & 5)==5 );

    assert( isOpen(pPager->fd) || pPager->exclusiveMode );
    if( !pPager->exclusiveMode && (eOld & 5)==1 && (eMode & 1)==0 ){

      /* In this case we would like to delete the journal file. If it is
      ** not possible, then that is not a problem. Deleting the journal file
      ** here is an optimization only.
      **
      ** Before deleting the journal file, obtain a RESERVED lock on the
      ** database file. This ensures that the journal file is not deleted
      ** while it is in use by some other client.
      */
      int rc = SQLITE_OK;
      int state = pPager->state;
      if( state<PAGER_SHARED ){
        rc = sqlite3PagerSharedLock(pPager);
      }
      if( pPager->state==PAGER_SHARED ){
        assert( rc==SQLITE_OK );
        rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK);
      }
      if( rc==SQLITE_OK ){
        sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
      }
      if( rc==SQLITE_OK && state==PAGER_SHARED ){
        sqlite3OsUnlock(pPager->fd, SHARED_LOCK);
      }else if( state==PAGER_UNLOCK ){
        pager_unlock(pPager);
      }
      assert( state==pPager->state );
    }
  }

  /* Return the new journal mode */
  return (int)pPager->journalMode;
}

/*
Changes to src/vdbe.c.
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276

5277
5278
5279
5280
5281
5282
5283
        */
        rc = sqlite3PagerCloseWal(pPager);
        if( rc==SQLITE_OK ){
          sqlite3PagerSetJournalMode(pPager, eNew);
        }else if( rc==SQLITE_BUSY && pOp->p5==0 ){
          goto abort_due_to_error;
        }
      }else{
        sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_DELETE);
        rc = SQLITE_OK;
      }
  
      /* Open a transaction on the database file. Regardless of the journal
      ** mode, this transaction always uses a rollback journal.
      */
      assert( sqlite3BtreeIsInTrans(pBt)==0 );
      if( rc==SQLITE_OK ){
        rc = sqlite3BtreeSetVersion(pBt, 
                                    (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
        if( rc==SQLITE_BUSY && pOp->p5==0 ) goto abort_due_to_error;
      }
      if( rc==SQLITE_BUSY ){
        eNew = eOld;
        rc = SQLITE_OK;
      }
    }
  }
#endif /* ifndef SQLITE_OMIT_WAL */

  eNew = sqlite3PagerSetJournalMode(pPager, eNew);

  pOut = &aMem[pOp->p2];
  pOut->flags = MEM_Str|MEM_Static|MEM_Term;
  pOut->z = (char *)sqlite3JournalModename(eNew);
  pOut->n = sqlite3Strlen30(pOut->z);
  pOut->enc = SQLITE_UTF8;
  sqlite3VdbeChangeEncoding(pOut, encoding);
  break;







<
<
<







|
<











>







5247
5248
5249
5250
5251
5252
5253



5254
5255
5256
5257
5258
5259
5260
5261

5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
        */
        rc = sqlite3PagerCloseWal(pPager);
        if( rc==SQLITE_OK ){
          sqlite3PagerSetJournalMode(pPager, eNew);
        }else if( rc==SQLITE_BUSY && pOp->p5==0 ){
          goto abort_due_to_error;
        }



      }
  
      /* Open a transaction on the database file. Regardless of the journal
      ** mode, this transaction always uses a rollback journal.
      */
      assert( sqlite3BtreeIsInTrans(pBt)==0 );
      if( rc==SQLITE_OK ){
        rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));

        if( rc==SQLITE_BUSY && pOp->p5==0 ) goto abort_due_to_error;
      }
      if( rc==SQLITE_BUSY ){
        eNew = eOld;
        rc = SQLITE_OK;
      }
    }
  }
#endif /* ifndef SQLITE_OMIT_WAL */

  eNew = sqlite3PagerSetJournalMode(pPager, eNew);

  pOut = &aMem[pOp->p2];
  pOut->flags = MEM_Str|MEM_Static|MEM_Term;
  pOut->z = (char *)sqlite3JournalModename(eNew);
  pOut->n = sqlite3Strlen30(pOut->z);
  pOut->enc = SQLITE_UTF8;
  sqlite3VdbeChangeEncoding(pOut, encoding);
  break;
Changes to test/journal2.test.
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
      INSERT INTO t1 VALUES(3.14159);
    }
    set ::oplog
  } {xOpen test.db-journal}
  do_test journal2-2.3 {
    expr {[file size test.db-journal] > 512}
  } {1}
  do_test journal2-2.3 {
    set ::oplog [list]
    execsql { PRAGMA journal_mode = WAL }
    set ::oplog
  } [list                                              \
      xClose test.db-journal                           \
      xOpen test.db-journal xClose test.db-journal     \
      xOpen test.db-journal xClose test.db-journal     \
      xDelete test.db-journal                          \
  ]
  db close
}

tvfs delete
finish_test








|



<
|
<
<
<
<






211
212
213
214
215
216
217
218
219
220
221

222




223
224
225
226
227
228
      INSERT INTO t1 VALUES(3.14159);
    }
    set ::oplog
  } {xOpen test.db-journal}
  do_test journal2-2.3 {
    expr {[file size test.db-journal] > 512}
  } {1}
  do_test journal2-2.4 {
    set ::oplog [list]
    execsql { PRAGMA journal_mode = WAL }
    set ::oplog

  } {xClose test.db-journal xDelete test.db-journal}




  db close
}

tvfs delete
finish_test

Changes to test/jrnlmode.test.
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
    do_test jrnlmode-6.5 {
      execsql {
        PRAGMA journal_mode = MEMORY;
        BEGIN;
          INSERT INTO t4 VALUES(3, 4);
      }
      file exists test.db-journal
    } {1}
    do_test jrnlmode-6.7 {
      execsql {
        COMMIT;
        SELECT * FROM t4;
      }
    } {1 2 3 4}
    do_test jrnlmode-6.8 {
      file exists test.db-journal
    } {1}
    do_test jrnlmode-6.9 {
      execsql {
        PRAGMA journal_mode = DELETE;
        BEGIN IMMEDIATE; INSERT INTO t4 VALUES(1,2); COMMIT;
      }
      file exists test.db-journal
    } {0}







|








|







480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
    do_test jrnlmode-6.5 {
      execsql {
        PRAGMA journal_mode = MEMORY;
        BEGIN;
          INSERT INTO t4 VALUES(3, 4);
      }
      file exists test.db-journal
    } {0}
    do_test jrnlmode-6.7 {
      execsql {
        COMMIT;
        SELECT * FROM t4;
      }
    } {1 2 3 4}
    do_test jrnlmode-6.8 {
      file exists test.db-journal
    } {0}
    do_test jrnlmode-6.9 {
      execsql {
        PRAGMA journal_mode = DELETE;
        BEGIN IMMEDIATE; INSERT INTO t4 VALUES(1,2); COMMIT;
      }
      file exists test.db-journal
    } {0}