Skip to content

Commit 71937fd

Browse files
committed
Fix NH-3731 by moving the try-catch-finally blocks inside the loops. This ensures that each created command is closed.
1 parent 19612c0 commit 71937fd

File tree

1 file changed

+68
-68
lines changed

1 file changed

+68
-68
lines changed

src/NHibernate/Persister/Collection/OneToManyPersister.cs

Lines changed: 68 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -164,29 +164,29 @@ protected override int DoUpdateRows(object id, IPersistentCollection collection,
164164
IExpectation deleteExpectation = Expectations.AppropriateExpectation(DeleteCheckStyle);
165165
bool useBatch = deleteExpectation.CanBeBatched;
166166
SqlCommandInfo sql = SqlDeleteRowString;
167-
IDbCommand st = null;
168167
// update removed rows fks to null
169-
try
170-
{
171-
int i = 0;
172-
IEnumerable entries = collection.Entries(this);
168+
int i = 0;
169+
IEnumerable entries = collection.Entries(this);
173170

174-
foreach (object entry in entries)
171+
foreach (object entry in entries)
172+
{
173+
if (collection.NeedsUpdating(entry, i, ElementType))
175174
{
176-
if (collection.NeedsUpdating(entry, i, ElementType))
175+
IDbCommand st = null;
176+
// will still be issued when it used to be null
177+
if (useBatch)
177178
{
178-
// will still be issued when it used to be null
179-
if (useBatch)
180-
{
181-
st = session.Batcher.PrepareBatchCommand(SqlDeleteRowString.CommandType, sql.Text,
182-
SqlDeleteRowString.ParameterTypes);
183-
}
184-
else
185-
{
186-
st = session.Batcher.PrepareCommand(SqlDeleteRowString.CommandType, sql.Text,
187-
SqlDeleteRowString.ParameterTypes);
188-
}
179+
st = session.Batcher.PrepareBatchCommand(SqlDeleteRowString.CommandType, sql.Text,
180+
SqlDeleteRowString.ParameterTypes);
181+
}
182+
else
183+
{
184+
st = session.Batcher.PrepareCommand(SqlDeleteRowString.CommandType, sql.Text,
185+
SqlDeleteRowString.ParameterTypes);
186+
}
189187

188+
try
189+
{
190190
int loc = WriteKey(st, id, offset, session);
191191
WriteElementToWhere(st, collection.GetSnapshotElement(entry, i), loc, session);
192192
if (useBatch)
@@ -197,25 +197,25 @@ protected override int DoUpdateRows(object id, IPersistentCollection collection,
197197
{
198198
deleteExpectation.VerifyOutcomeNonBatched(session.Batcher.ExecuteNonQuery(st), st);
199199
}
200-
count++;
201200
}
202-
i++;
203-
}
204-
}
205-
catch (Exception e)
206-
{
207-
if (useBatch)
208-
{
209-
session.Batcher.AbortBatch(e);
210-
}
211-
throw;
212-
}
213-
finally
214-
{
215-
if (!useBatch && st != null)
216-
{
217-
session.Batcher.CloseCommand(st, null);
201+
catch (Exception e)
202+
{
203+
if (useBatch)
204+
{
205+
session.Batcher.AbortBatch(e);
206+
}
207+
throw;
208+
}
209+
finally
210+
{
211+
if (!useBatch && st != null)
212+
{
213+
session.Batcher.CloseCommand(st, null);
214+
}
215+
}
216+
count++;
218217
}
218+
i++;
219219
}
220220
}
221221

@@ -225,28 +225,28 @@ protected override int DoUpdateRows(object id, IPersistentCollection collection,
225225
//bool callable = InsertCallable;
226226
bool useBatch = insertExpectation.CanBeBatched;
227227
SqlCommandInfo sql = SqlInsertRowString;
228-
IDbCommand st = null;
229228

230229
// now update all changed or added rows fks
231-
try
230+
int i = 0;
231+
IEnumerable entries = collection.Entries(this);
232+
foreach (object entry in entries)
232233
{
233-
int i = 0;
234-
IEnumerable entries = collection.Entries(this);
235-
foreach (object entry in entries)
234+
if (collection.NeedsUpdating(entry, i, ElementType))
236235
{
237-
if (collection.NeedsUpdating(entry, i, ElementType))
236+
IDbCommand st = null;
237+
if (useBatch)
238238
{
239-
if (useBatch)
240-
{
241-
st = session.Batcher.PrepareBatchCommand(SqlInsertRowString.CommandType, sql.Text,
242-
SqlInsertRowString.ParameterTypes);
243-
}
244-
else
245-
{
246-
st = session.Batcher.PrepareCommand(SqlInsertRowString.CommandType, sql.Text,
247-
SqlInsertRowString.ParameterTypes);
248-
}
239+
st = session.Batcher.PrepareBatchCommand(SqlInsertRowString.CommandType, sql.Text,
240+
SqlInsertRowString.ParameterTypes);
241+
}
242+
else
243+
{
244+
st = session.Batcher.PrepareCommand(SqlInsertRowString.CommandType, sql.Text,
245+
SqlInsertRowString.ParameterTypes);
246+
}
249247

248+
try
249+
{
250250
//offset += insertExpectation.Prepare(st, Factory.ConnectionProvider.Driver);
251251
int loc = WriteKey(st, id, offset, session);
252252
if (HasIndex && !indexContainsFormula)
@@ -262,25 +262,25 @@ protected override int DoUpdateRows(object id, IPersistentCollection collection,
262262
{
263263
insertExpectation.VerifyOutcomeNonBatched(session.Batcher.ExecuteNonQuery(st), st);
264264
}
265-
count++;
266265
}
267-
i++;
268-
}
269-
}
270-
catch (Exception e)
271-
{
272-
if (useBatch)
273-
{
274-
session.Batcher.AbortBatch(e);
275-
}
276-
throw;
277-
}
278-
finally
279-
{
280-
if (!useBatch && st != null)
281-
{
282-
session.Batcher.CloseCommand(st, null);
266+
catch (Exception e)
267+
{
268+
if (useBatch)
269+
{
270+
session.Batcher.AbortBatch(e);
271+
}
272+
throw;
273+
}
274+
finally
275+
{
276+
if (!useBatch && st != null)
277+
{
278+
session.Batcher.CloseCommand(st, null);
279+
}
280+
}
281+
count++;
283282
}
283+
i++;
284284
}
285285
}
286286
return count;

0 commit comments

Comments
 (0)