diff options
author | AlexRa | 2010-04-28 13:40:35 +0300 |
---|---|---|
committer | AlexRa | 2010-04-28 13:40:35 +0300 |
commit | be1141f0f7994cb068683e07fe99936025361001 (patch) | |
tree | e1b2c66c7a65f0ed724d6d3e299da0d5f0bc6be8 /OpenSim/Data/Migration.cs | |
parent | Fixed comments in Migration.cs: wrong argument order (no change to code) (diff) | |
download | opensim-SC-be1141f0f7994cb068683e07fe99936025361001.zip opensim-SC-be1141f0f7994cb068683e07fe99936025361001.tar.gz opensim-SC-be1141f0f7994cb068683e07fe99936025361001.tar.bz2 opensim-SC-be1141f0f7994cb068683e07fe99936025361001.tar.xz |
Refactoring in Migration.cs: "using()" instead of explicit Dispose()
This ensures that 'cmd' gets disposed on errors
Diffstat (limited to 'OpenSim/Data/Migration.cs')
-rw-r--r-- | OpenSim/Data/Migration.cs | 108 |
1 files changed, 56 insertions, 52 deletions
diff --git a/OpenSim/Data/Migration.cs b/OpenSim/Data/Migration.cs index 5ca8e44..06defe4 100644 --- a/OpenSim/Data/Migration.cs +++ b/OpenSim/Data/Migration.cs | |||
@@ -110,10 +110,11 @@ namespace OpenSim.Data | |||
110 | return; | 110 | return; |
111 | 111 | ||
112 | // If not, create the migration tables | 112 | // If not, create the migration tables |
113 | DbCommand cmd = _conn.CreateCommand(); | 113 | using (DbCommand cmd = _conn.CreateCommand()) |
114 | cmd.CommandText = _migrations_create; | 114 | { |
115 | cmd.ExecuteNonQuery(); | 115 | cmd.CommandText = _migrations_create; |
116 | cmd.Dispose(); | 116 | cmd.ExecuteNonQuery(); |
117 | } | ||
117 | 118 | ||
118 | InsertVersion("migrations", 1); | 119 | InsertVersion("migrations", 1); |
119 | } | 120 | } |
@@ -131,37 +132,37 @@ namespace OpenSim.Data | |||
131 | m_log.InfoFormat("[MIGRATIONS] Upgrading {0} to latest revision {1}.", _type, migrations.Keys[migrations.Count - 1]); | 132 | m_log.InfoFormat("[MIGRATIONS] Upgrading {0} to latest revision {1}.", _type, migrations.Keys[migrations.Count - 1]); |
132 | m_log.Info("[MIGRATIONS] NOTE: this may take a while, don't interupt this process!"); | 133 | m_log.Info("[MIGRATIONS] NOTE: this may take a while, don't interupt this process!"); |
133 | 134 | ||
134 | DbCommand cmd = _conn.CreateCommand(); | 135 | using (DbCommand cmd = _conn.CreateCommand()) |
135 | foreach (KeyValuePair<int, string> kvp in migrations) | ||
136 | { | 136 | { |
137 | int newversion = kvp.Key; | 137 | foreach (KeyValuePair<int, string> kvp in migrations) |
138 | cmd.CommandText = kvp.Value; | ||
139 | // we need to up the command timeout to infinite as we might be doing long migrations. | ||
140 | cmd.CommandTimeout = 0; | ||
141 | try | ||
142 | { | ||
143 | cmd.ExecuteNonQuery(); | ||
144 | } | ||
145 | catch (Exception e) | ||
146 | { | 138 | { |
147 | m_log.DebugFormat("[MIGRATIONS] Cmd was {0}", cmd.CommandText); | 139 | int newversion = kvp.Key; |
148 | m_log.DebugFormat("[MIGRATIONS]: An error has occurred in the migration {0}.\n This may mean you could see errors trying to run OpenSim. If you see database related errors, you will need to fix the issue manually. Continuing.", e.Message); | 140 | cmd.CommandText = kvp.Value; |
149 | cmd.CommandText = "ROLLBACK;"; | 141 | // we need to up the command timeout to infinite as we might be doing long migrations. |
150 | cmd.ExecuteNonQuery(); | 142 | cmd.CommandTimeout = 0; |
151 | } | 143 | try |
144 | { | ||
145 | cmd.ExecuteNonQuery(); | ||
146 | } | ||
147 | catch (Exception e) | ||
148 | { | ||
149 | m_log.DebugFormat("[MIGRATIONS] Cmd was {0}", cmd.CommandText); | ||
150 | m_log.DebugFormat("[MIGRATIONS]: An error has occurred in the migration {0}.\n This may mean you could see errors trying to run OpenSim. If you see database related errors, you will need to fix the issue manually. Continuing.", e.Message); | ||
151 | cmd.CommandText = "ROLLBACK;"; | ||
152 | cmd.ExecuteNonQuery(); | ||
153 | } | ||
152 | 154 | ||
153 | if (version == 0) | 155 | if (version == 0) |
154 | { | 156 | { |
155 | InsertVersion(_type, newversion); | 157 | InsertVersion(_type, newversion); |
156 | } | 158 | } |
157 | else | 159 | else |
158 | { | 160 | { |
159 | UpdateVersion(_type, newversion); | 161 | UpdateVersion(_type, newversion); |
162 | } | ||
163 | version = newversion; | ||
160 | } | 164 | } |
161 | version = newversion; | ||
162 | } | 165 | } |
163 | |||
164 | cmd.Dispose(); | ||
165 | } | 166 | } |
166 | 167 | ||
167 | // private int MaxVersion() | 168 | // private int MaxVersion() |
@@ -200,43 +201,46 @@ namespace OpenSim.Data | |||
200 | protected virtual int FindVersion(DbConnection conn, string type) | 201 | protected virtual int FindVersion(DbConnection conn, string type) |
201 | { | 202 | { |
202 | int version = 0; | 203 | int version = 0; |
203 | DbCommand cmd = conn.CreateCommand(); | 204 | using (DbCommand cmd = conn.CreateCommand()) |
204 | try | ||
205 | { | 205 | { |
206 | cmd.CommandText = "select version from migrations where name='" + type +"' order by version desc"; | 206 | try |
207 | using (IDataReader reader = cmd.ExecuteReader()) | ||
208 | { | 207 | { |
209 | if (reader.Read()) | 208 | cmd.CommandText = "select version from migrations where name='" + type + "' order by version desc"; |
209 | using (IDataReader reader = cmd.ExecuteReader()) | ||
210 | { | 210 | { |
211 | version = Convert.ToInt32(reader["version"]); | 211 | if (reader.Read()) |
212 | { | ||
213 | version = Convert.ToInt32(reader["version"]); | ||
214 | } | ||
215 | reader.Close(); | ||
212 | } | 216 | } |
213 | reader.Close(); | 217 | } |
218 | catch | ||
219 | { | ||
220 | // Something went wrong, so we're version 0 | ||
214 | } | 221 | } |
215 | } | 222 | } |
216 | catch | ||
217 | { | ||
218 | // Something went wrong, so we're version 0 | ||
219 | } | ||
220 | cmd.Dispose(); | ||
221 | return version; | 223 | return version; |
222 | } | 224 | } |
223 | 225 | ||
224 | private void InsertVersion(string type, int version) | 226 | private void InsertVersion(string type, int version) |
225 | { | 227 | { |
226 | DbCommand cmd = _conn.CreateCommand(); | 228 | using (DbCommand cmd = _conn.CreateCommand()) |
227 | cmd.CommandText = "insert into migrations(name, version) values('" + type + "', " + version + ")"; | 229 | { |
228 | m_log.InfoFormat("[MIGRATIONS]: Creating {0} at version {1}", type, version); | 230 | cmd.CommandText = "insert into migrations(name, version) values('" + type + "', " + version + ")"; |
229 | cmd.ExecuteNonQuery(); | 231 | m_log.InfoFormat("[MIGRATIONS]: Creating {0} at version {1}", type, version); |
230 | cmd.Dispose(); | 232 | cmd.ExecuteNonQuery(); |
233 | } | ||
231 | } | 234 | } |
232 | 235 | ||
233 | private void UpdateVersion(string type, int version) | 236 | private void UpdateVersion(string type, int version) |
234 | { | 237 | { |
235 | DbCommand cmd = _conn.CreateCommand(); | 238 | using (DbCommand cmd = _conn.CreateCommand()) |
236 | cmd.CommandText = "update migrations set version=" + version + " where name='" + type + "'"; | 239 | { |
237 | m_log.InfoFormat("[MIGRATIONS]: Updating {0} to version {1}", type, version); | 240 | cmd.CommandText = "update migrations set version=" + version + " where name='" + type + "'"; |
238 | cmd.ExecuteNonQuery(); | 241 | m_log.InfoFormat("[MIGRATIONS]: Updating {0} to version {1}", type, version); |
239 | cmd.Dispose(); | 242 | cmd.ExecuteNonQuery(); |
243 | } | ||
240 | } | 244 | } |
241 | 245 | ||
242 | // private SortedList<int, string> GetAllMigrations() | 246 | // private SortedList<int, string> GetAllMigrations() |