System.Data.SQLite
Check-in [79365bcaed]
Not logged in

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

Overview
Comment:Add support for mapping transaction isolation levels to their legacy default values. Pursuant to [56b42d99c1].
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | isolationLevels
Files: files | file ages | folders
SHA1:79365bcaed517b41e5eccc8af3e1f0981a8afd5a
User & Date: mistachkin 2014-03-30 01:19:01
Original User & Date: denika 2014-03-30 01:19:01
Context
2014-03-30
01:42
Update version history docs. Closed-Leaf check-in: a9fb57516e user: mistachkin tags: isolationLevels
01:19
Add support for mapping transaction isolation levels to their legacy default values. Pursuant to [56b42d99c1]. check-in: 79365bcaed user: mistachkin tags: isolationLevels
2014-03-29
20:52
Make test 'data-1.37' more resilient to thread timing issues. check-in: a242748e03 user: mistachkin tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

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

1037
1038
1039
1040
1041
1042
1043















1044
1045
1046
1047
1048
1049
1050
      /// <summary>
      /// When the <see cref="SQLiteDataReader.HasRows" /> property is used, it
      /// should return non-zero if there were ever any rows in the associated
      /// result sets.
      /// </summary>
      StickyHasRows = 0x400000,
















      /// <summary>
      /// When binding parameter values or returning column values, always
      /// treat them as though they were plain text (i.e. no numeric,
      /// date/time, or other conversions should be attempted).
      /// </summary>
      BindAndGetAllAsText = BindAllAsText | GetAllAsText,








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







1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
      /// <summary>
      /// When the <see cref="SQLiteDataReader.HasRows" /> property is used, it
      /// should return non-zero if there were ever any rows in the associated
      /// result sets.
      /// </summary>
      StickyHasRows = 0x400000,

      /// <summary>
      /// Enable "strict" transaction enlistment semantics.  Setting this flag
      /// will cause an exception to be thrown if an attempt is made to enlist
      /// in a transaction with an unavailable or unsupported isolation level.
      /// In the future, more extensive checks may be enabled by this flag as
      /// well.
      /// </summary>
      StrictEnlistment = 0x800000,

      /// <summary>
      /// Enable mapping of unsupported transaction isolation levels to the
      /// closest supported transaction isolation level.
      /// </summary>
      MapIsolationLevels = 0x1000000,

      /// <summary>
      /// When binding parameter values or returning column values, always
      /// treat them as though they were plain text (i.e. no numeric,
      /// date/time, or other conversions should be attempted).
      /// </summary>
      BindAndGetAllAsText = BindAllAsText | GetAllAsText,

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

334
335
336
337
338
339
340



341
342
343
344
345
346
347
....
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
....
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
....
1326
1327
1328
1329
1330
1331
1332

1333
1334

1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
....
1859
1860
1861
1862
1863
1864
1865



1866


1867
1868
1869
1870
1871
1872
1873
....
2178
2179
2180
2181
2182
2183
2184







































2185
2186
2187
2188
2189
2190
2191
....
2280
2281
2282
2283
2284
2285
2286

2287
2288

2289
2290
2291
2292
2293
2294
2295
    /// because those names are reserved for use by SQLite (i.e. they cannot
    /// be confused with the names of user objects).
    /// </summary>
    internal const string DefaultBaseSchemaName = "sqlite_default_schema";

    private const string MemoryFileName = ":memory:";




    private const SQLiteConnectionFlags DefaultFlags = SQLiteConnectionFlags.Default;
    private const SQLiteSynchronousEnum DefaultSynchronous = SQLiteSynchronousEnum.Default;
    private const SQLiteJournalModeEnum DefaultJournalMode = SQLiteJournalModeEnum.Default;
    private const IsolationLevel DefaultIsolationLevel = IsolationLevel.Serializable;
    private const SQLiteDateFormats DefaultDateTimeFormat = SQLiteDateFormats.ISO8601;
    private const DateTimeKind DefaultDateTimeKind = DateTimeKind.Unspecified;
    private const string DefaultDateTimeFormatString = null;
................................................................................
    /// Determines and returns the fallback default isolation level when one cannot be
    /// obtained from an existing connection instance.
    /// </summary>
    /// <returns>
    /// The fallback default isolation level for this connection instance -OR-
    /// <see cref="IsolationLevel.Unspecified" /> if it cannot be determined.
    /// </returns>
    internal static IsolationLevel GetFallbackDefaultIsolationLevel()
    {
        return DefaultIsolationLevel;
    }

    /// <summary>
    /// Determines and returns the default isolation level for this connection instance.
    /// </summary>
................................................................................
    /// When FALSE, a writelock is obtained immediately.  The default is TRUE, but in a multi-threaded multi-writer
    /// environment, one may instead choose to lock the database immediately to avoid any possible writer deadlock.</param>
    /// <returns>Returns a SQLiteTransaction object.</returns>
    [Obsolete("Use one of the standard BeginTransaction methods, this one will be removed soon")]
    public SQLiteTransaction BeginTransaction(IsolationLevel isolationLevel, bool deferredLock)
    {
      CheckDisposed();
      return (SQLiteTransaction)BeginDbTransaction(deferredLock == false ? IsolationLevel.Serializable : IsolationLevel.ReadCommitted);
    }

    /// <summary>
    /// OBSOLETE.  Creates a new SQLiteTransaction if one isn't already active on the connection.
    /// </summary>
    /// <param name="deferredLock">When TRUE, SQLite defers obtaining a write lock until a write operation is requested.
    /// When FALSE, a writelock is obtained immediately.  The default is false, but in a multi-threaded multi-writer
    /// environment, one may instead choose to lock the database immediately to avoid any possible writer deadlock.</param>
    /// <returns>Returns a SQLiteTransaction object.</returns>
    [Obsolete("Use one of the standard BeginTransaction methods, this one will be removed soon")]
    public SQLiteTransaction BeginTransaction(bool deferredLock)
    {
      CheckDisposed();
      return (SQLiteTransaction)BeginDbTransaction(deferredLock == false ? IsolationLevel.Serializable : IsolationLevel.ReadCommitted);
    }

    /// <summary>
    /// Creates a new <see cref="SQLiteTransaction" /> if one isn't already active on the connection.
    /// </summary>
    /// <param name="isolationLevel">Supported isolation levels are Serializable, ReadCommitted and Unspecified.</param>
    /// <remarks>
................................................................................
    /// <returns></returns>
    protected override DbTransaction BeginDbTransaction(IsolationLevel isolationLevel)
    {
      if (_connectionState != ConnectionState.Open)
        throw new InvalidOperationException();

      if (isolationLevel == IsolationLevel.Unspecified) isolationLevel = _defaultIsolation;


      if (isolationLevel != IsolationLevel.Serializable && isolationLevel != IsolationLevel.ReadCommitted)

        throw new ArgumentException("isolationLevel");

      SQLiteTransaction transaction =
          new SQLiteTransaction(this, isolationLevel != IsolationLevel.Serializable);

      OnChanged(this, new ConnectionEventArgs(
          SQLiteConnectionEventType.NewTransaction, null, transaction,
          null, null, null, null, null));

      return transaction;
    }
................................................................................
        throw new ArgumentException("Already enlisted in a transaction");

      if (_transactionLevel > 0 && transaction != null)
        throw new ArgumentException("Unable to enlist in transaction, a local transaction already exists");
      else if (transaction == null)
        throw new ArgumentNullException("Unable to enlist in transaction, it is null");




      _enlistment = new SQLiteEnlistment(this, transaction);



      OnChanged(this, new ConnectionEventArgs(
          SQLiteConnectionEventType.EnlistTransaction, null, null, null, null,
          null, null, new object[] { _enlistment }));
    }
#endif

................................................................................

            if ((_flags & SQLiteConnectionFlags.NoConnectionPool) == SQLiteConnectionFlags.NoConnectionPool)
                result = false;
        }

        return result;
    }








































    /// <summary>
    /// Opens the connection using the parameters found in the <see cref="ConnectionString" />.
    /// </summary>
    public override void Open()
    {
      CheckDisposed();
................................................................................
        bool usePooling = SQLiteConvert.ToBoolean(FindKey(opts, "Pooling", GetDefaultPooling().ToString()));
        int maxPoolSize = Convert.ToInt32(FindKey(opts, "Max Pool Size", DefaultMaxPoolSize.ToString()), CultureInfo.InvariantCulture);

        _defaultTimeout = Convert.ToInt32(FindKey(opts, "Default Timeout", DefaultConnectionTimeout.ToString()), CultureInfo.InvariantCulture);

        enumValue = TryParseEnum(typeof(IsolationLevel), FindKey(opts, "Default IsolationLevel", DefaultIsolationLevel.ToString()), true);
        _defaultIsolation = (enumValue is IsolationLevel) ? (IsolationLevel)enumValue : DefaultIsolationLevel;


        if (_defaultIsolation != IsolationLevel.Serializable && _defaultIsolation != IsolationLevel.ReadCommitted)

          throw new NotSupportedException("Invalid Default IsolationLevel specified");

        _baseSchemaName = FindKey(opts, "BaseSchemaName", DefaultBaseSchemaName);

        if (_sql == null)
        {
            SetupSQLiteBase(opts);







>
>
>







 







|







 







|













|







 







>

<
>



|







 







>
>
>
|
>
>







 







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







 







>

<
>







334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
....
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
....
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
....
1329
1330
1331
1332
1333
1334
1335
1336
1337

1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
....
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
....
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
....
2328
2329
2330
2331
2332
2333
2334
2335
2336

2337
2338
2339
2340
2341
2342
2343
2344
    /// because those names are reserved for use by SQLite (i.e. they cannot
    /// be confused with the names of user objects).
    /// </summary>
    internal const string DefaultBaseSchemaName = "sqlite_default_schema";

    private const string MemoryFileName = ":memory:";

    internal const IsolationLevel DeferredIsolationLevel = IsolationLevel.ReadCommitted;
    internal const IsolationLevel ImmediateIsolationLevel = IsolationLevel.Serializable;

    private const SQLiteConnectionFlags DefaultFlags = SQLiteConnectionFlags.Default;
    private const SQLiteSynchronousEnum DefaultSynchronous = SQLiteSynchronousEnum.Default;
    private const SQLiteJournalModeEnum DefaultJournalMode = SQLiteJournalModeEnum.Default;
    private const IsolationLevel DefaultIsolationLevel = IsolationLevel.Serializable;
    private const SQLiteDateFormats DefaultDateTimeFormat = SQLiteDateFormats.ISO8601;
    private const DateTimeKind DefaultDateTimeKind = DateTimeKind.Unspecified;
    private const string DefaultDateTimeFormatString = null;
................................................................................
    /// Determines and returns the fallback default isolation level when one cannot be
    /// obtained from an existing connection instance.
    /// </summary>
    /// <returns>
    /// The fallback default isolation level for this connection instance -OR-
    /// <see cref="IsolationLevel.Unspecified" /> if it cannot be determined.
    /// </returns>
    private static IsolationLevel GetFallbackDefaultIsolationLevel()
    {
        return DefaultIsolationLevel;
    }

    /// <summary>
    /// Determines and returns the default isolation level for this connection instance.
    /// </summary>
................................................................................
    /// When FALSE, a writelock is obtained immediately.  The default is TRUE, but in a multi-threaded multi-writer
    /// environment, one may instead choose to lock the database immediately to avoid any possible writer deadlock.</param>
    /// <returns>Returns a SQLiteTransaction object.</returns>
    [Obsolete("Use one of the standard BeginTransaction methods, this one will be removed soon")]
    public SQLiteTransaction BeginTransaction(IsolationLevel isolationLevel, bool deferredLock)
    {
      CheckDisposed();
      return (SQLiteTransaction)BeginDbTransaction(deferredLock == false ? ImmediateIsolationLevel : DeferredIsolationLevel);
    }

    /// <summary>
    /// OBSOLETE.  Creates a new SQLiteTransaction if one isn't already active on the connection.
    /// </summary>
    /// <param name="deferredLock">When TRUE, SQLite defers obtaining a write lock until a write operation is requested.
    /// When FALSE, a writelock is obtained immediately.  The default is false, but in a multi-threaded multi-writer
    /// environment, one may instead choose to lock the database immediately to avoid any possible writer deadlock.</param>
    /// <returns>Returns a SQLiteTransaction object.</returns>
    [Obsolete("Use one of the standard BeginTransaction methods, this one will be removed soon")]
    public SQLiteTransaction BeginTransaction(bool deferredLock)
    {
      CheckDisposed();
      return (SQLiteTransaction)BeginDbTransaction(deferredLock == false ? ImmediateIsolationLevel : DeferredIsolationLevel);
    }

    /// <summary>
    /// Creates a new <see cref="SQLiteTransaction" /> if one isn't already active on the connection.
    /// </summary>
    /// <param name="isolationLevel">Supported isolation levels are Serializable, ReadCommitted and Unspecified.</param>
    /// <remarks>
................................................................................
    /// <returns></returns>
    protected override DbTransaction BeginDbTransaction(IsolationLevel isolationLevel)
    {
      if (_connectionState != ConnectionState.Open)
        throw new InvalidOperationException();

      if (isolationLevel == IsolationLevel.Unspecified) isolationLevel = _defaultIsolation;
      isolationLevel = GetEffectiveIsolationLevel(isolationLevel);


      if (isolationLevel != ImmediateIsolationLevel && isolationLevel != DeferredIsolationLevel)
        throw new ArgumentException("isolationLevel");

      SQLiteTransaction transaction =
          new SQLiteTransaction(this, isolationLevel != ImmediateIsolationLevel);

      OnChanged(this, new ConnectionEventArgs(
          SQLiteConnectionEventType.NewTransaction, null, transaction,
          null, null, null, null, null));

      return transaction;
    }
................................................................................
        throw new ArgumentException("Already enlisted in a transaction");

      if (_transactionLevel > 0 && transaction != null)
        throw new ArgumentException("Unable to enlist in transaction, a local transaction already exists");
      else if (transaction == null)
        throw new ArgumentNullException("Unable to enlist in transaction, it is null");

      bool strictEnlistment = ((_flags & SQLiteConnectionFlags.StrictEnlistment) ==
          SQLiteConnectionFlags.StrictEnlistment);

      _enlistment = new SQLiteEnlistment(this, transaction,
          GetFallbackDefaultIsolationLevel(), strictEnlistment,
          strictEnlistment);

      OnChanged(this, new ConnectionEventArgs(
          SQLiteConnectionEventType.EnlistTransaction, null, null, null, null,
          null, null, new object[] { _enlistment }));
    }
#endif

................................................................................

            if ((_flags & SQLiteConnectionFlags.NoConnectionPool) == SQLiteConnectionFlags.NoConnectionPool)
                result = false;
        }

        return result;
    }

    /// <summary>
    /// Determines the transaction isolation level that should be used by
    /// the caller, primarily based upon the one specified by the caller.
    /// If mapping of transaction isolation levels is enabled, the returned
    /// transaction isolation level may be significantly different than the
    /// originally specified one.
    /// </summary>
    /// <param name="isolationLevel">
    /// The originally specified transaction isolation level.
    /// </param>
    /// <returns>
    /// The transaction isolation level that should be used.
    /// </returns>
    private IsolationLevel GetEffectiveIsolationLevel(
        IsolationLevel isolationLevel
        )
    {
        if ((_flags & SQLiteConnectionFlags.MapIsolationLevels)
                != SQLiteConnectionFlags.MapIsolationLevels)
        {
            return isolationLevel;
        }

        switch (isolationLevel)
        {
            case IsolationLevel.Unspecified:
            case IsolationLevel.Chaos:
            case IsolationLevel.ReadUncommitted:
            case IsolationLevel.ReadCommitted:
                return DeferredIsolationLevel;
            case IsolationLevel.RepeatableRead:
            case IsolationLevel.Serializable:
            case IsolationLevel.Snapshot:
                return ImmediateIsolationLevel;
            default:
                return GetFallbackDefaultIsolationLevel();
        }
    }

    /// <summary>
    /// Opens the connection using the parameters found in the <see cref="ConnectionString" />.
    /// </summary>
    public override void Open()
    {
      CheckDisposed();
................................................................................
        bool usePooling = SQLiteConvert.ToBoolean(FindKey(opts, "Pooling", GetDefaultPooling().ToString()));
        int maxPoolSize = Convert.ToInt32(FindKey(opts, "Max Pool Size", DefaultMaxPoolSize.ToString()), CultureInfo.InvariantCulture);

        _defaultTimeout = Convert.ToInt32(FindKey(opts, "Default Timeout", DefaultConnectionTimeout.ToString()), CultureInfo.InvariantCulture);

        enumValue = TryParseEnum(typeof(IsolationLevel), FindKey(opts, "Default IsolationLevel", DefaultIsolationLevel.ToString()), true);
        _defaultIsolation = (enumValue is IsolationLevel) ? (IsolationLevel)enumValue : DefaultIsolationLevel;
        _defaultIsolation = GetEffectiveIsolationLevel(_defaultIsolation);


        if (_defaultIsolation != ImmediateIsolationLevel && _defaultIsolation != DeferredIsolationLevel)
          throw new NotSupportedException("Invalid Default IsolationLevel specified");

        _baseSchemaName = FindKey(opts, "BaseSchemaName", DefaultBaseSchemaName);

        if (_sql == null)
        {
            SetupSQLiteBase(opts);

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

4
5
6
7
8
9
10

11
12
13
14
15
16
17
18
19






20
21
22


23
24
25
26
27
28
29
30
31
32
33
34



35
36
37
38
39
40

41
42
43
44







45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61


62
63
64
65
66
67
68
69
70
71
72


73
74







75
76
77
78
79
80
81
82
 *
 * Released to the public domain, use at your own risk!
 ********************************************************/

#if !PLATFORM_COMPACTFRAMEWORK
namespace System.Data.SQLite
{

  using System.Transactions;

  internal sealed class SQLiteEnlistment : IDisposable, IEnlistmentNotification
  {
    internal SQLiteTransaction _transaction;
    internal Transaction _scope;
    internal bool _disposeConnection;

    internal SQLiteEnlistment(SQLiteConnection cnn, Transaction scope)






    {
      _transaction = cnn.BeginTransaction(GetSystemDataIsolationLevel(
          cnn, scope));



      _scope = scope;

      _scope.EnlistVolatile(this, System.Transactions.EnlistmentOptions.None);
    }

    ///////////////////////////////////////////////////////////////////////////

    #region Private Methods
    private System.Data.IsolationLevel GetSystemDataIsolationLevel(
        SQLiteConnection connection,
        Transaction transaction



        )
    {
        if (transaction == null)
        {
            //
            // TODO: Perhaps throw an exception here if the connection

            //       is null?
            //
            return (connection != null) ?
                connection.GetDefaultIsolationLevel() :







                SQLiteConnection.GetFallbackDefaultIsolationLevel();
        }

        System.Transactions.IsolationLevel isolationLevel =
            transaction.IsolationLevel;

        //
        // TODO: Are these isolation level mappings actually correct?
        //
        switch (isolationLevel)
        {
            case IsolationLevel.Chaos:
                return System.Data.IsolationLevel.Chaos;
            case IsolationLevel.ReadCommitted:
                return System.Data.IsolationLevel.ReadCommitted;
            case IsolationLevel.ReadUncommitted:
                return System.Data.IsolationLevel.ReadUncommitted;


            case IsolationLevel.RepeatableRead:
                return System.Data.IsolationLevel.RepeatableRead;
            case IsolationLevel.Serializable:
                return System.Data.IsolationLevel.Serializable;
            case IsolationLevel.Snapshot:
                return System.Data.IsolationLevel.Snapshot;
            case IsolationLevel.Unspecified:
                return System.Data.IsolationLevel.Unspecified;
        }

        //


        // TODO: Perhaps throw an exception here?
        //







        return SQLiteConnection.GetFallbackDefaultIsolationLevel();
    }

    ///////////////////////////////////////////////////////////////////////////

    private void Cleanup(SQLiteConnection cnn)
    {
        if (_disposeConnection)







>
|







|
>
>
>
>
>
>


<
>
>











|
>
>
>





|
>
|

|
|
>
>
>
>
>
>
>
|










|
|
|
|


>
>






<
<



>
>
|

>
>
>
>
>
>
>
|







4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
 *
 * Released to the public domain, use at your own risk!
 ********************************************************/

#if !PLATFORM_COMPACTFRAMEWORK
namespace System.Data.SQLite
{
    using System.Globalization;
    using System.Transactions;

  internal sealed class SQLiteEnlistment : IDisposable, IEnlistmentNotification
  {
    internal SQLiteTransaction _transaction;
    internal Transaction _scope;
    internal bool _disposeConnection;

    internal SQLiteEnlistment(
        SQLiteConnection cnn,
        Transaction scope,
        System.Data.IsolationLevel defaultIsolationLevel,
        bool throwOnUnavailable,
        bool throwOnUnsupported
        )
    {
      _transaction = cnn.BeginTransaction(GetSystemDataIsolationLevel(

          cnn, scope, defaultIsolationLevel, throwOnUnavailable,
          throwOnUnsupported));

      _scope = scope;

      _scope.EnlistVolatile(this, System.Transactions.EnlistmentOptions.None);
    }

    ///////////////////////////////////////////////////////////////////////////

    #region Private Methods
    private System.Data.IsolationLevel GetSystemDataIsolationLevel(
        SQLiteConnection connection,
        Transaction transaction,
        System.Data.IsolationLevel defaultIsolationLevel,
        bool throwOnUnavailable,
        bool throwOnUnsupported
        )
    {
        if (transaction == null)
        {
            //
            // NOTE: If neither the transaction nor connection isolation
            //       level is available, throw an exception if instructed
            //       by the caller.
            //
            if (connection != null)
                return connection.GetDefaultIsolationLevel();

            if (throwOnUnavailable)
            {
                throw new InvalidOperationException(
                    "isolation level is unavailable");
            }

            return defaultIsolationLevel;
        }

        System.Transactions.IsolationLevel isolationLevel =
            transaction.IsolationLevel;

        //
        // TODO: Are these isolation level mappings actually correct?
        //
        switch (isolationLevel)
        {
            case IsolationLevel.Unspecified:
                return System.Data.IsolationLevel.Unspecified;
            case IsolationLevel.Chaos:
                return System.Data.IsolationLevel.Chaos;
            case IsolationLevel.ReadUncommitted:
                return System.Data.IsolationLevel.ReadUncommitted;
            case IsolationLevel.ReadCommitted:
                return System.Data.IsolationLevel.ReadCommitted;
            case IsolationLevel.RepeatableRead:
                return System.Data.IsolationLevel.RepeatableRead;
            case IsolationLevel.Serializable:
                return System.Data.IsolationLevel.Serializable;
            case IsolationLevel.Snapshot:
                return System.Data.IsolationLevel.Snapshot;


        }

        //
        // NOTE: When in "strict" mode, throw an exception if the isolation
        //       level is not recognized; otherwise, fallback to the default
        //       isolation level specified by the caller.
        //
        if (throwOnUnsupported)
        {
            throw new InvalidOperationException(
                String.Format(CultureInfo.InvariantCulture,
                "unsupported isolation level {0}", isolationLevel));
        }

        return defaultIsolationLevel;
    }

    ///////////////////////////////////////////////////////////////////////////

    private void Cleanup(SQLiteConnection cnn)
    {
        if (_disposeConnection)

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

30
31
32
33
34
35
36
37


38
39
40
41
42
43
44
    /// <param name="connection">The connection to open a transaction on</param>
    /// <param name="deferredLock">TRUE to defer the writelock, or FALSE to lock immediately</param>
    internal SQLiteTransaction(SQLiteConnection connection, bool deferredLock)
    {
      _cnn = connection;
      _version = _cnn._version;

      _level = (deferredLock == true) ? IsolationLevel.ReadCommitted : IsolationLevel.Serializable;



      if (_cnn._transactionLevel++ == 0)
      {
        try
        {
          using (SQLiteCommand cmd = _cnn.CreateCommand())
          {







|
>
>







30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
    /// <param name="connection">The connection to open a transaction on</param>
    /// <param name="deferredLock">TRUE to defer the writelock, or FALSE to lock immediately</param>
    internal SQLiteTransaction(SQLiteConnection connection, bool deferredLock)
    {
      _cnn = connection;
      _version = _cnn._version;

      _level = (deferredLock == true) ?
          SQLiteConnection.DeferredIsolationLevel :
          SQLiteConnection.ImmediateIsolationLevel;

      if (_cnn._transactionLevel++ == 0)
      {
        try
        {
          using (SQLiteCommand cmd = _cnn.CreateCommand())
          {

Changes to Tests/tkt-56b42d99c1.eagle.

406
407
408
409
410
411
412
413
414
415

















































































































































































































































































































































































































416
417
} -cleanup {
  cleanupDb $fileName

  unset -nocomplain result results errors code sql dataSource id db fileName
} -constraints {eagle monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite compileCSharp} -match regexp -result {^Ok\
System#CodeDom#Compiler#CompilerResults#\d+ \{\} 0 1$}}

###############################################################################


















































































































































































































































































































































































































runSQLiteTestEpilogue
runTestEpilogue










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


406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
} -cleanup {
  cleanupDb $fileName

  unset -nocomplain result results errors code sql dataSource id db fileName
} -constraints {eagle monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite compileCSharp} -match regexp -result {^Ok\
System#CodeDom#Compiler#CompilerResults#\d+ \{\} 0 1$}}

###############################################################################

set flags MapIsolationLevels

###############################################################################

runTest {test tkt-56b42d99c1-1.6 {enlisted transaction isolation} -setup {
  setupDb [set fileName tkt-56b42d99c1-1.6.db]
} -body {
  set id [object invoke Interpreter.GetActive NextId]
  set dataSource [file join [getDatabaseDirectory] $fileName]

  unset -nocomplain results errors

  set code [compileCSharpWith [subst {
    using System.Data.SQLite;
    using System.Reflection;
    using System.Transactions;

    namespace _Dynamic${id}
    {
      public static class Test${id}
      {
        public static bool TryEnlistInTransaction()
        {
          TransactionOptions transactionOptions = new TransactionOptions();
          transactionOptions.IsolationLevel = IsolationLevel.ReadUncommitted;

          using (TransactionScope transactionScope = new TransactionScope(
              TransactionScopeOption.Required, transactionOptions))
          {
            using (SQLiteConnection connection1 = new SQLiteConnection(
                "Data Source=${dataSource};[getFlagsProperty $flags]"))
            {
              connection1.Open();

              using (SQLiteConnection connection2 = new SQLiteConnection(
                  "Data Source=${dataSource};[getFlagsProperty $flags]"))
              {
                connection2.Open();

                BindingFlags bindingFlags = BindingFlags.Instance |
                  BindingFlags.NonPublic | BindingFlags.GetField;

                FieldInfo fieldInfo1 = connection1.GetType().GetField(
                  "_enlistment", bindingFlags);

                object enlistment1 = fieldInfo1.GetValue(connection1);
                object enlistment2 = fieldInfo1.GetValue(connection2);

                FieldInfo fieldInfo2 = enlistment1.GetType().GetField(
                  "_transaction", bindingFlags);

                SQLiteTransaction transaction1 =
                  (SQLiteTransaction)fieldInfo2.GetValue(enlistment1);

                SQLiteTransaction transaction2 =
                  (SQLiteTransaction)fieldInfo2.GetValue(enlistment2);

                return (transaction1.IsolationLevel ==
                        transaction2.IsolationLevel);
              }
            }
          }
        }

        ///////////////////////////////////////////////////////////////////////

        public static void Main()
        {
          // do nothing.
        }
      }
    }
  }] true true true results errors System.Data.SQLite.dll]

  list $code $results \
      [expr {[info exists errors] ? $errors : ""}] \
      [expr {$code eq "Ok" ? [catch {
        object invoke _Dynamic${id}.Test${id} TryEnlistInTransaction
      } result] : [set result ""]}] $result
} -cleanup {
  cleanupDb $fileName

  unset -nocomplain result results errors code dataSource id db fileName
} -constraints {eagle monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite compileCSharp} -match regexp -result {^Ok\
System#CodeDom#Compiler#CompilerResults#\d+ \{\} 0 True$}}

###############################################################################

runTest {test tkt-56b42d99c1-1.7 {enlisted transaction isolation} -setup {
  setupDb [set fileName tkt-56b42d99c1-1.7.db]
} -body {
  set id [object invoke Interpreter.GetActive NextId]
  set dataSource [file join [getDatabaseDirectory] $fileName]

  unset -nocomplain results errors

  set sql(1) { \
    CREATE TABLE t1(x); \
    INSERT INTO t1 (x) VALUES(1); \
  }

  set sql(2) { \
    SELECT COUNT(*) FROM sqlite_master WHERE type = 'table'; \
  }

  set code [compileCSharpWith [subst {
    using System.Data.SQLite;
    using System.Transactions;

    namespace _Dynamic${id}
    {
      public static class Test${id}
      {
        public static int Main()
        {
          TransactionOptions transactionOptions = new TransactionOptions();
          transactionOptions.IsolationLevel = IsolationLevel.ReadUncommitted;

          using (TransactionScope transactionScope = new TransactionScope(
              TransactionScopeOption.Required, transactionOptions))
          {
            using (SQLiteConnection connection1 = new SQLiteConnection(
                "Data Source=${dataSource};[getFlagsProperty $flags]"))
            {
              connection1.Open();

              using (SQLiteConnection connection2 = new SQLiteConnection(
                  "Data Source=${dataSource};[getFlagsProperty $flags]"))
              {
                connection2.Open();

                using (SQLiteCommand command1 = connection1.CreateCommand())
                {
                  command1.CommandText = "${sql(1)}";
                  command1.ExecuteNonQuery();

                  using (SQLiteCommand command2 = connection2.CreateCommand())
                  {
                    command2.CommandText = "${sql(2)}";
                    return (int)(long)command2.ExecuteScalar();
                  }
                }
              }
            }
          }
        }
      }
    }
  }] true true true results errors System.Data.SQLite.dll]

  list $code $results \
      [expr {[info exists errors] ? $errors : ""}] \
      [expr {$code eq "Ok" ? [catch {
        object invoke _Dynamic${id}.Test${id} Main
      } result] : [set result ""]}] $result
} -cleanup {
  cleanupDb $fileName

  unset -nocomplain result results errors code sql dataSource id db fileName
} -constraints {eagle monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite compileCSharp} -match regexp -result {^Ok\
System#CodeDom#Compiler#CompilerResults#\d+ \{\} 0 0$}}

###############################################################################

runTest {test tkt-56b42d99c1-1.8 {enlisted transaction isolation} -setup {
  setupDb [set fileName tkt-56b42d99c1-1.8.db]
} -body {
  set id [object invoke Interpreter.GetActive NextId]
  set dataSource [file join [getDatabaseDirectory] $fileName]

  unset -nocomplain results errors

  set sql(1) { \
    CREATE TABLE t1(x); \
    INSERT INTO t1 (x) VALUES(1); \
  }

  set sql(2) { \
    SELECT COUNT(*) FROM sqlite_master WHERE type = 'table'; \
  }

  set code [compileCSharpWith [subst {
    using System.Data.SQLite;
    using System.Transactions;

    namespace _Dynamic${id}
    {
      public static class Test${id}
      {
        public static int Main()
        {
          TransactionOptions transactionOptions = new TransactionOptions();
          transactionOptions.IsolationLevel = IsolationLevel.ReadUncommitted;

          using (TransactionScope transactionScope = new TransactionScope(
              TransactionScopeOption.Required, transactionOptions))
          {
            using (SQLiteConnection connection1 = new SQLiteConnection(
                "Data Source=${dataSource};Enlist=False;[getFlagsProperty $flags]"))
            {
              connection1.Open();

              using (SQLiteConnection connection2 = new SQLiteConnection(
                  "Data Source=${dataSource};Enlist=False;[getFlagsProperty $flags]"))
              {
                connection2.Open();

                using (SQLiteCommand command1 = connection1.CreateCommand())
                {
                  command1.CommandText = "${sql(1)}";
                  command1.ExecuteNonQuery();

                  using (SQLiteCommand command2 = connection2.CreateCommand())
                  {
                    command2.CommandText = "${sql(2)}";
                    return (int)(long)command2.ExecuteScalar();
                  }
                }
              }
            }
          }
        }
      }
    }
  }] true true true results errors System.Data.SQLite.dll]

  list $code $results \
      [expr {[info exists errors] ? $errors : ""}] \
      [expr {$code eq "Ok" ? [catch {
        object invoke _Dynamic${id}.Test${id} Main
      } result] : [set result ""]}] $result
} -cleanup {
  cleanupDb $fileName

  unset -nocomplain result results errors code sql dataSource id db fileName
} -constraints {eagle monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite compileCSharp} -match regexp -result {^Ok\
System#CodeDom#Compiler#CompilerResults#\d+ \{\} 0 1$}}

###############################################################################

runTest {test tkt-56b42d99c1-1.9 {enlisted transaction isolation} -setup {
  setupDb [set fileName tkt-56b42d99c1-1.9.db]
} -body {
  set id [object invoke Interpreter.GetActive NextId]
  set dataSource [file join [getDatabaseDirectory] $fileName]

  unset -nocomplain results errors

  set sql(1) { \
    CREATE TABLE t1(x); \
    INSERT INTO t1 (x) VALUES(1); \
  }

  set sql(2) { \
    SELECT COUNT(*) FROM sqlite_master WHERE type = 'table'; \
  }

  set code [compileCSharpWith [subst {
    using System.Data.SQLite;
    using System.Transactions;

    namespace _Dynamic${id}
    {
      public static class Test${id}
      {
        public static int Main()
        {
          TransactionOptions transactionOptions = new TransactionOptions();
          transactionOptions.IsolationLevel = IsolationLevel.ReadUncommitted;

          using (TransactionScope transactionScope = new TransactionScope(
              TransactionScopeOption.Required, transactionOptions))
          {
            using (SQLiteConnection connection1 = new SQLiteConnection(
                "Data Source=${dataSource};[getFlagsProperty $flags]"))
            {
              connection1.Open();

              using (SQLiteConnection connection2 = new SQLiteConnection(
                  "Data Source=${dataSource};Enlist=False;[getFlagsProperty $flags]"))
              {
                connection2.Open();

                using (SQLiteCommand command1 = connection1.CreateCommand())
                {
                  command1.CommandText = "${sql(1)}";
                  command1.ExecuteNonQuery();

                  using (SQLiteCommand command2 = connection2.CreateCommand())
                  {
                    command2.CommandText = "${sql(2)}";
                    return (int)(long)command2.ExecuteScalar();
                  }
                }
              }
            }
          }
        }
      }
    }
  }] true true true results errors System.Data.SQLite.dll]

  list $code $results \
      [expr {[info exists errors] ? $errors : ""}] \
      [expr {$code eq "Ok" ? [catch {
        object invoke _Dynamic${id}.Test${id} Main
      } result] : [set result ""]}] $result
} -cleanup {
  cleanupDb $fileName

  unset -nocomplain result results errors code sql dataSource id db fileName
} -constraints {eagle monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite compileCSharp} -match regexp -result {^Ok\
System#CodeDom#Compiler#CompilerResults#\d+ \{\} 0 0$}}

###############################################################################

runTest {test tkt-56b42d99c1-1.10 {enlisted transaction isolation} -setup {
  setupDb [set fileName tkt-56b42d99c1-1.10.db]
} -body {
  set id [object invoke Interpreter.GetActive NextId]
  set dataSource [file join [getDatabaseDirectory] $fileName]

  unset -nocomplain results errors

  set sql(1) { \
    CREATE TABLE t1(x); \
    INSERT INTO t1 (x) VALUES(1); \
  }

  set sql(2) { \
    SELECT COUNT(*) FROM sqlite_master WHERE type = 'table'; \
  }

  set code [compileCSharpWith [subst {
    using System.Data.SQLite;
    using System.Transactions;

    namespace _Dynamic${id}
    {
      public static class Test${id}
      {
        public static int Main()
        {
          TransactionOptions transactionOptions = new TransactionOptions();
          transactionOptions.IsolationLevel = IsolationLevel.ReadUncommitted;

          using (TransactionScope transactionScope = new TransactionScope(
              TransactionScopeOption.Required, transactionOptions))
          {
            using (SQLiteConnection connection1 = new SQLiteConnection(
                "Data Source=${dataSource};Enlist=False;[getFlagsProperty $flags]"))
            {
              connection1.Open();

              using (SQLiteConnection connection2 = new SQLiteConnection(
                  "Data Source=${dataSource};[getFlagsProperty $flags]"))
              {
                connection2.Open();

                using (SQLiteCommand command1 = connection1.CreateCommand())
                {
                  command1.CommandText = "${sql(1)}";
                  command1.ExecuteNonQuery();

                  using (SQLiteCommand command2 = connection2.CreateCommand())
                  {
                    command2.CommandText = "${sql(2)}";
                    return (int)(long)command2.ExecuteScalar();
                  }
                }
              }
            }
          }
        }
      }
    }
  }] true true true results errors System.Data.SQLite.dll]

  list $code $results \
      [expr {[info exists errors] ? $errors : ""}] \
      [expr {$code eq "Ok" ? [catch {
        object invoke _Dynamic${id}.Test${id} Main
      } result] : [set result ""]}] $result
} -cleanup {
  cleanupDb $fileName

  unset -nocomplain result results errors code sql dataSource id db fileName
} -constraints {eagle monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite compileCSharp} -match regexp -result {^Ok\
System#CodeDom#Compiler#CompilerResults#\d+ \{\} 0 1$}}

###############################################################################

unset -nocomplain flags

###############################################################################

runSQLiteTestEpilogue
runTestEpilogue