System.Data.SQLite
Check-in [fbf498a216]
Not logged in

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

Overview
Comment:Obtain a lock on the connection handle prior to finalizing a statement and/or finishing a backup object.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: fbf498a216efbfd73c2ab8236455c06b0e94a634
User & Date: mistachkin 2012-05-03 17:15:41
Context
2012-05-03
17:46
Update test case for ticket [996d13cd87] to run with and without connection pooling enabled. check-in: ead1f27df0 user: mistachkin tags: trunk
17:15
Obtain a lock on the connection handle prior to finalizing a statement and/or finishing a backup object. check-in: fbf498a216 user: mistachkin tags: trunk
16:44
Expand scope for the GC.KeepAlive call in the SQLiteConnectionPool.Add method. check-in: d13b6a5885 user: mistachkin tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to System.Data.SQLite/SQLite3.cs.

534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
....
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
          }
        }

        if (n > 0) throw new SQLiteException(n, GetLastError());

        strRemain = UTF8ToString(ptr, len);

        if (stmt != IntPtr.Zero) cmd = new SQLiteStatement(this, flags, new SQLiteStatementHandle(stmt), strSql.Substring(0, strSql.Length - strRemain.Length), previous);

        return cmd;
      }
      finally
      {
        handle.Free();
      }
................................................................................
        IntPtr backup = UnsafeNativeMethods.sqlite3_backup_init(
            destHandle, zDestName, sourceHandle, zSourceName);

        if (backup == IntPtr.Zero)
            throw new SQLiteException(ResultCode(), GetLastError());

        return new SQLiteBackup(
            this, new SQLiteBackupHandle(backup), destHandle, zDestName,
            sourceHandle, zSourceName);
    }

    /// <summary>
    /// Copies up to N pages from the source database to the destination
    /// database associated with the specified backup object.
    /// </summary>
    /// <param name="backup">The backup object to use.</param>







|







 







|
|







534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
....
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
          }
        }

        if (n > 0) throw new SQLiteException(n, GetLastError());

        strRemain = UTF8ToString(ptr, len);

        if (stmt != IntPtr.Zero) cmd = new SQLiteStatement(this, flags, new SQLiteStatementHandle(_sql, stmt), strSql.Substring(0, strSql.Length - strRemain.Length), previous);

        return cmd;
      }
      finally
      {
        handle.Free();
      }
................................................................................
        IntPtr backup = UnsafeNativeMethods.sqlite3_backup_init(
            destHandle, zDestName, sourceHandle, zSourceName);

        if (backup == IntPtr.Zero)
            throw new SQLiteException(ResultCode(), GetLastError());

        return new SQLiteBackup(
            this, new SQLiteBackupHandle(destHandle, backup),
            destHandle, zDestName, sourceHandle, zSourceName);
    }

    /// <summary>
    /// Copies up to N pages from the source database to the destination
    /// database associated with the specified backup object.
    /// </summary>
    /// <param name="backup">The backup object to use.</param>

Changes to System.Data.SQLite/SQLiteBase.cs.

363
364
365
366
367
368
369
370
371
372


373
374
375
376

377
378
379


380
381
382
383
384
385

386
387
388
389
390
391
392
        }

#pragma warning disable 162
        GC.KeepAlive(hdl); /* NOTE: Unreachable code. */
#pragma warning restore 162
    }

    internal static void FinishBackup(IntPtr backup)
    {
        if (backup == IntPtr.Zero) return;


        int n = UnsafeNativeMethods.sqlite3_backup_finish(backup);
        if (n > 0) throw new SQLiteException(n, null);
    }


    internal static void FinalizeStatement(IntPtr stmt)
    {
        if (stmt == IntPtr.Zero) return;


#if !SQLITE_STANDARD
        int n = UnsafeNativeMethods.sqlite3_finalize_interop(stmt);
#else
        int n = UnsafeNativeMethods.sqlite3_finalize(stmt);
#endif
        if (n > 0) throw new SQLiteException(n, null);

    }

    internal static void CloseConnection(SQLiteConnectionHandle hdl, IntPtr db)
    {
        if ((hdl == null) || (db == IntPtr.Zero)) return;
        lock (hdl)
        {







|

|
>
>
|
|
|
|
>
|

|
>
>

|

|

|
>







363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
        }

#pragma warning disable 162
        GC.KeepAlive(hdl); /* NOTE: Unreachable code. */
#pragma warning restore 162
    }

    internal static void FinishBackup(SQLiteConnectionHandle hdl, IntPtr backup)
    {
        if ((hdl == null) || (backup == IntPtr.Zero)) return;
        lock (hdl)
        {
            int n = UnsafeNativeMethods.sqlite3_backup_finish(backup);
            if (n > 0) throw new SQLiteException(n, null);
        }
    }

    internal static void FinalizeStatement(SQLiteConnectionHandle hdl, IntPtr stmt)
    {
        if ((hdl == null) || (stmt == IntPtr.Zero)) return;
        lock (hdl)
        {
#if !SQLITE_STANDARD
            int n = UnsafeNativeMethods.sqlite3_finalize_interop(stmt);
#else
            int n = UnsafeNativeMethods.sqlite3_finalize(stmt);
#endif
            if (n > 0) throw new SQLiteException(n, null);
        }
    }

    internal static void CloseConnection(SQLiteConnectionHandle hdl, IntPtr db)
    {
        if ((hdl == null) || (db == IntPtr.Zero)) return;
        lock (hdl)
        {

Changes to System.Data.SQLite/UnsafeNativeMethods.cs.

1453
1454
1455
1456
1457
1458
1459


1460
1461
1462
1463
1464
1465
1466
1467

1468
1469
1470
1471
1472
1473
1474
....
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
....
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
....
1547
1548
1549
1550
1551
1552
1553


1554
1555
1556
1557
1558
1559
1560
1561

1562
1563
1564
1565
1566
1567
1568
....
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
....
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
    }
#endif
  }

  // Provides finalization support for unmanaged SQLite statements.
  internal class SQLiteStatementHandle : CriticalHandle
  {


    public static implicit operator IntPtr(SQLiteStatementHandle stmt)
    {
      return (stmt != null) ? stmt.handle : IntPtr.Zero;
    }

    internal SQLiteStatementHandle(IntPtr stmt)
      : this()
    {

      SetHandle(stmt);
    }

    private SQLiteStatementHandle()
      : base(IntPtr.Zero)
    {
    }
................................................................................
      try
      {
#if !PLATFORM_COMPACTFRAMEWORK
        IntPtr localHandle = Interlocked.Exchange(
          ref handle, IntPtr.Zero);

        if (localHandle != IntPtr.Zero)
          SQLiteBase.FinalizeStatement(localHandle);

#if DEBUG && !NET_COMPACT_20
        try
        {
          Trace.WriteLine(String.Format(
              "FinalizeStatement: {0}", localHandle));
        }
................................................................................
        catch
        {
        }
#endif
#else
        if (handle != IntPtr.Zero)
        {
          SQLiteBase.FinalizeStatement(handle);
          SetHandle(IntPtr.Zero);
        }
#endif

#if DEBUG
        return true;
#endif
................................................................................
    }
#endif
  }

  // Provides finalization support for unmanaged SQLite backup objects.
  internal class SQLiteBackupHandle : CriticalHandle
  {


      public static implicit operator IntPtr(SQLiteBackupHandle backup)
      {
          return (backup != null) ? backup.handle : IntPtr.Zero;
      }

      internal SQLiteBackupHandle(IntPtr backup)
          : this()
      {

          SetHandle(backup);
      }

      private SQLiteBackupHandle()
          : base(IntPtr.Zero)
      {
      }
................................................................................
          try
          {
#if !PLATFORM_COMPACTFRAMEWORK
              IntPtr localHandle = Interlocked.Exchange(
                  ref handle, IntPtr.Zero);

              if (localHandle != IntPtr.Zero)
                  SQLiteBase.FinishBackup(localHandle);

#if DEBUG && !NET_COMPACT_20
              try
              {
                  Trace.WriteLine(String.Format(
                      "FinishBackup: {0}", localHandle));
              }
................................................................................
              catch
              {
              }
#endif
#else
              if (handle != IntPtr.Zero)
              {
                SQLiteBase.FinishBackup(handle);
                SetHandle(IntPtr.Zero);
              }
#endif

#if DEBUG
              return true;
#endif







>
>





|


>







 







|







 







|







 







>
>





|


>







 







|







 







|







1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
....
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
....
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
....
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
....
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
....
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
    }
#endif
  }

  // Provides finalization support for unmanaged SQLite statements.
  internal class SQLiteStatementHandle : CriticalHandle
  {
    private SQLiteConnectionHandle cnn;

    public static implicit operator IntPtr(SQLiteStatementHandle stmt)
    {
      return (stmt != null) ? stmt.handle : IntPtr.Zero;
    }

    internal SQLiteStatementHandle(SQLiteConnectionHandle cnn, IntPtr stmt)
      : this()
    {
      this.cnn = cnn;
      SetHandle(stmt);
    }

    private SQLiteStatementHandle()
      : base(IntPtr.Zero)
    {
    }
................................................................................
      try
      {
#if !PLATFORM_COMPACTFRAMEWORK
        IntPtr localHandle = Interlocked.Exchange(
          ref handle, IntPtr.Zero);

        if (localHandle != IntPtr.Zero)
          SQLiteBase.FinalizeStatement(cnn, localHandle);

#if DEBUG && !NET_COMPACT_20
        try
        {
          Trace.WriteLine(String.Format(
              "FinalizeStatement: {0}", localHandle));
        }
................................................................................
        catch
        {
        }
#endif
#else
        if (handle != IntPtr.Zero)
        {
          SQLiteBase.FinalizeStatement(cnn, handle);
          SetHandle(IntPtr.Zero);
        }
#endif

#if DEBUG
        return true;
#endif
................................................................................
    }
#endif
  }

  // Provides finalization support for unmanaged SQLite backup objects.
  internal class SQLiteBackupHandle : CriticalHandle
  {
      private SQLiteConnectionHandle cnn;

      public static implicit operator IntPtr(SQLiteBackupHandle backup)
      {
          return (backup != null) ? backup.handle : IntPtr.Zero;
      }

      internal SQLiteBackupHandle(SQLiteConnectionHandle cnn, IntPtr backup)
          : this()
      {
          this.cnn = cnn;
          SetHandle(backup);
      }

      private SQLiteBackupHandle()
          : base(IntPtr.Zero)
      {
      }
................................................................................
          try
          {
#if !PLATFORM_COMPACTFRAMEWORK
              IntPtr localHandle = Interlocked.Exchange(
                  ref handle, IntPtr.Zero);

              if (localHandle != IntPtr.Zero)
                  SQLiteBase.FinishBackup(cnn, localHandle);

#if DEBUG && !NET_COMPACT_20
              try
              {
                  Trace.WriteLine(String.Format(
                      "FinishBackup: {0}", localHandle));
              }
................................................................................
              catch
              {
              }
#endif
#else
              if (handle != IntPtr.Zero)
              {
                SQLiteBase.FinishBackup(cnn, handle);
                SetHandle(IntPtr.Zero);
              }
#endif

#if DEBUG
              return true;
#endif