[banshee] Dispose the SqliteCommand after it's executed (bgo#586498)
- From: Alexander Kojevnikov <alexk src gnome org>
- To: svn-commits-list gnome org
- Subject: [banshee] Dispose the SqliteCommand after it's executed (bgo#586498)
- Date: Sun, 21 Jun 2009 21:04:07 -0400 (EDT)
commit b0fdb8a71681d858ac952dfbf43d6d3a2aa5caf6
Author: Alexander Kojevnikov <alexander kojevnikov com>
Date: Mon Jun 22 10:57:25 2009 +1000
Dispose the SqliteCommand after it's executed (bgo#586498)
Not disposing the command causes huge increases in memory usage when
re-scanning the library.
.../Hyena/Hyena.Data.Sqlite/HyenaSqliteCommand.cs | 67 ++++++++++----------
.../Hyena.Data.Sqlite/Tests/SqliteCommandTests.cs | 17 +++++
2 files changed, 51 insertions(+), 33 deletions(-)
---
diff --git a/src/Libraries/Hyena/Hyena.Data.Sqlite/HyenaSqliteCommand.cs b/src/Libraries/Hyena/Hyena.Data.Sqlite/HyenaSqliteCommand.cs
index fb08d65..53fc49e 100644
--- a/src/Libraries/Hyena/Hyena.Data.Sqlite/HyenaSqliteCommand.cs
+++ b/src/Libraries/Hyena/Hyena.Data.Sqlite/HyenaSqliteCommand.cs
@@ -109,43 +109,44 @@ namespace Hyena.Data.Sqlite
execution_exception = null;
result = null;
- SqliteCommand sql_command = new SqliteCommand (CurrentSqlText);
- sql_command.Connection = connection;
-
- hconnection.OnExecuting (sql_command);
-
- try {
- if (log_all)
- ticks = System.Environment.TickCount;
+ using (SqliteCommand sql_command = new SqliteCommand (CurrentSqlText)) {
+ sql_command.Connection = connection;
+
+ hconnection.OnExecuting (sql_command);
+
+ try {
+ if (log_all)
+ ticks = System.Environment.TickCount;
+
+ switch (command_type) {
+ case HyenaCommandType.Reader:
+ using (SqliteDataReader reader = sql_command.ExecuteReader ()) {
+ result = new HyenaSqliteArrayDataReader (reader);
+ }
+ break;
+
+ case HyenaCommandType.Scalar:
+ result = sql_command.ExecuteScalar ();
+ break;
+
+ case HyenaCommandType.Execute:
+ default:
+ sql_command.ExecuteNonQuery ();
+ result = sql_command.LastInsertRowID ();
+ break;
+ }
- switch (command_type) {
- case HyenaCommandType.Reader:
- using (SqliteDataReader reader = sql_command.ExecuteReader ()) {
- result = new HyenaSqliteArrayDataReader (reader);
+ if (log_all) {
+ Log.DebugFormat ("Executed in {0}ms {1}", System.Environment.TickCount - ticks, sql_command.CommandText);
+ CommandExecutedHandler handler = CommandExecuted;
+ if (handler != null) {
+ handler (this, new CommandExecutedArgs (Text, sql_command.CommandText, null, System.Environment.TickCount - ticks));
}
- break;
-
- case HyenaCommandType.Scalar:
- result = sql_command.ExecuteScalar ();
- break;
-
- case HyenaCommandType.Execute:
- default:
- sql_command.ExecuteNonQuery ();
- result = sql_command.LastInsertRowID ();
- break;
- }
-
- if (log_all) {
- Log.DebugFormat ("Executed in {0}ms {1}", System.Environment.TickCount - ticks, sql_command.CommandText);
- CommandExecutedHandler handler = CommandExecuted;
- if (handler != null) {
- handler (this, new CommandExecutedArgs (Text, sql_command.CommandText, null, System.Environment.TickCount - ticks));
}
+ } catch (Exception e) {
+ Log.DebugFormat (String.Format ("Exception executing command: {0}", sql_command.CommandText), e.ToString ());
+ execution_exception = e;
}
- } catch (Exception e) {
- Log.DebugFormat (String.Format ("Exception executing command: {0}", sql_command.CommandText), e.ToString ());
- execution_exception = e;
}
finished_event.Reset ();
diff --git a/src/Libraries/Hyena/Hyena.Data.Sqlite/Tests/SqliteCommandTests.cs b/src/Libraries/Hyena/Hyena.Data.Sqlite/Tests/SqliteCommandTests.cs
index 17a24a4..f847f1b 100644
--- a/src/Libraries/Hyena/Hyena.Data.Sqlite/Tests/SqliteCommandTests.cs
+++ b/src/Libraries/Hyena/Hyena.Data.Sqlite/Tests/SqliteCommandTests.cs
@@ -94,6 +94,23 @@ namespace Hyena.Data.Sqlite.Tests
Assert.AreEqual ("select foo from bar where id in (1,2,4) and foo not in ('foo','baz')",
GetGeneratedSql (cmd3, new int [] {1, 2, 4}, new string [] {"foo", "baz"}));
}
+
+ [Test]
+ public void ExecuteMustDisposeSqliteCommand ()
+ {
+ using(var connection = new HyenaSqliteConnection (":memory:")) {
+ bool disposed = false;
+ connection.Executing += delegate (object sender, ExecutingEventArgs args) {
+ args.Command.Disposed += delegate {
+ disposed = true;
+ };
+ };
+
+ connection.Query<int> ("SELECT 42");
+
+ Assert.IsTrue (disposed);
+ }
+ }
static PropertyInfo tf = typeof(HyenaSqliteCommand).GetProperty ("CurrentSqlText", BindingFlags.Instance | BindingFlags.NonPublic);
private static string GetGeneratedSql (HyenaSqliteCommand cmd, params object [] p)
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]