| 1 |
<!DOCTYPE html> |
| 2 |
<html><head> |
| 3 |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 4 |
<meta http-equiv="content-type" content="text/html; charset=UTF-8"> |
| 5 |
<link href="sqlite.css" rel="stylesheet"> |
| 6 |
<title>Transaction</title> |
| 7 |
<!-- path= --> |
| 8 |
</head> |
| 9 |
<body> |
| 10 |
<div class=nosearch> |
| 11 |
<a href="index.html"> |
| 12 |
<img class="logo" src="images/sqlite370_banner.gif" alt="SQLite" border="0"> |
| 13 |
</a> |
| 14 |
<div><!-- IE hack to prevent disappearing logo --></div> |
| 15 |
<div class="tagline desktoponly"> |
| 16 |
Small. Fast. Reliable.<br>Choose any three. |
| 17 |
</div> |
| 18 |
<div class="menu mainmenu"> |
| 19 |
<ul> |
| 20 |
<li><a href="index.html">Home</a> |
| 21 |
<li class='mobileonly'><a href="javascript:void(0)" onclick='toggle_div("submenu")'>Menu</a> |
| 22 |
<li class='wideonly'><a href='about.html'>About</a> |
| 23 |
<li class='desktoponly'><a href="docs.html">Documentation</a> |
| 24 |
<li class='desktoponly'><a href="download.html">Download</a> |
| 25 |
<li class='wideonly'><a href='copyright.html'>License</a> |
| 26 |
<li class='desktoponly'><a href="support.html">Support</a> |
| 27 |
<li class='desktoponly'><a href="prosupport.html">Purchase</a> |
| 28 |
<li class='search' id='search_menubutton'> |
| 29 |
<a href="javascript:void(0)" onclick='toggle_search()'>Search</a> |
| 30 |
</ul> |
| 31 |
</div> |
| 32 |
<div class="menu submenu" id="submenu"> |
| 33 |
<ul> |
| 34 |
<li><a href='about.html'>About</a> |
| 35 |
<li><a href='docs.html'>Documentation</a> |
| 36 |
<li><a href='download.html'>Download</a> |
| 37 |
<li><a href='support.html'>Support</a> |
| 38 |
<li><a href='prosupport.html'>Purchase</a> |
| 39 |
</ul> |
| 40 |
</div> |
| 41 |
<div class="searchmenu" id="searchmenu"> |
| 42 |
<form method="GET" action="search"> |
| 43 |
<select name="s" id="searchtype"> |
| 44 |
<option value="d">Search Documentation</option> |
| 45 |
<option value="c">Search Changelog</option> |
| 46 |
</select> |
| 47 |
<input type="text" name="q" id="searchbox" value=""> |
| 48 |
<input type="submit" value="Go"> |
| 49 |
</form> |
| 50 |
</div> |
| 51 |
</div> |
| 52 |
<script> |
| 53 |
function toggle_div(nm) { |
| 54 |
var w = document.getElementById(nm); |
| 55 |
if( w.style.display=="block" ){ |
| 56 |
w.style.display = "none"; |
| 57 |
}else{ |
| 58 |
w.style.display = "block"; |
| 59 |
} |
| 60 |
} |
| 61 |
function toggle_search() { |
| 62 |
var w = document.getElementById("searchmenu"); |
| 63 |
if( w.style.display=="block" ){ |
| 64 |
w.style.display = "none"; |
| 65 |
} else { |
| 66 |
w.style.display = "block"; |
| 67 |
setTimeout(function(){ |
| 68 |
document.getElementById("searchbox").focus() |
| 69 |
}, 30); |
| 70 |
} |
| 71 |
} |
| 72 |
function div_off(nm){document.getElementById(nm).style.display="none";} |
| 73 |
window.onbeforeunload = function(e){div_off("submenu");} |
| 74 |
/* Disable the Search feature if we are not operating from CGI, since */ |
| 75 |
/* Search is accomplished using CGI and will not work without it. */ |
| 76 |
if( !location.origin || !location.origin.match || !location.origin.match(/http/) ){ |
| 77 |
document.getElementById("search_menubutton").style.display = "none"; |
| 78 |
} |
| 79 |
/* Used by the Hide/Show button beside syntax diagrams, to toggle the */ |
| 80 |
function hideorshow(btn,obj){ |
| 81 |
var x = document.getElementById(obj); |
| 82 |
var b = document.getElementById(btn); |
| 83 |
if( x.style.display!='none' ){ |
| 84 |
x.style.display = 'none'; |
| 85 |
b.innerHTML='show'; |
| 86 |
}else{ |
| 87 |
x.style.display = ''; |
| 88 |
b.innerHTML='hide'; |
| 89 |
} |
| 90 |
return false; |
| 91 |
} |
| 92 |
var antiRobot = 0; |
| 93 |
function antiRobotGo(){ |
| 94 |
if( antiRobot!=3 ) return; |
| 95 |
antiRobot = 7; |
| 96 |
var j = document.getElementById("mtimelink"); |
| 97 |
if(j && j.hasAttribute("data-href")) j.href=j.getAttribute("data-href"); |
| 98 |
} |
| 99 |
function antiRobotDefense(){ |
| 100 |
document.body.onmousedown=function(){ |
| 101 |
antiRobot |= 2; |
| 102 |
antiRobotGo(); |
| 103 |
document.body.onmousedown=null; |
| 104 |
} |
| 105 |
document.body.onmousemove=function(){ |
| 106 |
antiRobot |= 2; |
| 107 |
antiRobotGo(); |
| 108 |
document.body.onmousemove=null; |
| 109 |
} |
| 110 |
setTimeout(function(){ |
| 111 |
antiRobot |= 1; |
| 112 |
antiRobotGo(); |
| 113 |
}, 100) |
| 114 |
antiRobotGo(); |
| 115 |
} |
| 116 |
antiRobotDefense(); |
| 117 |
</script> |
| 118 |
<div class=fancy> |
| 119 |
<div class=nosearch> |
| 120 |
<div class="fancy_title"> |
| 121 |
Transaction |
| 122 |
</div> |
| 123 |
</div> |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
<h1 id="transaction_control_syntax"><span>1. </span>Transaction Control Syntax</h1> |
| 129 |
|
| 130 |
<p><b><a href="syntax/begin-stmt.html">begin-stmt:</a></b> |
| 131 |
<button id='xb1701bb4' onclick='hideorshow("xb1701bb4","x4999ba07")'>hide</button></p> |
| 132 |
<div id='x4999ba07' class='imgcontainer'> |
| 133 |
<div style="max-width:560px"><svg xmlns='http://www.w3.org/2000/svg' class="pikchr" viewBox="0 0 560.669 140.4"> |
| 134 |
<circle cx="5" cy="17" r="3.6" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 135 |
<polygon points="32,17 20,21 20,12" style="fill:rgb(0,0,0)"/> |
| 136 |
<path d="M9,17L26,17" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 137 |
<path d="M47,32L86,32A15 15 0 0 0 101 17A15 15 0 0 0 86 2L47,2A15 15 0 0 0 32 17A15 15 0 0 0 47 32Z" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 138 |
<text x="66" y="17" text-anchor="middle" fill="rgb(0,0,0)" dominant-baseline="central">BEGIN</text> |
| 139 |
<polygon points="124,17 112,21 112,12" style="fill:rgb(0,0,0)"/> |
| 140 |
<path d="M101,17L118,17" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 141 |
<polygon points="162,123 150,127 150,118" style="fill:rgb(0,0,0)"/> |
| 142 |
<path d="M124,17 L 131,17 Q 139,17 139,32 L 139,108 Q 139,123 147,123 L 156,123" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 143 |
<path d="M177,138L261,138A15 15 0 0 0 276 123A15 15 0 0 0 261 108L177,108A15 15 0 0 0 162 123A15 15 0 0 0 177 138Z" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 144 |
<text x="219" y="123" text-anchor="middle" fill="rgb(0,0,0)" dominant-baseline="central">EXCLUSIVE</text> |
| 145 |
<polygon points="299,123 287,127 287,118" style="fill:rgb(0,0,0)"/> |
| 146 |
<path d="M276,123L293,123" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 147 |
<polygon points="337,17 325,21 325,12" style="fill:rgb(0,0,0)"/> |
| 148 |
<path d="M299,123 L 306,123 Q 314,123 314,108 L 314,32 Q 314,17 322,17 L 331,17" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 149 |
<polygon points="373,17 361,21 361,12" style="fill:rgb(0,0,0)"/> |
| 150 |
<path d="M337,17L367,17" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 151 |
<path d="M388,32L500,32A15 15 0 0 0 515 17A15 15 0 0 0 500 2L388,2A15 15 0 0 0 373 17A15 15 0 0 0 388 32Z" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 152 |
<text x="444" y="17" text-anchor="middle" fill="rgb(0,0,0)" dominant-baseline="central">TRANSACTION</text> |
| 153 |
<polygon points="551,17 539,21 539,12" style="fill:rgb(0,0,0)"/> |
| 154 |
<path d="M515,17L545,17" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 155 |
<circle cx="554" cy="17" r="3.6" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 156 |
<polygon points="444,47 432,51 432,43" style="fill:rgb(0,0,0)"/> |
| 157 |
<path d="M337,17 L 344,17 Q 352,17 352,32 L 352,32 Q 352,47 367,47 L 423,47 L 438,47" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 158 |
<path d="M444,47 L 511,47 Q 526,47 526,32 L 526,32 Q 526,17 533,17 L 541,17" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 159 |
<path d="M124,17L325,17" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 160 |
<polygon points="162,47 150,51 150,43" style="fill:rgb(0,0,0)"/> |
| 161 |
<path d="M139,32 L 139,39 Q 139,47 147,47 L 156,47" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 162 |
<path d="M177,62L257,62A15 15 0 0 0 272 47L272,47A15 15 0 0 0 257 32L177,32A15 15 0 0 0 162 47L162,47A15 15 0 0 0 177 62Z" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 163 |
<text x="217" y="47" text-anchor="middle" fill="rgb(0,0,0)" dominant-baseline="central">DEFERRED</text> |
| 164 |
<polygon points="295,47 284,51 284,43" style="fill:rgb(0,0,0)"/> |
| 165 |
<path d="M272,47L289,47" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 166 |
<path d="M295,47 L 304,47 Q 314,47 314,40 L 314,32" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 167 |
<polygon points="162,85 150,89 150,81" style="fill:rgb(0,0,0)"/> |
| 168 |
<path d="M139,32 L 139,70 Q 139,85 147,85 L 156,85" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 169 |
<path d="M177,100L265,100A15 15 0 0 0 281 85A15 15 0 0 0 265 70L177,70A15 15 0 0 0 162 85A15 15 0 0 0 177 100Z" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 170 |
<text x="221" y="85" text-anchor="middle" fill="rgb(0,0,0)" dominant-baseline="central">IMMEDIATE</text> |
| 171 |
<polygon points="304,85 292,89 292,81" style="fill:rgb(0,0,0)"/> |
| 172 |
<path d="M281,85L298,85" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 173 |
<path d="M304,85 L 309,85 Q 314,85 314,77 L 314,70" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 174 |
</svg> |
| 175 |
</div> |
| 176 |
</div> |
| 177 |
<p><b><a href="syntax/commit-stmt.html">commit-stmt:</a></b> |
| 178 |
<button id='x4ef3720b' onclick='hideorshow("x4ef3720b","x9d6c8324")'>hide</button></p> |
| 179 |
<div id='x9d6c8324' class='imgcontainer'> |
| 180 |
<div style="max-width:434px"><svg xmlns='http://www.w3.org/2000/svg' class="pikchr" viewBox="0 0 434.506 72.36"> |
| 181 |
<circle cx="5" cy="17" r="3.6" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 182 |
<polygon points="32,17 20,21 20,12" style="fill:rgb(0,0,0)"/> |
| 183 |
<path d="M9,17L26,17" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 184 |
<polygon points="68,17 56,21 56,12" style="fill:rgb(0,0,0)"/> |
| 185 |
<path d="M32,17L62,17" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 186 |
<path d="M83,32L141,32A15 15 0 0 0 157 17A15 15 0 0 0 141 2L83,2A15 15 0 0 0 68 17A15 15 0 0 0 83 32Z" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 187 |
<text x="112" y="17" text-anchor="middle" fill="rgb(0,0,0)" dominant-baseline="central">COMMIT</text> |
| 188 |
<polygon points="202,17 190,21 190,12" style="fill:rgb(0,0,0)"/> |
| 189 |
<path d="M157,17L196,17" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 190 |
<path d="M202,17L238,17" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 191 |
<path d="M253,32L365,32A15 15 0 0 0 380 17A15 15 0 0 0 365 2L253,2A15 15 0 0 0 238 17A15 15 0 0 0 253 32Z" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 192 |
<text x="309" y="17" text-anchor="middle" fill="rgb(0,0,0)" dominant-baseline="central">TRANSACTION</text> |
| 193 |
<polygon points="425,17 413,21 413,12" style="fill:rgb(0,0,0)"/> |
| 194 |
<path d="M380,17L419,17" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 195 |
<circle cx="428" cy="17" r="3.6" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 196 |
<path d="M83,70L104,70A15 15 0 0 0 119 55L119,55A15 15 0 0 0 104 39L83,39A15 15 0 0 0 68 55L68,55A15 15 0 0 0 83 70Z" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 197 |
<text x="94" y="55" text-anchor="middle" fill="rgb(0,0,0)" dominant-baseline="central">END</text> |
| 198 |
<polygon points="68,55 56,59 56,50" style="fill:rgb(0,0,0)"/> |
| 199 |
<path d="M32,17 L 39,17 Q 47,17 47,32 L 47,40 Q 47,55 55,55 L 62,55" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 200 |
<polygon points="157,55 145,59 145,50" style="fill:rgb(0,0,0)"/> |
| 201 |
<path d="M119,55L151,55" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 202 |
<path d="M157,55 L 164,55 Q 172,55 172,40 L 172,32 Q 172,17 179,17 L 187,17" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 203 |
<polygon points="309,47 297,51 297,43" style="fill:rgb(0,0,0)"/> |
| 204 |
<path d="M202,17 L 209,17 Q 217,17 217,32 L 217,32 Q 217,47 232,47 L 288,47 L 303,47" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 205 |
<path d="M309,47 L 380,47 Q 395,47 395,32 L 395,32 Q 395,17 402,17 L 410,17" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 206 |
</svg> |
| 207 |
</div> |
| 208 |
</div> |
| 209 |
<p><b><a href="syntax/rollback-stmt.html">rollback-stmt:</a></b> |
| 210 |
<button id='x82e26188' onclick='hideorshow("x82e26188","xbfddd6c6")'>hide</button></p> |
| 211 |
<div id='xbfddd6c6' class='imgcontainer'> |
| 212 |
<div style="max-width:801px"><svg xmlns='http://www.w3.org/2000/svg' class="pikchr" viewBox="0 0 801.734 67.392"> |
| 213 |
<circle cx="5" cy="33" r="3.6" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 214 |
<polygon points="32,33 20,38 20,29" style="fill:rgb(0,0,0)"/> |
| 215 |
<path d="M9,33L26,33" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 216 |
<path d="M47,48L125,48A15 15 0 0 0 140 33A15 15 0 0 0 125 18L47,18A15 15 0 0 0 32 33A15 15 0 0 0 47 48Z" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 217 |
<text x="86" y="33" text-anchor="middle" fill="rgb(0,0,0)" dominant-baseline="central">ROLLBACK</text> |
| 218 |
<polygon points="176,33 164,38 164,29" style="fill:rgb(0,0,0)"/> |
| 219 |
<path d="M140,33L170,33" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 220 |
<path d="M191,48L303,48A15 15 0 0 0 318 33A15 15 0 0 0 303 18L191,18A15 15 0 0 0 176 33A15 15 0 0 0 191 48Z" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 221 |
<text x="247" y="33" text-anchor="middle" fill="rgb(0,0,0)" dominant-baseline="central">TRANSACTION</text> |
| 222 |
<polygon points="390,33 378,38 378,29" style="fill:rgb(0,0,0)"/> |
| 223 |
<path d="M318,33L384,33" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 224 |
<path d="M405,48L411,48A15 15 0 0 0 426 33A15 15 0 0 0 411 18L405,18A15 15 0 0 0 390 33A15 15 0 0 0 405 48Z" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 225 |
<text x="408" y="33" text-anchor="middle" fill="rgb(0,0,0)" dominant-baseline="central">TO</text> |
| 226 |
<polygon points="462,33 450,38 450,29" style="fill:rgb(0,0,0)"/> |
| 227 |
<path d="M426,33L456,33" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 228 |
<path d="M477,48L561,48A15 15 0 0 0 576 33A15 15 0 0 0 561 18L477,18A15 15 0 0 0 462 33A15 15 0 0 0 477 48Z" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 229 |
<text x="519" y="33" text-anchor="middle" fill="rgb(0,0,0)" dominant-baseline="central">SAVEPOINT</text> |
| 230 |
<polygon points="612,33 601,38 601,29" style="fill:rgb(0,0,0)"/> |
| 231 |
<path d="M576,33L606,33" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 232 |
<path d="M627,48L741,48A15 15 0 0 0 756 33A15 15 0 0 0 741 18L627,18A15 15 0 0 0 612 33A15 15 0 0 0 627 48Z" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 233 |
<text x="684" y="33" text-anchor="middle" fill="rgb(0,0,0)" dominant-baseline="central">savepoint-name</text> |
| 234 |
<polygon points="792,33 780,38 780,29" style="fill:rgb(0,0,0)"/> |
| 235 |
<path d="M756,33L786,33" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 236 |
<circle cx="795" cy="33" r="3.6" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 237 |
<polygon points="519,6 507,10 507,2" style="fill:rgb(0,0,0)"/> |
| 238 |
<path d="M426,33 L 433,33 Q 441,33 441,20 Q 441,6 456,6 L 498,6 L 513,6" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 239 |
<path d="M519,6 L 576,6 Q 591,6 591,20 Q 591,33 599,33 L 606,33" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 240 |
<polygon points="519,60 507,65 507,56" style="fill:rgb(0,0,0)"/> |
| 241 |
<path d="M352,33 L 359,33 Q 367,33 367,47 Q 367,60 382,60 L 498,60 L 513,60" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 242 |
<path d="M519,60 L 756,60 Q 771,60 771,47 Q 771,33 778,33 L 786,33" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 243 |
<polygon points="247,60 235,65 235,56" style="fill:rgb(0,0,0)"/> |
| 244 |
<path d="M140,33 L 147,33 Q 155,33 155,47 Q 155,60 170,60 L 226,60 L 241,60" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 245 |
<path d="M247,60 L 318,60 Q 333,60 333,47 Q 333,33 340,33 L 348,33" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 246 |
</svg> |
| 247 |
</div> |
| 248 |
</div> |
| 249 |
|
| 250 |
|
| 251 |
<h1 id="transactions"><span>2. </span>Transactions</h1> |
| 252 |
|
| 253 |
<p> |
| 254 |
No reads or writes occur except within a transaction. |
| 255 |
Any command that accesses the database (basically, any SQL command, |
| 256 |
except a few <a href="pragma.html#syntax">PRAGMA</a> statements) |
| 257 |
will automatically start a transaction if |
| 258 |
one is not already in effect. Automatically started transactions |
| 259 |
are committed when the last SQL statement finishes. |
| 260 |
</p> |
| 261 |
|
| 262 |
<p> |
| 263 |
Transactions can be started manually using the BEGIN |
| 264 |
command. Such transactions usually persist until the next |
| 265 |
COMMIT or ROLLBACK command. But a transaction will also |
| 266 |
ROLLBACK if the database is closed or if an error occurs |
| 267 |
and the ROLLBACK conflict resolution algorithm is specified. |
| 268 |
See the documentation on the <a href="lang_conflict.html">ON CONFLICT</a> |
| 269 |
clause for additional information about the ROLLBACK |
| 270 |
conflict resolution algorithm. |
| 271 |
</p> |
| 272 |
|
| 273 |
<p> |
| 274 |
END TRANSACTION is an alias for COMMIT. |
| 275 |
</p> |
| 276 |
|
| 277 |
<p> Transactions created using BEGIN...COMMIT do not nest. |
| 278 |
For nested transactions, use the <a href="lang_savepoint.html">SAVEPOINT</a> and <a href="lang_savepoint.html">RELEASE</a> commands. |
| 279 |
The "TO SAVEPOINT <span class='yyterm'>name</span>" clause of the ROLLBACK command shown |
| 280 |
in the syntax diagram above is only applicable to <a href="lang_savepoint.html">SAVEPOINT</a> |
| 281 |
transactions. An attempt to invoke the BEGIN command within |
| 282 |
a transaction will fail with an error, regardless of whether |
| 283 |
the transaction was started by <a href="lang_savepoint.html">SAVEPOINT</a> or a prior BEGIN. |
| 284 |
The COMMIT command and the ROLLBACK command without the TO clause |
| 285 |
work the same on <a href="lang_savepoint.html">SAVEPOINT</a> transactions as they do with transactions |
| 286 |
started by BEGIN.</p> |
| 287 |
|
| 288 |
<h2 id="read_transactions_versus_write_transactions"><span>2.1. </span>Read transactions versus write transactions</h2> |
| 289 |
|
| 290 |
<p>SQLite supports multiple simultaneous read transactions |
| 291 |
coming from separate database connections, possibly in separate |
| 292 |
threads or processes, but only one simultaneous write transaction. |
| 293 |
</p><p> |
| 294 |
|
| 295 |
</p><p>A read transaction is used for reading only. A write transaction |
| 296 |
allows both reading and writing. A read transaction is started |
| 297 |
by a SELECT statement, and a write transaction is started by |
| 298 |
statements like CREATE, DELETE, DROP, INSERT, or UPDATE (collectively |
| 299 |
"write statements"). If a write statement occurs while |
| 300 |
a read transaction is active, then the read transaction is upgraded |
| 301 |
to a write transaction if possible. If some other database connection |
| 302 |
has already modified the database or is already in the process of |
| 303 |
modifying the database, then upgrading to a write transaction is |
| 304 |
not possible and the write statement will fail with <a href="rescode.html#busy">SQLITE_BUSY</a>. |
| 305 |
</p> |
| 306 |
|
| 307 |
<p> |
| 308 |
While a read transaction is active, any changes to the database that |
| 309 |
are implemented by separate database connections will not be seen |
| 310 |
by the database connection that started the read transaction. If database |
| 311 |
connection X is holding a read transaction, it is possible that some |
| 312 |
other database connection Y might change the content of the database |
| 313 |
while X's transaction is still open, however X will not be able to see |
| 314 |
those changes until after the transaction ends. While its read |
| 315 |
transaction is active, X will continue to see an historic snapshot of |
| 316 |
the database prior to the changes implemented by Y. |
| 317 |
</p> |
| 318 |
|
| 319 |
|
| 320 |
<a name="immediate"></a> |
| 321 |
|
| 322 |
<h2 id="deferred_immediate_and_exclusive_transactions"><span>2.2. </span>DEFERRED, IMMEDIATE, and EXCLUSIVE transactions</h2> |
| 323 |
|
| 324 |
<p> |
| 325 |
Transactions can be DEFERRED, IMMEDIATE, or EXCLUSIVE. |
| 326 |
The default transaction behavior is DEFERRED. |
| 327 |
</p> |
| 328 |
|
| 329 |
<p> |
| 330 |
DEFERRED means that the transaction does not actually |
| 331 |
start until the database is first accessed. Internally, |
| 332 |
the BEGIN DEFERRED statement merely sets a flag on the database |
| 333 |
connection that turns off the automatic commit that would normally |
| 334 |
occur when the last statement finishes. This causes the transaction |
| 335 |
that is automatically started to persist until an explicit |
| 336 |
COMMIT or ROLLBACK or until a rollback is provoked by an error |
| 337 |
or an ON CONFLICT ROLLBACK clause. If the first statement after |
| 338 |
BEGIN DEFERRED is a SELECT, then a read transaction is started. |
| 339 |
Subsequent write statements will upgrade the transaction to a |
| 340 |
write transaction if possible, or return SQLITE_BUSY. If the |
| 341 |
first statement after BEGIN DEFERRED is a write statement, then |
| 342 |
a write transaction is started. |
| 343 |
</p> |
| 344 |
|
| 345 |
<p> |
| 346 |
IMMEDIATE causes the database connection to start a new write |
| 347 |
immediately, without waiting for a write statement. The |
| 348 |
BEGIN IMMEDIATE might fail with <a href="rescode.html#busy">SQLITE_BUSY</a> if another write |
| 349 |
transaction is already active on another database connection. |
| 350 |
</p> |
| 351 |
|
| 352 |
<p> |
| 353 |
EXCLUSIVE is similar to IMMEDIATE in that a write transaction |
| 354 |
is started immediately. EXCLUSIVE and IMMEDIATE are the same |
| 355 |
in <a href="wal.html">WAL mode</a>, but in other journaling modes, EXCLUSIVE prevents |
| 356 |
other database connections from reading the database while the |
| 357 |
transaction is underway. |
| 358 |
</p> |
| 359 |
|
| 360 |
<h2 id="implicit_versus_explicit_transactions"><span>2.3. </span>Implicit versus explicit transactions</h2> |
| 361 |
|
| 362 |
<p> |
| 363 |
An implicit transaction (a transaction that is started automatically, |
| 364 |
not a transaction started by BEGIN) is committed automatically when |
| 365 |
the last active statement finishes. A statement finishes when its |
| 366 |
last cursor closes, which is guaranteed to happen when the |
| 367 |
prepared statement is <a href="c3ref/reset.html">reset</a> or |
| 368 |
<a href="c3ref/finalize.html">finalized</a>. Some statements might "finish" |
| 369 |
for the purpose of transaction control prior to being reset or finalized, |
| 370 |
but there is no guarantee of this. The only way to ensure that a |
| 371 |
statement has "finished" is to invoke <a href="c3ref/reset.html">sqlite3_reset()</a> or |
| 372 |
<a href="c3ref/finalize.html">sqlite3_finalize()</a> on that statement. An open <a href="c3ref/blob.html">sqlite3_blob</a> used for |
| 373 |
incremental BLOB I/O also counts as an unfinished statement. |
| 374 |
The <a href="c3ref/blob.html">sqlite3_blob</a> finishes when it is <a href="c3ref/blob_close.html">closed</a>. |
| 375 |
</p> |
| 376 |
|
| 377 |
<p> |
| 378 |
The explicit COMMIT command runs immediately, even if there are |
| 379 |
pending <a href="lang_select.html">SELECT</a> statements. However, if there are pending |
| 380 |
write operations, the COMMIT command |
| 381 |
will fail with an error code <a href="rescode.html#busy">SQLITE_BUSY</a>. |
| 382 |
</p> |
| 383 |
|
| 384 |
<p> |
| 385 |
An attempt to execute COMMIT might also result in an <a href="rescode.html#busy">SQLITE_BUSY</a> return code |
| 386 |
if an another thread or process has an open read connection. |
| 387 |
When COMMIT fails in this |
| 388 |
way, the transaction remains active and the COMMIT can be retried later |
| 389 |
after the reader has had a chance to clear. |
| 390 |
</p> |
| 391 |
|
| 392 |
<p> |
| 393 |
In very old versions of SQLite (before version 3.7.11 - 2012-03-20) |
| 394 |
the ROLLBACK will fail with an error code |
| 395 |
<a href="rescode.html#busy">SQLITE_BUSY</a> if there are any pending queries. In more recent |
| 396 |
versions of SQLite, the ROLLBACK will proceed and pending statements |
| 397 |
will often be aborted, causing them to return an <a href="rescode.html#abort">SQLITE_ABORT</a> or |
| 398 |
<a href="rescode.html#abort_rollback">SQLITE_ABORT_ROLLBACK</a> error. |
| 399 |
In SQLite version 3.8.8 (2015-01-16) and later, |
| 400 |
a pending read will continue functioning |
| 401 |
after the ROLLBACK as long as the ROLLBACK does not modify the database |
| 402 |
schema. |
| 403 |
</p> |
| 404 |
|
| 405 |
<p> |
| 406 |
If <a href="pragma.html#pragma_journal_mode">PRAGMA journal_mode</a> is set to OFF (thus disabling the rollback journal |
| 407 |
file) then the behavior of the ROLLBACK command is undefined. |
| 408 |
</p> |
| 409 |
|
| 410 |
<h1 id="response_to_errors_within_a_transaction"><span>3. </span>Response To Errors Within A Transaction</h1> |
| 411 |
|
| 412 |
<p> If certain kinds of errors occur within a transaction, the |
| 413 |
transaction may or may not be rolled back automatically. The |
| 414 |
errors that can cause an automatic rollback include:</p> |
| 415 |
|
| 416 |
<ul> |
| 417 |
<li> <a href="rescode.html#full">SQLITE_FULL</a>: database or disk full |
| 418 |
</li><li> <a href="rescode.html#ioerr">SQLITE_IOERR</a>: disk I/O error |
| 419 |
</li><li> <a href="rescode.html#busy">SQLITE_BUSY</a>: database in use by another process |
| 420 |
</li><li> <a href="rescode.html#nomem">SQLITE_NOMEM</a>: out of memory |
| 421 |
</li></ul> |
| 422 |
|
| 423 |
<p> |
| 424 |
For all of these errors, SQLite attempts to undo just the one statement |
| 425 |
it was working on and leave changes from prior statements within the |
| 426 |
same transaction intact and continue with the transaction. However, |
| 427 |
depending on the statement being evaluated and the point at which the |
| 428 |
error occurs, it might be necessary for SQLite to rollback and |
| 429 |
cancel the entire transaction. An application can tell which |
| 430 |
course of action SQLite took by using the |
| 431 |
<a href="c3ref/get_autocommit.html">sqlite3_get_autocommit()</a> C-language interface.</p> |
| 432 |
|
| 433 |
<p>It is recommended that applications respond to the errors |
| 434 |
listed above by explicitly issuing a ROLLBACK command. If the |
| 435 |
transaction has already been rolled back automatically |
| 436 |
by the error response, then the ROLLBACK command will fail with an |
| 437 |
error, but no harm is caused by this.</p> |
| 438 |
|
| 439 |
<p>Future versions of SQLite may extend the list of errors which |
| 440 |
might cause automatic transaction rollback. Future versions of |
| 441 |
SQLite might change the error response. In particular, we may |
| 442 |
choose to simplify the interface in future versions of SQLite by |
| 443 |
causing the errors above to force an unconditional rollback.</p> |
| 444 |
<p align="center"><small><i>This page last modified on <a href="https://sqlite.org/docsrc/honeypot" id="mtimelink" data-href="https://sqlite.org/docsrc/finfo/pages/lang_transaction.in?m=9c5adbec23">2023-03-14 14:31:07</a> UTC </small></i></p> |
| 445 |
|