System.Data.SQLite

Check-in [feda13301a]
Login

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

Overview
Comment:Revise handling of CriticalHandle derived classes to make them more thread-safe.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | tkt-996d13cd87
Files: files | file ages | folders
SHA1: feda13301a60a9de81c4d56734cb526713f4f291
User & Date: mistachkin 2012-04-29 03:23:43.723
Context
2012-04-29
03:27
Modify static IsAutocommit method to prevent it from passing IntPtr.Zero to the native library. check-in: 5dd7f49382 user: mistachkin tags: tkt-996d13cd87
03:23
Revise handling of CriticalHandle derived classes to make them more thread-safe. check-in: feda13301a user: mistachkin tags: tkt-996d13cd87
2012-04-28
12:21
Reform how the connection pool treats its connection handles. check-in: 4a6b21aeef user: mistachkin tags: tkt-996d13cd87
Changes
Unified Diff Ignore Whitespace Patch
Changes to System.Data.SQLite/SQLiteBase.cs.
336
337
338
339
340
341
342
343
344

345
346
347
348
349
350
351
352
353
354

355
356
357
358
359
360
361
362
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
    ///////////////////////////////////////////////////////////////////////////////////////////////

    // These statics are here for lack of a better place to put them.
    // They exist here because they are called during the finalization of
    // a SQLiteStatementHandle, SQLiteConnectionHandle, and SQLiteFunctionCookieHandle.
    // Therefore these functions have to be static, and have to be low-level.

    internal static string SQLiteLastError(SQLiteConnectionHandle db)
    {

#if !SQLITE_STANDARD
      int len;
      return UTF8ToString(UnsafeNativeMethods.sqlite3_errmsg_interop(db, out len), len);
#else
      return UTF8ToString(UnsafeNativeMethods.sqlite3_errmsg(db), -1);
#endif
    }

    internal static void FinishBackup(SQLiteBackupHandle backup)
    {

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

    internal static void FinalizeStatement(SQLiteStatementHandle stmt)
    {

      lock (_lock)
      {
#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 db)
    {

      lock (_lock)
      {
#if !SQLITE_STANDARD
        int n = UnsafeNativeMethods.sqlite3_close_interop(db);
#else
      ResetConnection(db);
      int n = UnsafeNativeMethods.sqlite3_close(db);
#endif
        if (n > 0) throw new SQLiteException(n, SQLiteLastError(db));
      }
    }

    internal static void ResetConnection(SQLiteConnectionHandle db)
    {

      lock (_lock)
      {
        IntPtr stmt = IntPtr.Zero;
        int n;
        do
        {
          stmt = UnsafeNativeMethods.sqlite3_next_stmt(db, stmt);







|

>








|

>



<




|

>





|





|

>





|
|





|

>







336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359

360
361
362
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
399
400
401
402
    ///////////////////////////////////////////////////////////////////////////////////////////////

    // These statics are here for lack of a better place to put them.
    // They exist here because they are called during the finalization of
    // a SQLiteStatementHandle, SQLiteConnectionHandle, and SQLiteFunctionCookieHandle.
    // Therefore these functions have to be static, and have to be low-level.

    internal static string SQLiteLastError(IntPtr db)
    {
      if (db == IntPtr.Zero) return "invalid database handle";
#if !SQLITE_STANDARD
      int len;
      return UTF8ToString(UnsafeNativeMethods.sqlite3_errmsg_interop(db, out len), len);
#else
      return UTF8ToString(UnsafeNativeMethods.sqlite3_errmsg(db), -1);
#endif
    }

    internal static void FinishBackup(IntPtr backup)
    {
        if (backup == IntPtr.Zero) return;
        lock (_lock)
        {
            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;
      lock (_lock)
      {
#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(IntPtr db)
    {
      if (db == IntPtr.Zero) return;
      lock (_lock)
      {
#if !SQLITE_STANDARD
        int n = UnsafeNativeMethods.sqlite3_close_interop(db);
#else
        ResetConnection(db);
        int n = UnsafeNativeMethods.sqlite3_close(db);
#endif
        if (n > 0) throw new SQLiteException(n, SQLiteLastError(db));
      }
    }

    internal static void ResetConnection(IntPtr db)
    {
      if (db == IntPtr.Zero) return;
      lock (_lock)
      {
        IntPtr stmt = IntPtr.Zero;
        int n;
        do
        {
          stmt = UnsafeNativeMethods.sqlite3_next_stmt(db, stmt);
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
        {
          n = UnsafeNativeMethods.sqlite3_exec(db, ToUTF8("ROLLBACK"), IntPtr.Zero, IntPtr.Zero, out stmt);
          if (n > 0) throw new SQLiteException(n, SQLiteLastError(db));
        }
      }
    }

    internal static bool IsAutocommit(SQLiteConnectionHandle hdl)
    {
      return (UnsafeNativeMethods.sqlite3_get_autocommit(hdl) == 1);
    }

  }

  internal interface ISQLiteSchemaExtensions
  {
    void BuildTempSchema(SQLiteConnection cnn);







|

|







414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
        {
          n = UnsafeNativeMethods.sqlite3_exec(db, ToUTF8("ROLLBACK"), IntPtr.Zero, IntPtr.Zero, out stmt);
          if (n > 0) throw new SQLiteException(n, SQLiteLastError(db));
        }
      }
    }

    internal static bool IsAutocommit(IntPtr db)
    {
      return (UnsafeNativeMethods.sqlite3_get_autocommit(db) == 1);
    }

  }

  internal interface ISQLiteSchemaExtensions
  {
    void BuildTempSchema(SQLiteConnection cnn);
Changes to System.Data.SQLite/UnsafeNativeMethods.cs.
19
20
21
22
23
24
25




26
27
28
29
30
31
32
#endif

#if !PLATFORM_COMPACTFRAMEWORK && !DEBUG
  using System.Security;
#endif

  using System.Runtime.InteropServices;





#if !PLATFORM_COMPACTFRAMEWORK && !DEBUG
  [SuppressUnmanagedCodeSecurity]
#endif
  internal static class UnsafeNativeMethods
  {
      #region Optional Native SQLite Library Pre-Loading Code







>
>
>
>







19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#endif

#if !PLATFORM_COMPACTFRAMEWORK && !DEBUG
  using System.Security;
#endif

  using System.Runtime.InteropServices;

#if !PLATFORM_COMPACTFRAMEWORK
  using System.Threading;
#endif

#if !PLATFORM_COMPACTFRAMEWORK && !DEBUG
  [SuppressUnmanagedCodeSecurity]
#endif
  internal static class UnsafeNativeMethods
  {
      #region Optional Native SQLite Library Pre-Loading Code
1380
1381
1382
1383
1384
1385
1386





1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399







1400
1401
1402
1403
1404
1405
1406
    {
    }

    protected override bool ReleaseHandle()
    {
      try
      {





        SQLiteBase.CloseConnection(this);
        SetHandle(IntPtr.Zero);

#if DEBUG && !NET_COMPACT_20
        try
        {
          Trace.WriteLine(String.Format(
              "CloseConnection: {0}", handle));
        }
        catch
        {
        }
#endif








#if DEBUG
        return true;
#endif
      }
#if DEBUG && !NET_COMPACT_20
      catch (SQLiteException e)







>
>
>
>
>
|
<





|





>
>
>
>
>
>
>







1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396

1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
    {
    }

    protected override bool ReleaseHandle()
    {
      try
      {
#if !PLATFORM_COMPACTFRAMEWORK
        IntPtr localHandle = Interlocked.Exchange(
          ref handle, IntPtr.Zero);

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


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

#if DEBUG
        return true;
#endif
      }
#if DEBUG && !NET_COMPACT_20
      catch (SQLiteException e)
1416
1417
1418
1419
1420
1421
1422




1423
1424
1425
1426
1427
1428
1429
              handle, e));
        }
        catch
        {
        }
#endif
      }




#if DEBUG
      return false;
#else
      return true;
#endif
    }








>
>
>
>







1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
              handle, e));
        }
        catch
        {
        }
#endif
      }
      finally
      {
        SetHandleAsInvalid();
      }
#if DEBUG
      return false;
#else
      return true;
#endif
    }

1464
1465
1466
1467
1468
1469
1470





1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483







1484
1485
1486
1487
1488
1489
1490
    {
    }

    protected override bool ReleaseHandle()
    {
      try
      {





        SQLiteBase.FinalizeStatement(this);
        SetHandle(IntPtr.Zero);

#if DEBUG && !NET_COMPACT_20
        try
        {
          Trace.WriteLine(String.Format(
              "FinalizeStatement: {0}", handle));
        }
        catch
        {
        }
#endif








#if DEBUG
        return true;
#endif
      }
#if DEBUG && !NET_COMPACT_20
      catch (SQLiteException e)







>
>
>
>
>
|
<





|





>
>
>
>
>
>
>







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
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
    {
    }

    protected override bool ReleaseHandle()
    {
      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
      }
#if DEBUG && !NET_COMPACT_20
      catch (SQLiteException e)
1500
1501
1502
1503
1504
1505
1506




1507
1508
1509
1510
1511
1512
1513
              handle, e));
        }
        catch
        {
        }
#endif
      }




#if DEBUG
      return false;
#else
      return true;
#endif
    }








>
>
>
>







1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
              handle, e));
        }
        catch
        {
        }
#endif
      }
      finally
      {
        SetHandleAsInvalid();
      }
#if DEBUG
      return false;
#else
      return true;
#endif
    }

1548
1549
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
      {
      }

      protected override bool ReleaseHandle()
      {
          try
          {





              SQLiteBase.FinishBackup(this);
              SetHandle(IntPtr.Zero);

#if DEBUG && !NET_COMPACT_20
              try
              {
                  Trace.WriteLine(String.Format(
                      "FinishBackup: {0}", handle));
              }
              catch
              {
              }
#endif








#if DEBUG
              return true;
#endif
          }
#if DEBUG && !NET_COMPACT_20
          catch (SQLiteException e)







>
>
>
>
>
|
<





|





>
>
>
>
>
>
>







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
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
      {
      }

      protected override bool ReleaseHandle()
      {
          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
          }
#if DEBUG && !NET_COMPACT_20
          catch (SQLiteException e)
1584
1585
1586
1587
1588
1589
1590




1591
1592
1593
1594
1595
1596
1597
                      handle, e));
              }
              catch
              {
              }
#endif
          }




#if DEBUG
          return false;
#else
          return true;
#endif
      }








>
>
>
>







1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
                      handle, e));
              }
              catch
              {
              }
#endif
          }
          finally
          {
              SetHandleAsInvalid();
          }
#if DEBUG
          return false;
#else
          return true;
#endif
      }