Index: uc_reserve.py
===================================================================
--- uc_reserve.py	(revision 95cf703d2ef8f7abf8a3684935be560cdf73467f)
+++ uc_reserve.py	(revision 527c0976508c7d2fad14e9180bcf1a211d5f18af)
@@ -1,5 +1,5 @@
 import datetime
 
-from db import get_connection, pick_from_list, print_table, input_date, input_time
+from db import get_connection, get_transaction, pick_from_list, print_table, input_date, input_time
 
 
@@ -96,19 +96,34 @@
             return
 
-        # Step 7: INSERT
-        conn = get_connection()
+        # Step 7: INSERT (transaction: re-check conflicts + insert atomically)
         try:
-            with conn.cursor() as cur:
-                cur.execute(
-                    """
-                    INSERT INTO reservations
-                        (start_time, end_time, status, purpose, created_at, user_id, resource_id)
-                    VALUES (%s, %s, 'pending', %s, CURRENT_TIMESTAMP, %s, %s)
-                    RETURNING reservation_id, status, created_at
-                    """,
-                    (start_dt, end_dt, purpose, user_id, rid),
-                )
-                result = cur.fetchone()
-            conn.commit()
+            with get_transaction() as conn:
+                with conn.cursor() as cur:
+                    # Re-check conflicts inside the transaction to prevent
+                    # race conditions (another user inserting between our
+                    # check and insert)
+                    cur.execute(
+                        """
+                        SELECT COUNT(*) FROM reservations
+                        WHERE resource_id = %s
+                          AND status IN ('approved', 'pending')
+                          AND start_time < %s AND end_time > %s
+                        """,
+                        (rid, end_dt, start_dt),
+                    )
+                    if cur.fetchone()[0] > 0:
+                        raise RuntimeError("Conflict: another reservation was just created for this time slot")
+
+                    cur.execute(
+                        """
+                        INSERT INTO reservations
+                            (start_time, end_time, status, purpose, created_at, user_id, resource_id)
+                        VALUES (%s, %s, 'pending', %s, CURRENT_TIMESTAMP, %s, %s)
+                        RETURNING reservation_id, status, created_at
+                        """,
+                        (start_dt, end_dt, purpose, user_id, rid),
+                    )
+                    result = cur.fetchone()
+            # Transaction committed automatically
             print(f"\n  Reservation created successfully!")
             print(f"  Reservation ID: {result[0]}")
@@ -117,9 +132,7 @@
             return
         except Exception as e:
-            conn.rollback()
+            # Transaction rolled back automatically
             print(f"\n  Error creating reservation: {e}")
             return
-        finally:
-            conn.close()
 
 
