[Sqlalchemy-commits] [1163] sqlalchemy/trunk/lib/sqlalchemy: overhaul to SQLSession change from yest
Brought to you by:
zzzeek
From: <co...@sq...> - 2006-03-17 21:58:11
|
<!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>[1163] sqlalchemy/trunk/lib/sqlalchemy: overhaul to SQLSession change from yesterday, needs to use the threadlocal capability of the pool after all</title> </head> <body> <div id="msg"> <dl> <dt>Revision</dt> <dd>1163</dd> <dt>Author</dt> <dd>zzzeek</dd> <dt>Date</dt> <dd>2006-03-17 15:58:00 -0600 (Fri, 17 Mar 2006)</dd> </dl> <h3>Log Message</h3> <pre>overhaul to SQLSession change from yesterday, needs to use the threadlocal capability of the pool after all</pre> <h3>Modified Paths</h3> <ul> <li><a href="#sqlalchemytrunklibsqlalchemyenginepy">sqlalchemy/trunk/lib/sqlalchemy/engine.py</a></li> <li><a href="#sqlalchemytrunklibsqlalchemypoolpy">sqlalchemy/trunk/lib/sqlalchemy/pool.py</a></li> </ul> </div> <div id="patch"> <h3>Diff</h3> <a id="sqlalchemytrunklibsqlalchemyenginepy"></a> <div class="modfile"><h4>Modified: sqlalchemy/trunk/lib/sqlalchemy/engine.py (1162 => 1163)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/trunk/lib/sqlalchemy/engine.py 2006-03-17 21:22:30 UTC (rev 1162) +++ sqlalchemy/trunk/lib/sqlalchemy/engine.py 2006-03-17 21:58:00 UTC (rev 1163) </span><span class="lines">@@ -171,33 +171,42 @@ </span><span class="cx"> return default.arg </span><span class="cx"> </span><span class="cx"> class SQLSession(object): </span><del>- """represents a particular connection retrieved from a SQLEngine, and associated transactional state.""" </del><ins>+ """represents a a handle to the SQLEngine's connection pool. the default SQLSession maintains a distinct connection during transactions, otherwise returns connections newly retrieved from the pool each time. the Pool is usually configured to have use_threadlocal=True so if a particular connection is already checked out, youll get that same connection in the same thread. There can also be a "unique" SQLSession pushed onto the engine, which returns a connection via the unique_connection() method on Pool; this allows nested transactions to take place, or other operations upon more than one connection at a time.`""" </ins><span class="cx"> def __init__(self, engine, parent=None): </span><span class="cx"> self.engine = engine </span><span class="cx"> self.parent = parent </span><ins>+ # if we have a parent SQLSession, then use a unique connection. + # else we use the default connection returned by the pool. + if parent is not None: + self.__connection = self.engine._pool.unique_connection() </ins><span class="cx"> self.__tcount = 0 </span><span class="cx"> def _connection(self): </span><span class="cx"> try: </span><del>- return self.__connection </del><ins>+ return self.__transaction </ins><span class="cx"> except AttributeError: </span><del>- self.__connection = self.engine._pool.connect() - return self.__connection </del><ins>+ try: + return self.__connection + except AttributeError: + return self.engine._pool.connect() </ins><span class="cx"> connection = property(_connection, doc="the connection represented by this SQLSession. The connection is late-connecting, meaning the call to the connection pool only occurs when it is first called (and the pool will typically only connect the first time it is called as well)") </span><span class="cx"> </span><span class="cx"> def begin(self): </span><span class="cx"> """begins" a transaction on this SQLSession's connection. repeated calls to begin() will increment a counter that must be decreased by corresponding commit() statements before an actual commit occurs. this is to provide "nested" behavior of transactions so that different functions in a particular call stack can call begin()/commit() independently of each other without knowledge of an existing transaction.""" </span><span class="cx"> if self.__tcount == 0: </span><ins>+ self.__transaction = self.connection </ins><span class="cx"> self.engine.do_begin(self.connection) </span><span class="cx"> self.__tcount += 1 </span><span class="cx"> def rollback(self): </span><span class="cx"> """rolls back the transaction on this SQLSession's connection. this can be called regardless of the "begin" counter value, i.e. can be called from anywhere inside a callstack. the "begin" counter is cleared.""" </span><span class="cx"> if self.__tcount > 0: </span><span class="cx"> self.engine.do_rollback(self.connection) </span><ins>+ del self.__transaction </ins><span class="cx"> self.__tcount = 0 </span><span class="cx"> def commit(self): </span><span class="cx"> """commits the transaction started by begin(). If begin() was called multiple times, a counter will be decreased for each call to commit(), with the actual commit operation occuring when the counter reaches zero. this is to provide "nested" behavior of transactions so that different functions in a particular call stack can call begin()/commit() independently of each other without knowledge of an existing transaction.""" </span><span class="cx"> if self.__tcount == 1: </span><span class="cx"> self.engine.do_commit(self.connection) </span><ins>+ del self.__transaction </ins><span class="cx"> self.__tcount = 0 </span><span class="cx"> elif self.__tcount > 1: </span><span class="cx"> self.__tcount -= 1 </span><span class="lines">@@ -223,7 +232,7 @@ </span><span class="cx"> (cargs, cparams) = self.connect_args() </span><span class="cx"> if pool is None: </span><span class="cx"> params['echo'] = echo_pool </span><del>- params['use_threadlocal'] = False </del><ins>+ params['use_threadlocal'] = True </ins><span class="cx"> self._pool = sqlalchemy.pool.manage(self.dbapi(), **params).get_pool(*cargs, **cparams) </span><span class="cx"> else: </span><span class="cx"> self._pool = pool </span><span class="lines">@@ -417,9 +426,11 @@ </span><span class="cx"> def do_rollback(self, connection): </span><span class="cx"> """implementations might want to put logic here for turning autocommit on/off, </span><span class="cx"> etc.""" </span><ins>+ #print "ENGINE ROLLBACK ON ", connection.connection </ins><span class="cx"> connection.rollback() </span><span class="cx"> def do_commit(self, connection): </span><span class="cx"> """implementations might want to put logic here for turning autocommit on/off, etc.""" </span><ins>+ #print "ENGINE COMMIT ON ", connection.connection </ins><span class="cx"> connection.commit() </span><span class="cx"> </span><span class="cx"> def _session(self): </span></span></pre></div> <a id="sqlalchemytrunklibsqlalchemypoolpy"></a> <div class="modfile"><h4>Modified: sqlalchemy/trunk/lib/sqlalchemy/pool.py (1162 => 1163)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/trunk/lib/sqlalchemy/pool.py 2006-03-17 21:22:30 UTC (rev 1162) +++ sqlalchemy/trunk/lib/sqlalchemy/pool.py 2006-03-17 21:58:00 UTC (rev 1163) </span><span class="lines">@@ -196,7 +196,7 @@ </span><span class="cx"> </span><span class="cx"> def do_return_conn(self, conn): </span><span class="cx"> if self._echo: </span><del>- self.log("return connection to pool") </del><ins>+ self.log("return QP connection to pool") </ins><span class="cx"> try: </span><span class="cx"> self._pool.put(conn, False) </span><span class="cx"> except Queue.Full: </span><span class="lines">@@ -210,7 +210,7 @@ </span><span class="cx"> </span><span class="cx"> def do_get(self): </span><span class="cx"> if self._echo: </span><del>- self.log("get connection from pool") </del><ins>+ self.log("get QP connection from pool") </ins><span class="cx"> self.log(self.status()) </span><span class="cx"> try: </span><span class="cx"> return self._pool.get(self._max_overflow > -1 and self._overflow >= self._max_overflow) </span></span></pre> </div> </div> </body> </html> |