public override object DoWorkInCurrentTransaction(ISessionImplementor session, DbConnection conn, DbTransaction transaction) { long result; int rows; do { //the loop ensure atomicitiy of the //select + update even for no transaction //or read committed isolation level (needed for .net?) var qps = conn.CreateCommand(); DbDataReader rs = null; qps.CommandText = query; qps.CommandType = CommandType.Text; qps.Transaction = transaction; PersistentIdGeneratorParmsNames.SqlStatementLogger.LogCommand("Reading high value:", qps, FormatStyle.Basic); try { rs = qps.ExecuteReader(); if (!rs.Read()) { var errFormat = string.IsNullOrEmpty(whereClause) ? "could not read a hi value - you need to populate the table: {0}" : "could not read a hi value from table '{0}' using the where clause ({1})- you need to populate the table."; log.Error(errFormat, tableName, whereClause); throw new IdentifierGenerationException(string.Format(errFormat, tableName, whereClause)); } result = Convert.ToInt64(columnType.Get(rs, 0, session)); } catch (Exception e) { log.Error(e, "could not read a hi value"); throw; } finally { if (rs != null) { rs.Close(); } qps.Dispose(); } var ups = session.Factory.ConnectionProvider.Driver.GenerateCommand(CommandType.Text, updateSql, parameterTypes); ups.Connection = conn; ups.Transaction = transaction; try { columnType.Set(ups, result + 1, 0, session); columnType.Set(ups, result, 1, session); PersistentIdGeneratorParmsNames.SqlStatementLogger.LogCommand("Updating high value:", ups, FormatStyle.Basic); rows = ups.ExecuteNonQuery(); } catch (Exception e) { log.Error(e, "could not update hi value in: {0}", tableName); throw; } finally { ups.Dispose(); } }while (rows == 0); return(result); }