Skip to content

Commit 003f5e6

Browse files
fix(csharp/src/Client): Fix some resource management in AdbcDataReader (#4134)
Fixes a few small issues I noticed while reviewing a change recently.
1 parent 153762d commit 003f5e6

2 files changed

Lines changed: 18 additions & 19 deletions

File tree

csharp/src/Client/AdbcDataReader.cs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,7 @@ private RecordBatch RecordBatch
134134

135135
public override void Close()
136136
{
137-
if (this.closeConnection)
138-
{
139-
this.adbcCommand?.Connection?.Close();
140-
}
141-
this.adbcQueryResult.Stream?.Dispose();
142-
this.adbcQueryResult.Stream = null;
143-
this.isClosed = true;
137+
this.Dispose(disposing: true);
144138
}
145139

146140
public override bool GetBoolean(int ordinal)
@@ -308,22 +302,23 @@ public override bool IsDBNull(int ordinal)
308302

309303
public override bool NextResult()
310304
{
311-
this.recordBatch = ReadNextRecordBatchAsync().Result;
312-
313-
if (this.recordBatch != null)
314-
{
315-
return true;
316-
}
317-
318305
return false;
319306
}
320307

321308
protected override void Dispose(bool disposing)
322309
{
323310
if (disposing)
324311
{
312+
if (this.closeConnection)
313+
{
314+
this.adbcCommand?.Connection?.Close();
315+
}
316+
325317
this.recordBatch?.Dispose();
326318
this.recordBatch = null;
319+
this.adbcQueryResult.Stream?.Dispose();
320+
this.adbcQueryResult.Stream = null;
321+
this.isClosed = true;
327322
}
328323
}
329324

@@ -339,6 +334,7 @@ public override bool Read()
339334
// If ReadNextRecordBatchAsync throws (e.g. server error mid-stream),
340335
// callers that retry Read() must not re-read stale rows from the
341336
// old batch — they must see the exception again immediately.
337+
this.recordBatch?.Dispose();
342338
this.recordBatch = null;
343339
this.recordBatch = ReadNextRecordBatchAsync().Result;
344340

csharp/test/Apache.Arrow.Adbc.Tests/Client/DuckDbClientTests.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,16 @@ public void BindParameters()
139139
command.Parameters[1].DbType = DbType.String;
140140
command.Parameters[1].Value = "foo";
141141

142-
using var reader = command.ExecuteReader();
143-
long count = 0;
144-
while (reader.Read())
142+
using (var reader = command.ExecuteReader(CommandBehavior.CloseConnection))
145143
{
146-
count++;
144+
long count = 0;
145+
while (reader.Read())
146+
{
147+
count++;
148+
}
149+
Assert.Equal(1, count);
147150
}
148-
Assert.Equal(1, count);
151+
Assert.True(connection.State == ConnectionState.Closed);
149152
}
150153

151154
private static long GetResultCount(AdbcCommand command, string query)

0 commit comments

Comments
 (0)