[Sqlalchemy-commits] [1436] sqlalchemy/branches/schema/doc/build/content/tutorial.txt: threadlocal c
Brought to you by:
zzzeek
From: <co...@sq...> - 2006-05-11 19:53:21
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head><style type="text/css"><!-- #msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; } #msg dt { float: left; width: 6em; font-weight: bold; } #msg dt:after { content:':';} #msg dl, #msg dt, #msg ul, #msg li { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; } #msg dl a { font-weight: bold} #msg dl a:link { color:#fc3; } #msg dl a:active { color:#ff0; } #msg dl a:visited { color:#cc6; } h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; } #msg pre { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; } #msg ul, pre { overflow: auto; } #patch { width: 100%; } #patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;} #patch .propset h4, #patch .binary h4 {margin:0;} #patch pre {padding:0;line-height:1.2em;margin:0;} #patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;} #patch .propset .diff, #patch .binary .diff {padding:10px 0;} #patch span {display:block;padding:0 10px;} #patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;} #patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;} #patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;} #patch .lines, .info {color:#888;background:#fff;} --></style> <title>[1436] sqlalchemy/branches/schema/doc/build/content/tutorial.txt: threadlocal concept removed</title> </head> <body> <div id="msg"> <dl> <dt>Revision</dt> <dd>1436</dd> <dt>Author</dt> <dd>zzzeek</dd> <dt>Date</dt> <dd>2006-05-11 14:53:11 -0500 (Thu, 11 May 2006)</dd> </dl> <h3>Log Message</h3> <pre>threadlocal concept removed</pre> <h3>Modified Paths</h3> <ul> <li><a href="#sqlalchemybranchesschemadocbuildcontenttutorialtxt">sqlalchemy/branches/schema/doc/build/content/tutorial.txt</a></li> </ul> </div> <div id="patch"> <h3>Diff</h3> <a id="sqlalchemybranchesschemadocbuildcontenttutorialtxt"></a> <div class="modfile"><h4>Modified: sqlalchemy/branches/schema/doc/build/content/tutorial.txt (1435 => 1436)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/branches/schema/doc/build/content/tutorial.txt 2006-05-11 15:56:19 UTC (rev 1435) +++ sqlalchemy/branches/schema/doc/build/content/tutorial.txt 2006-05-11 19:53:11 UTC (rev 1436) </span><span class="lines">@@ -47,11 +47,10 @@ </span><span class="cx"> </span><span class="cx"> ### Imports </span><span class="cx"> </span><del>-SQLAlchemy provides the entire namespace of everything you'll need under the module name `sqlalchemy`. For the purposes of this tutorial, we will import its full list of symbols into our own local namespace. We also will be using a *mod* that provides access to thread-managed connection and session objects, which will greatly simplifies our code. A *mod* is a module that augments the core functionality of SQLAlchemy with additional functionality, and only needs to be imported once within an application. </del><ins>+SQLAlchemy provides the entire namespace of everything you'll need under the module name `sqlalchemy`. For the purposes of this tutorial, we will import its full list of symbols into our own local namespace. </ins><span class="cx"> </span><span class="cx"> {python} </span><span class="cx"> >>> from sqlalchemy import * </span><del>- >>> import sqlalchemy.mods.threadlocal </del><span class="cx"> </span><span class="cx"> ### Connecting to the Database </span><span class="cx"> </span><span class="lines">@@ -287,10 +286,10 @@ </span><span class="cx"> </span><span class="cx"> After you create a Mapper, all operations with that Mapper require the usage of an important object called a `Session`. All objects loaded or saved by the Mapper must be *attached* to a `Session` object, which represents a kind of "workspace" of objects that are loaded into memory. A particular object instance can only be attached to one `Session` at a time. </span><span class="cx"> </span><del>-By default, you have to create a `Session` object explicitly before you can load or save objects. But recall that we imported a special *mod* called `threadlocal`, which has made life easier for us by creating a `Session` that is automatically associated with the current thread. So now, lets get a handle to that `Session` and deal with it directly. To locate the `Session` corresponding to the current thread, just use `current_session()`: </del><ins>+By default, you have to create a `Session` object explicitly before you can load or save objects. Theres several ways to manage sessions, but the most straightforward is to just create one, which we will do by saying, `create_session()`: </ins><span class="cx"> </span><span class="cx"> {python} </span><del>- >>> session = current_session() </del><ins>+ >>> session = create_session() </ins><span class="cx"> >>> session # doctest:+ELLIPSIS </span><span class="cx"> <sqlalchemy.orm.session.Session object at 0x...> </span><span class="cx"> </span><span class="lines">@@ -307,7 +306,7 @@ </span><span class="cx"> ['Harry'] </span><span class="cx"> [(User Harry,password:None)] </span><span class="cx"> </span><del>-All querying for objects is performed via an instance of `Query`. The various `select` methods on an instance of `Mapper` also use an underlying `Query` object to perform the operation. A `Query` can be bound to a specific `Session`, or it can also use `current_session()` to locate the session bound to the current thread, if one is available. </del><ins>+All querying for objects is performed via an instance of `Query`. The various `select` methods on an instance of `Mapper` also use an underlying `Query` object to perform the operation. A `Query` is always bound to a specific `Session`. </ins><span class="cx"> </span><span class="cx"> Lets turn off the database echoing for a moment, and try out a few methods on `Query`. Methods that end with the suffix `_by` primarily take keyword arguments which correspond to properties on the object. Other methods take `ClauseElement` objects, which are constructed by using `Column` objects inside of Python expressions, in the same way as we did with our SQL select example in the previous section of this tutorial. Using `ClauseElement` structures to query objects is more verbose but more flexible: </span><span class="cx"> </span><span class="lines">@@ -328,12 +327,13 @@ </span><span class="cx"> </span><span class="cx"> ### Making Changes {@name=changes} </span><span class="cx"> </span><del>-With a little experience in loading objects, lets see what its like to make changes. First, lets create a new user "Ed". We do this by just constructing the new object. The `Mapper` for the `User` class will be called when we create the object, which will then automatically add it to the current thread's Session, if one is available: </del><ins>+With a little experience in loading objects, lets see what its like to make changes. First, lets create a new user "Ed". We do this by just constructing the new object. Then, we just add it to the session: </ins><span class="cx"> </span><span class="cx"> {python} </span><span class="cx"> >>> ed = User() </span><span class="cx"> >>> ed.user_name = 'Ed' </span><span class="cx"> >>> ed.password = 'edspassword' </span><ins>+ >>> session.save(ed) </ins><span class="cx"> >>> ed in session </span><span class="cx"> True </span><span class="cx"> </span><span class="lines">@@ -352,7 +352,7 @@ </span><span class="cx"> >>> mary is mary2 </span><span class="cx"> True </span><span class="cx"> </span><del>-With the identity map, a single `Session` can be relied upon to keep all loaded instances straight, and when a thread-local Session is used, it becomes pretty hard for an application to lose track of the changes made on objects. </del><ins>+With the identity map, a single `Session` can be relied upon to keep all loaded instances straight. </ins><span class="cx"> </span><span class="cx"> As far as the issue of the same object being modified in two different Sessions, that's an issue of concurrency detection; SQLAlchemy does some basic concurrency checks when saving objects, with the option for a stronger check using version ids. See [adv_datamapping](rel:adv_datamapping) for more details. </span><span class="cx"> </span><span class="lines">@@ -375,8 +375,6 @@ </span><span class="cx"> UPDATE users SET password=? WHERE users.user_id = ? </span><span class="cx"> ['harrysnewpassword', 4] </span><span class="cx"> INSERT INTO users (user_name, password) VALUES (?, ?) </span><del>- [None, None] - INSERT INTO users (user_name, password) VALUES (?, ?) </del><span class="cx"> ['Ed', 'edspassword'] </span><span class="cx"> DELETE FROM users WHERE users.user_id = ? </span><span class="cx"> [3] </span><span class="lines">@@ -455,6 +453,7 @@ </span><span class="cx"> ... fred.user_name = 'fred_again' </span><span class="cx"> ... fred.addresses.append(Address('fr...@fr...')) </span><span class="cx"> ... fred.addresses.append(Address('fre...@fr...')) </span><ins>+ ... session.save(fred) </ins><span class="cx"> ... transaction.commit() </span><span class="cx"> ... except: </span><span class="cx"> ... transaction.rollback() </span><span class="lines">@@ -476,9 +475,9 @@ </span><span class="cx"> INSERT INTO users (user_name, password) VALUES (?, ?) </span><span class="cx"> ['fred_again', None] </span><span class="cx"> INSERT INTO email_addresses (email_address, user_id) VALUES (?, ?) </span><del>- ['fr...@fr...', 7] </del><ins>+ ['fr...@fr...', 6] </ins><span class="cx"> INSERT INTO email_addresses (email_address, user_id) VALUES (?, ?) </span><del>- ['fre...@fr...', 7] </del><ins>+ ['fre...@fr...', 6] </ins><span class="cx"> COMMIT </span><span class="cx"> </span><span class="cx"> Main documentation: [unitofwork](rel:unitofwork) </span></span></pre> </div> </div> </body> </html> |