transaction
This commit is contained in:
parent
a2555db6d7
commit
0529e678f1
@ -32,7 +32,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -42,24 +42,24 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Datenbankverbindung herstellen\n",
|
||||
"def get_connection():\n",
|
||||
" # TODO Datenbankverbindung herstellen - geben Sie hier Ihre Zugangsdaten ein\n",
|
||||
" conn = psycopg2.connect(\"dbname=... user=... password=...\")\n",
|
||||
" conn = psycopg2.connect(\"dbname=transaction_tutorial user=postgres password=postgres\")\n",
|
||||
" \n",
|
||||
" # TODO später autocommit ausschalten\n",
|
||||
" conn.autocommit = True\n",
|
||||
" #conn.autocommit = True\n",
|
||||
"\n",
|
||||
" return conn"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -90,37 +90,63 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"(1, 4)\n",
|
||||
"(2, 4)\n",
|
||||
"(3, 5)\n",
|
||||
"(4, 5)\n",
|
||||
"(5, 4)\n",
|
||||
"(6, 10)\n",
|
||||
"(7, 5)\n",
|
||||
"(8, 3)\n",
|
||||
"freie Plätze 40\n",
|
||||
"belegte Plätze 0\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Hilfsfunktion: Alle Seminar-Sessions ausgeben\n",
|
||||
"def show_sessions():\n",
|
||||
" # TODO alle Seminar-Sessions auf der Console ausgeben\n",
|
||||
" pass\n",
|
||||
" with conn.cursor() as cursor:\n",
|
||||
" cursor.execute(\"SELECT * FROM seminar_sessions\")\n",
|
||||
" rows = cursor.fetchall()\n",
|
||||
" \n",
|
||||
" for row in rows:\n",
|
||||
" print(row)\n",
|
||||
"\n",
|
||||
"# Anzahl aller freien Plätze bestimmen\n",
|
||||
"def count_free_seats():\n",
|
||||
" # TODO Anzahl freier Plätze bestimmen (Summe über die Spalte \"seats\")\n",
|
||||
" # und mit return zurückgeben\n",
|
||||
" pass\n",
|
||||
" with conn.cursor() as cursor: \n",
|
||||
" cursor.execute(\"SELECT SUM(seats) FROM seminar_sessions\")\n",
|
||||
" return cursor.fetchone()[0]\n",
|
||||
"\n",
|
||||
"# Anzahl der belegten Plätze bestimmen\n",
|
||||
"def count_occupied_seats():\n",
|
||||
" # TODO Anzahl belegter Plätze bestimmen (Anzahl der Einträge in der Tabelle seminar_registrations)\n",
|
||||
" # und mit return zurückgeben\n",
|
||||
" pass\n",
|
||||
" with conn.cursor() as cursor:\n",
|
||||
" cursor.execute(\"SELECT COUNT(user_id) FROM seminar_registrations\")\n",
|
||||
" return cursor.fetchone()[0]\n",
|
||||
"\n",
|
||||
"# Testaufrufe\n",
|
||||
"\n",
|
||||
"# show_sessions()\n",
|
||||
"# print(\"freie Plätze\", count_free_seats())\n",
|
||||
"# print(\"belegte Plätze\", count_occupied_seats())"
|
||||
"show_sessions()\n",
|
||||
"print(\"freie Plätze\", count_free_seats())\n",
|
||||
"print(\"belegte Plätze\", count_occupied_seats())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -131,21 +157,23 @@
|
||||
"\n",
|
||||
"def reserve_seat(conn, user_id, session_id):\n",
|
||||
" with conn.cursor() as cursor:\n",
|
||||
" cursor.execute(\"SELECT seats FROM seminar_sessions WHERE id = %s\", (session_id,))\n",
|
||||
" cursor.execute(\"SELECT seats FROM seminar_sessions WHERE id = %s FOR UPDATE\", (session_id,))\n",
|
||||
" seats = cursor.fetchone()[0]\n",
|
||||
"\n",
|
||||
" if seats > 0:\n",
|
||||
" cursor.execute(\"UPDATE seminar_sessions SET seats = %s WHERE id = %s\", (seats-1, session_id,))\n",
|
||||
" cursor.execute(\"INSERT INTO seminar_registrations (user_id, session_id) VALUES (%s, %s)\", (user_id, session_id))\n",
|
||||
" conn.commit()\n",
|
||||
" return True\n",
|
||||
" else:\n",
|
||||
" conn.rollback()\n",
|
||||
" return False\n",
|
||||
" \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 13,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -169,9 +197,130 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 14,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Vor dem Lasttest:\n",
|
||||
"--------------------\n",
|
||||
"Freie Plätze: 10\n",
|
||||
"Belegte Plätze: 30\n",
|
||||
"Gesamt: 40\n",
|
||||
"Platzanzahl ist konsistent!\n",
|
||||
"--------------------\n",
|
||||
"Session 1 ist ausgebucht\n",
|
||||
"Session 2 ist ausgebucht\n",
|
||||
"Session 4 ist ausgebucht\n",
|
||||
"User 50 hat sich für Session 3 angemeldet\n",
|
||||
"User 74 hat sich für Session 5 angemeldet\n",
|
||||
"User 88 hat sich für Session 7 angemeldet\n",
|
||||
"Session 2 ist ausgebucht\n",
|
||||
"Session 5 ist ausgebucht\n",
|
||||
"Session 7 ist ausgebucht\n",
|
||||
"Session 2 ist ausgebucht\n",
|
||||
"Session 2 ist ausgebucht\n",
|
||||
"User 49 hat sich für Session 6 angemeldet\n",
|
||||
"User 95 hat sich für Session 3 angemeldet\n",
|
||||
"Session 5 ist ausgebucht\n",
|
||||
"Session 4 ist ausgebucht\n",
|
||||
"Session 2 ist ausgebucht\n",
|
||||
"Session 4 ist ausgebucht\n",
|
||||
"Session 2 ist ausgebucht\n",
|
||||
"Session 4 ist ausgebucht\n",
|
||||
"User 97 hat sich für Session 6 angemeldet\n",
|
||||
"Session 2 ist ausgebucht\n",
|
||||
"Session 3 ist ausgebucht\n",
|
||||
"Session 8 ist ausgebucht\n",
|
||||
"Session 8 ist ausgebucht\n",
|
||||
"Session 8 ist ausgebucht\n",
|
||||
"Session 1 ist ausgebucht\n",
|
||||
"Session 1 ist ausgebucht\n",
|
||||
"Session 2 ist ausgebucht\n",
|
||||
"Session 1 ist ausgebucht\n",
|
||||
"Session 2 ist ausgebucht\n",
|
||||
"Session 1 ist ausgebucht\n",
|
||||
"Session 5 ist ausgebucht\n",
|
||||
"Session 1 ist ausgebucht\n",
|
||||
"Session 8 ist ausgebucht\n",
|
||||
"Session 4 ist ausgebucht\n",
|
||||
"User 86 hat sich für Session 6 angemeldet\n",
|
||||
"Session 8 ist ausgebucht\n",
|
||||
"Session 8 ist ausgebucht\n",
|
||||
"Session 2 ist ausgebucht\n",
|
||||
"Session 4 ist ausgebucht\n",
|
||||
"Session 2 ist ausgebucht\n",
|
||||
"Session 1 ist ausgebucht\n",
|
||||
"Session 5 ist ausgebucht\n",
|
||||
"Session 4 ist ausgebucht\n",
|
||||
"Session 4 ist ausgebucht\n",
|
||||
"Session 5 ist ausgebucht\n",
|
||||
"Session 5 ist ausgebucht\n",
|
||||
"Session 7 ist ausgebucht\n",
|
||||
"User 63 hat sich für Session 6 angemeldet\n",
|
||||
"Session 1 ist ausgebucht\n",
|
||||
"Session 7 ist ausgebucht\n",
|
||||
"Session 1 ist ausgebucht\n",
|
||||
"Session 3 ist ausgebucht\n",
|
||||
"Session 3 ist ausgebucht\n",
|
||||
"Session 1 ist ausgebucht\n",
|
||||
"Session 7 ist ausgebucht\n",
|
||||
"Session 1 ist ausgebucht\n",
|
||||
"Session 5 ist ausgebucht\n",
|
||||
"Session 8 ist ausgebucht\n",
|
||||
"Session 1 ist ausgebucht\n",
|
||||
"User 81 hat sich für Session 6 angemeldet\n",
|
||||
"Session 8 ist ausgebucht\n",
|
||||
"Session 8 ist ausgebucht\n",
|
||||
"User 71 hat sich für Session 6 angemeldet\n",
|
||||
"Session 7 ist ausgebucht\n",
|
||||
"Session 6 ist ausgebucht\n",
|
||||
"Session 6 ist ausgebucht\n",
|
||||
"Session 3 ist ausgebucht\n",
|
||||
"Session 6 ist ausgebucht\n",
|
||||
"Session 7 ist ausgebucht\n",
|
||||
"Session 4 ist ausgebucht\n",
|
||||
"Session 6 ist ausgebucht\n",
|
||||
"Session 6 ist ausgebucht\n",
|
||||
"Session 8 ist ausgebucht\n",
|
||||
"Session 8 ist ausgebucht\n",
|
||||
"Session 2 ist ausgebucht\n",
|
||||
"Session 5 ist ausgebucht\n",
|
||||
"Session 6 ist ausgebucht\n",
|
||||
"Session 6 ist ausgebucht\n",
|
||||
"Session 2 ist ausgebucht\n",
|
||||
"Session 2 ist ausgebucht\n",
|
||||
"Session 8 ist ausgebucht\n",
|
||||
"Session 2 ist ausgebucht\n",
|
||||
"Session 7 ist ausgebucht\n",
|
||||
"Session 3 ist ausgebucht\n",
|
||||
"Session 7 ist ausgebucht\n",
|
||||
"Session 1 ist ausgebucht\n",
|
||||
"Session 3 ist ausgebucht\n",
|
||||
"Session 7 ist ausgebucht\n",
|
||||
"Session 5 ist ausgebucht\n",
|
||||
"Session 8 ist ausgebucht\n",
|
||||
"Session 2 ist ausgebucht\n",
|
||||
"Session 4 ist ausgebucht\n",
|
||||
"Session 8 ist ausgebucht\n",
|
||||
"Session 4 ist ausgebucht\n",
|
||||
"Session 6 ist ausgebucht\n",
|
||||
"Session 3 ist ausgebucht\n",
|
||||
"Session 7 ist ausgebucht\n",
|
||||
"Session 6 ist ausgebucht\n",
|
||||
"Session 4 ist ausgebucht\n",
|
||||
"Nach dem Lasttest:\n",
|
||||
"--------------------\n",
|
||||
"Freie Plätze: 0\n",
|
||||
"Belegte Plätze: 40\n",
|
||||
"Gesamt: 40\n",
|
||||
"Platzanzahl ist konsistent!\n",
|
||||
"--------------------\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import threading\n",
|
||||
"\n",
|
||||
@ -214,13 +363,20 @@
|
||||
"print(\"Nach dem Lasttest:\")\n",
|
||||
"print_info()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "base",
|
||||
"display_name": "Python [conda env:base] *",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
"name": "conda-base-py"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
@ -232,9 +388,9 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.11.7"
|
||||
"version": "3.13.9"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
"nbformat_minor": 4
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user