Skip to content

Commit 0f3882b

Browse files
committed
Fix disposing IDbDataReader
1 parent bb4f19f commit 0f3882b

File tree

1 file changed

+41
-49
lines changed

1 file changed

+41
-49
lines changed

src/NHibernate/Id/TableGenerator.cs

Lines changed: 41 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -220,67 +220,59 @@ public override object DoWorkInCurrentTransaction(ISessionImplementor session, I
220220
//select + uspdate even for no transaction
221221
//or read committed isolation level (needed for .net?)
222222

223-
IDbCommand qps = conn.CreateCommand();
224-
IDataReader rs = null;
225-
qps.CommandText = query;
226-
qps.CommandType = CommandType.Text;
227-
qps.Transaction = transaction;
228-
PersistentIdGeneratorParmsNames.SqlStatementLogger.LogCommand("Reading high value:", qps, FormatStyle.Basic);
229-
try
223+
using (var qps = conn.CreateCommand())
230224
{
231-
rs = qps.ExecuteReader();
232-
if (!rs.Read())
225+
qps.CommandText = query;
226+
qps.CommandType = CommandType.Text;
227+
qps.Transaction = transaction;
228+
PersistentIdGeneratorParmsNames.SqlStatementLogger.LogCommand("Reading high value:", qps, FormatStyle.Basic);
229+
try
233230
{
234-
string err;
235-
if (string.IsNullOrEmpty(whereClause))
231+
using (var rs = qps.ExecuteReader())
236232
{
237-
err = "could not read a hi value - you need to populate the table: " + tableName;
233+
if (!rs.Read())
234+
{
235+
string err;
236+
if (string.IsNullOrEmpty(whereClause))
237+
{
238+
err = "could not read a hi value - you need to populate the table: " + tableName;
239+
}
240+
else
241+
{
242+
err = string.Format("could not read a hi value from table '{0}' using the where clause ({1})- you need to populate the table.", tableName, whereClause);
243+
}
244+
log.Error(err);
245+
throw new IdentifierGenerationException(err);
246+
}
247+
result = Convert.ToInt64(columnType.Get(rs, 0));
238248
}
239-
else
240-
{
241-
err = string.Format("could not read a hi value from table '{0}' using the where clause ({1})- you need to populate the table.", tableName, whereClause);
242-
}
243-
log.Error(err);
244-
throw new IdentifierGenerationException(err);
245249
}
246-
result = Convert.ToInt64(columnType.Get(rs, 0));
247-
}
248-
catch (Exception e)
249-
{
250-
log.Error("could not read a hi value", e);
251-
throw;
252-
}
253-
finally
254-
{
255-
if (rs != null)
250+
catch (Exception e)
256251
{
257-
rs.Close();
252+
log.Error("could not read a hi value", e);
253+
throw;
258254
}
259-
qps.Dispose();
260255
}
261256

262-
IDbCommand ups = session.Factory.ConnectionProvider.Driver.GenerateCommand(CommandType.Text, updateSql,
263-
parameterTypes);
264-
ups.Connection = conn;
265-
ups.Transaction = transaction;
266-
267-
try
257+
using (var ups = session.Factory.ConnectionProvider.Driver.GenerateCommand(CommandType.Text, updateSql, parameterTypes))
268258
{
269-
columnType.Set(ups, result + 1, 0);
270-
columnType.Set(ups, result, 1);
259+
ups.Connection = conn;
260+
ups.Transaction = transaction;
261+
262+
try
263+
{
264+
columnType.Set(ups, result + 1, 0);
265+
columnType.Set(ups, result, 1);
271266

272-
PersistentIdGeneratorParmsNames.SqlStatementLogger.LogCommand("Updating high value:", ups, FormatStyle.Basic);
267+
PersistentIdGeneratorParmsNames.SqlStatementLogger.LogCommand("Updating high value:", ups, FormatStyle.Basic);
273268

274-
rows = ups.ExecuteNonQuery();
275-
}
276-
catch (Exception e)
277-
{
278-
log.Error("could not update hi value in: " + tableName, e);
279-
throw;
280-
}
281-
finally
282-
{
283-
ups.Dispose();
269+
rows = ups.ExecuteNonQuery();
270+
}
271+
catch (Exception e)
272+
{
273+
log.Error("could not update hi value in: " + tableName, e);
274+
throw;
275+
}
284276
}
285277
}
286278
while (rows == 0);

0 commit comments

Comments
 (0)