Am 04.03.2011 um 22:44 schrieb demitri:



On Mar 1, 5:04 pm, "Diez B. Roggisch" <[email protected]> wrote:

A scoped session is a factory for the actual sesion - but you can use
it directly, for that thread, there will be a local instance. So it's
perfectly fine to call DBSession.<whatever>, it should work.

Thank - I'll give that a try.

The confusing thing from the tutorial here:
http://turbogears.org/2.0/docs/main/Wiki20/wiki20.html

is the use of transaction. When does the transaction begin - during
"import transaction"? Is there a reason to use this over

def index:
   session = DBSession()
   ...
   session.commit()

you confuse a few things here.

First of all, a transaction in SQLAlchemy is begun implicitly. You never call begin(). But this is not done through import - how could it be, you don't know your database configuration during import!

Second, the example you show doesn't really apply, because it's about setting up the application for testing or initial boot-strapping. It's *not* what happens during a normal application run.

So it's next to irrelevant for this discussion.

What happens in TG2 is this:

- through the ZopeTransactionExtension, SQLAlchemy's Session is hooked into the transaction-modules begin/commit/abort-functions - through repoze.tm2, there is a middleware generated that on each request will begin a transaction (as explained above, in SA a NOP), and when the request ends, it will commit or rollback - depending on either doom() being called, or an exception not being caught, or a normal run. - inside your controller, you never ever can run begin/commit/ rollback. You can only doom a transaction. - the last point is not 100% true - you can use commit/rollback your own, but only if your database supports sub-transactions. Then, the begin() is actually not a NOP.


But usually, that's not what you do. Instead, you write code that

- properly checks before jumping,as e.g. violating a unique- constraint will abort the transaction
 - locks, if needed

HTH,

Diez

--
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en.

Reply via email to