Ignore:
Timestamp:
09/23/26 13:29:17 (11 hours ago)
Author:
Stefan-Saveski <stefansaveski19@…>
Branches:
master
Children:
2853f2e
Parents:
e7bafc0 (diff), 002cf5f (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge remote-tracking branch 'origin/phase8-transactions-pooling'

File:
1 edited

Legend:

Unmodified
Added
Removed
  • iknow-api/Services/Implementations/EnrollmentService.cs

    re7bafc0 r0496e6e  
    33using iknow_api.Models;
    44using Microsoft.EntityFrameworkCore;
     5using Npgsql;
    56
    67namespace iknow_api.Services
     
    220221                };
    221222            }
     223            catch (DbUpdateException ex) when (IsUniqueViolation(ex))
     224            {
     225                // Two enrolments for the same semester sent at the same time both
     226                // pass the check above, because each transaction reads the state
     227                // from before the other one wrote. UNIQUE (user_id, semester_id)
     228                // is what actually settles it, so the loser gets the same
     229                // sentence as if the check had caught it.
     230                await transaction.RollbackAsync();
     231                return Fail("You are already enrolled in that semester.");
     232            }
    222233            catch
    223234            {
     
    226237            }
    227238        }
     239
     240        /// <summary>23505 is unique_violation.</summary>
     241        private static bool IsUniqueViolation(DbUpdateException ex) =>
     242            ex.InnerException is PostgresException { SqlState: "23505" };
    228243
    229244        private static EnrollSemesterResultDto Fail(string message) =>
Note: See TracChangeset for help on using the changeset viewer.