// // Drop(): // // If possible, function drops the student from the course. // // Return values: // -1 - error // 0 - dropped, no waitlist // 1 - dropped, enrolled student from waitlist // 2 - not enrolled, dropped from waitlist // 3 - student was not either enrolled or waitlisted // private int Drop(int sid, int cid) { int retries = 0; // make sure parameters are valid if (sid < 0 || cid < 0) { return(-1); } using (var db = new CoursemoDataContext()) { while (retries < 3) { try { var txOptions = new TransactionOptions(); txOptions.IsolationLevel = System.Transactions.IsolationLevel.Serializable; using (var transaction = new TransactionScope(TransactionScopeOption.Required, txOptions)) { // student enrolled, drop course and enroll first student on waitlist if (StudentEnrolled(sid, cid)) { db.UnregisterStudent(sid, cid); // candidate on waitlist exists int sidFromWaitList = GetFirstStudentFromWaitlist(cid); System.Threading.Thread.Sleep(_delay); if (sidFromWaitList != 0) { db.RegisterStudent(sidFromWaitList, cid); db.UnwaitlistStudent(sidFromWaitList, cid); db.SubmitChanges(); transaction.Complete(); return(1); } db.SubmitChanges(); transaction.Complete(); return(0); } // student not enrolled, but waitlisted, remove from waitlist else if (StudentWaitlisted(sid, cid)) { db.UnwaitlistStudent(sid, cid); db.SubmitChanges(); transaction.Complete(); return(2); } else { return(3); } } } catch (SqlException exc) { // deadlock if (exc.Number == 1205) { retries++; } } catch (Exception e) { MessageBox.Show("Drop(): " + e.Message); return(-1); } } // while } return(-1); }
private void InitializeDataStructures() { db = new CoursemoDataContext(); _students = new List <Student>(); _courses = new List <Course>(); }
// // Enroll(): // // If possible, function enrolls the student in the course. // // Return values: // -1 - error // 0 - Student already enrolled // 1 - Student enrolled // 2 - Student already waitlisted // 3 - Student waitlisted // private int Enroll(int sid, int cid) { int retries = 0; // make sure parameters are valid if (sid < 0 || cid < 0) { return(-1); } using (var db = new CoursemoDataContext()) { while (retries < 3) { try { var txOptions = new TransactionOptions(); txOptions.IsolationLevel = System.Transactions.IsolationLevel.Serializable; using (var transaction = new TransactionScope(TransactionScopeOption.Required, txOptions)) { // check if student is already enrolled if (StudentEnrolled(sid, cid)) { return(0); } // class is full, add to waitlist if (GetCourseCapacity(cid) - GetCurrentEnrollment(cid) < 1) { // check if student is not on waitlist alread if (StudentWaitlisted(sid, cid)) { return(2); } db.WaitlistStudent(sid, cid); db.SubmitChanges(); transaction.Complete(); return(3); } // enroll else { System.Threading.Thread.Sleep(_delay); db.RegisterStudent(sid, cid); db.SubmitChanges(); transaction.Complete(); return(1); } } } catch (SqlException exc) { // deadlock if (exc.Number == 1205) { retries++; } } catch (Exception e) { MessageBox.Show("Enroll(): " + e.Message); return(-1); } } // while } return(-1); } // Enroll()