Skip to content

Commit 40206cd

Browse files
authored
Enrol GetSchema commands into current transaction (#2876)
1 parent 2a015d2 commit 40206cd

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/Microsoft.Data.SqlClient/src/Microsoft/Data/ProviderBase/DbMetaDataFactory.cs

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information.
44

55
using Microsoft.Data.Common;
6+
using Microsoft.Data.SqlClient;
67
using System;
78
using System.Data;
89
using System.Data.Common;
@@ -126,8 +127,11 @@ private DataTable ExecuteCommand(DataRow requestedCollectionRow, string[] restri
126127
}
127128

128129
DbCommand command = connection.CreateCommand();
130+
SqlConnection castConnection = connection as SqlConnection;
131+
129132
command.CommandText = sqlCommand;
130133
command.CommandTimeout = Math.Max(command.CommandTimeout, 180);
134+
command.Transaction = castConnection?.GetOpenTdsConnection()?.CurrentTransaction?.Parent;
131135

132136
for (int i = 0; i < numberOfRestrictions; i++)
133137
{

src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/SqlSchemaInfoTest/SqlSchemaInfoTest.cs

+17-3
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,27 @@ namespace Microsoft.Data.SqlClient.ManualTesting.Tests
1515
public static class SqlSchemaInfoTest
1616
{
1717
#region TestMethods
18-
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
19-
public static void TestGetSchema()
18+
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
19+
[InlineData(true)]
20+
[InlineData(false)]
21+
public static void TestGetSchema(bool openTransaction)
2022
{
2123
using (SqlConnection conn = new SqlConnection(DataTestUtility.TCPConnectionString))
2224
{
2325
conn.Open();
24-
DataTable dataBases = conn.GetSchema("DATABASES");
26+
DataTable dataBases;
27+
28+
if (openTransaction)
29+
{
30+
using (SqlTransaction transaction = conn.BeginTransaction())
31+
{
32+
dataBases = conn.GetSchema("DATABASES");
33+
}
34+
}
35+
else
36+
{
37+
dataBases = conn.GetSchema("DATABASES");
38+
}
2539

2640
Assert.True(dataBases.Rows.Count > 0, "At least one database is expected");
2741

0 commit comments

Comments
 (0)