{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Transakcije v MariaDB/MySQL (transakcija B)\n",
"\n",
"### Pomožni elementi"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# pyODBC\n",
"import pyodbc\n",
"try:\n",
" cn2.close()\n",
"except:\n",
" pass\n",
"\n",
"# MariaDB/MySQL\n",
"conn = \"DRIVER={MySQL ODBC 5.3 Unicode Driver};SERVER=localhost;DATABASE=sandbox;UID=tup;PWD=tupvaje\"\n",
"cn2 = pyodbc.connect(conn, autocommit=False)\n",
"c2=cn2.cursor()"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Izpis rezultatov poizvedbe (pomožna funkcija)\n",
"def tabela(rez):\n",
" try:\n",
" # Glava\n",
" for g in rez.description:\n",
" print(g[0],end=\"\\t\")\n",
" print(\"\\n\"+\"-\"*31)\n",
" # Vsebina\n",
" for r in rez.fetchall():\n",
" for a in r:\n",
" print(a,end=\"\\t\")\n",
" print() \n",
" # Število vrstic\n",
" print(\"Vseh vrstic je\", rez.rowcount)\n",
" except Exception(e):\n",
" pass"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Nadaljuj s korakom A1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Korak B1: nastavi transakcijske parametre za vse nadaljnje nove transakcije"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Timeout ob predolgem zaklepanju\n",
"c2.execute(\"SET SESSION innodb_lock_wait_timeout = 5\") # Čas v sekundah\n",
"# Preizkusite različne stopnje izolacije\n",
"c2.execute(\"SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED\") \n",
"# Začnemo novo transakcijo\n",
"c2.commit()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"----\n",
"# Branje neobstoječega podatka (dirty read)\n",
"- stopnja izolacije transakcije B mora biti vsaj `READ COMMITTED`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Korak B2: izpis vsebine tabele pred spremembo"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"c2.execute(\"SELECT * FROM jadralec\")\n",
"tabela(c2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Nadaljuj s korakom A4"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Korak B3: izpis vsebine tabele po spremembi v prvi povezavi"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"tabela(c2.execute(\"SELECT * FROM jadralec\"))"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"## Korak B3: izpis vsebine tabele po razveljavitvi spremembe v prvi povezavi"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"tabela(c2.execute(\"SELECT * FROM jadralec\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Pri `READ UNCOMMITED` so vidne tudi nepotrjene spremembe, pri višjih stopnjah izolacije pa ne.\n",
"\n",
"-------------------"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Izgubljeno ažuriranje"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Korak B4: začetek nove transakcije in izpis ratinga"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n"
]
}
],
"source": [
"c2.rollback()\n",
"c2.execute(\"\"\" SELECT rating \n",
" FROM jadralec\n",
" WHERE jid = 29\"\"\")\n",
"rating = c2.fetchone()[0]\n",
"print (rating)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Nadaljuj s korakom A8"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Korak B5: sprememba ratinga"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"ename": "Error",
"evalue": "('HY000', '[HY000] [MySQL][ODBC 5.3(w) Driver][mysqld-5.5.5-10.1.14-MariaDB]Lock wait timeout exceeded; try restarting transaction (1205) (SQLExecDirectW)')",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mc2\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexecute\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"UPDATE jadralec SET rating = ? WHERE jid = 29\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mrating\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;36m10\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mError\u001b[0m: ('HY000', '[HY000] [MySQL][ODBC 5.3(w) Driver][mysqld-5.5.5-10.1.14-MariaDB]Lock wait timeout exceeded; try restarting transaction (1205) (SQLExecDirectW)')"
]
}
],
"source": [
"c2.execute(\"UPDATE jadralec \\\n",
" SET rating = ? \\\n",
" WHERE jid = 29\", rating + 10)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Ne glede na stopnjo izolacije dobimo obvestilo:\n",
" `Lock wait timeout exceeded; try restarting transaction (1205) `\n",
"\n",
"(razen če namesto privzetega InnoDB/XtraDB uporabimo netransakcijski shranjevalni pogon MyISAM/ARIA)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Nadaljujemo lahko le z MyISAM"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"## Korak B6: potrditev sprememb in izpis"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"c2.commit()\n",
"tabela(c2.execute(\"SELECT * FROM jadralec\"))"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"---------\n",
"# Neponovljivo branje (`non-repeatable read`)\n",
"## Potrebujemo najmanj `REPEATABLE READ` v transakciji A"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Korak B7: začetek transakcije, sprememba in potrditev"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"c2.commit()\n",
"c2.execute(\"UPDATE jadralec \\\n",
" SET rating = ? \\\n",
" WHERE jid = 29\", 666)\n",
"c2.commit()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Nadaljuj s korakom A11"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"---------\n",
"# Fantomsko branje (`phantom read`)\n",
"## Potrebujemo `SERIALIZABLE` v transakciji A"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Korak B8: začetek transakcije, sprememba in potrditev"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"c2.commit()\n",
"c2.execute(\"INSERT INTO jadralec VALUES(25,'PHANTOM',42, 666)\")\n",
"c2.commit()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Pri `SERIALIZABLE` pride do time-outa, sicer do fantomske vrstice"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Nadaljuj s korakom A13"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"c2.commit()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"----\n",
"# Mrtva zanka"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Korak B9: prva sprememba in zaklepanje"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"c2.commit()\n",
"c2.execute(\"UPDATE jadralec \\\n",
" SET rating = ? \\\n",
" WHERE jid = 29\", 229)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Nadaljuj s korakom A15\n",
"## Korak B10: druga sprememba in zaklepanje"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"c2.execute(\"UPDATE jadralec \\\n",
" SET rating = ? \\\n",
" WHERE jid = 22\", 222)\n",
"c2.commit()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Pride do pojava mrtve zanke, zato se transakcija prekine in razveljavi.\n",
"## Nadaljuj s korakom A16.\n",
"### Za ponovitev ponovno izvršimo koraka B9 in B10."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [default]",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}