From 29a4614529bbda02b9c690d2d1812be1d1e7bbae Mon Sep 17 00:00:00 2001 From: John Hurliman Date: Sun, 4 Oct 2009 13:57:51 -0700 Subject: * MySQL data tests now pass by fixing a bad fix for a bad cast on the asset Local member in MySQLAssetData * First pass at applying the using(){} pattern to IDisposable objects. Always use the using pattern on IDisposable objects whenever possible, do not manually call .Close() or .Dispose() unless there is no other way to write the code. This pass mostly covers OpenSim.Data.MySQL, and should have no functional change (tests still pass) --- OpenSim/Data/Migration.cs | 79 ++++++++++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 38 deletions(-) (limited to 'OpenSim/Data/Migration.cs') diff --git a/OpenSim/Data/Migration.cs b/OpenSim/Data/Migration.cs index e51dc22..7a99d4a 100644 --- a/OpenSim/Data/Migration.cs +++ b/OpenSim/Data/Migration.cs @@ -131,25 +131,26 @@ namespace OpenSim.Data m_log.InfoFormat("[MIGRATIONS] Upgrading {0} to latest revision.", _type); m_log.Info("[MIGRATIONS] NOTE: this may take a while, don't interupt this process!"); - DbCommand cmd = _conn.CreateCommand(); - foreach (KeyValuePair kvp in migrations) + using (DbCommand cmd = _conn.CreateCommand()) { - int newversion = kvp.Key; - cmd.CommandText = kvp.Value; - // we need to up the command timeout to infinite as we might be doing long migrations. - cmd.CommandTimeout = 0; - cmd.ExecuteNonQuery(); - - if (version == 0) - { - InsertVersion(_type, newversion); - } - else + foreach (KeyValuePair kvp in migrations) { - UpdateVersion(_type, newversion); + int newversion = kvp.Key; + cmd.CommandText = kvp.Value; + // we need to up the command timeout to infinite as we might be doing long migrations. + cmd.CommandTimeout = 0; + cmd.ExecuteNonQuery(); + + if (version == 0) + { + InsertVersion(_type, newversion); + } + else + { + UpdateVersion(_type, newversion); + } + version = newversion; } - version = newversion; - cmd.Dispose(); } } @@ -189,43 +190,45 @@ namespace OpenSim.Data protected virtual int FindVersion(DbConnection conn, string type) { int version = 0; - DbCommand cmd = conn.CreateCommand(); - try + + using (DbCommand cmd = conn.CreateCommand()) { - cmd.CommandText = "select version from migrations where name='" + type +"' order by version desc"; - using (IDataReader reader = cmd.ExecuteReader()) + try { - if (reader.Read()) + cmd.CommandText = "select version from migrations where name='" + type + "' order by version desc"; + using (IDataReader reader = cmd.ExecuteReader()) { - version = Convert.ToInt32(reader["version"]); + if (reader.Read()) + version = Convert.ToInt32(reader["version"]); } - reader.Close(); + } + catch + { + // Something went wrong, so we're version 0 } } - catch - { - // Something went wrong, so we're version 0 - } - cmd.Dispose(); + return version; } private void InsertVersion(string type, int version) { - DbCommand cmd = _conn.CreateCommand(); - cmd.CommandText = "insert into migrations(name, version) values('" + type + "', " + version + ")"; - m_log.InfoFormat("[MIGRATIONS]: Creating {0} at version {1}", type, version); - cmd.ExecuteNonQuery(); - cmd.Dispose(); + using (DbCommand cmd = _conn.CreateCommand()) + { + cmd.CommandText = "insert into migrations(name, version) values('" + type + "', " + version + ")"; + m_log.InfoFormat("[MIGRATIONS]: Creating {0} at version {1}", type, version); + cmd.ExecuteNonQuery(); + } } private void UpdateVersion(string type, int version) { - DbCommand cmd = _conn.CreateCommand(); - cmd.CommandText = "update migrations set version=" + version + " where name='" + type + "'"; - m_log.InfoFormat("[MIGRATIONS]: Updating {0} to version {1}", type, version); - cmd.ExecuteNonQuery(); - cmd.Dispose(); + using (DbCommand cmd = _conn.CreateCommand()) + { + cmd.CommandText = "update migrations set version=" + version + " where name='" + type + "'"; + m_log.InfoFormat("[MIGRATIONS]: Updating {0} to version {1}", type, version); + cmd.ExecuteNonQuery(); + } } // private SortedList GetAllMigrations() -- cgit v1.1