What do you mean by "Auto Commit" ?
1) SqlAlchemy has an auto-commit mode. That means you do not wrap
statements in transactions, and data is directly written to the tables.
You would still need to call a "flush()" for SqlAlchemy to talk to the
database though. SqlAlchemy will not talk to the database until you tell
it to.
2) Pyramid has the pyramid_tm transaction manager, which will automatically
wrap every view in a transaction; starting a request off with a BEGIN and
ending with a commit (if you do not raise any errors) or rollback if you do
raise errors.
the execution flow under pyramid tm is roughly like this:
* pyramid - new request
* pyramid_tm - transaction begin
* your code - some_view()
* pyramid_tm - if an exception was raised, "rollback" ; else "commit"
under pyramid_tm, the view can not wait for the transaction to
commit/rollback. the view must finish and return a value before pyramid_tm
will try to finish the transaction.
if you need to use the results of any database activity in your response,
you should call flush() -- which will make the data available to the view.
here's a quick example:
@viewconfig( renderer='json' )
def new_user(request):
user = model.User( name="jonathan" )
dbSession.add(user)
dbSession.flush()
return { 'user_id': user.id }
if I hit this view the first time:
* in the database i create a new user, i flush() the connection to
write to the database and my 'user.id' is now available. i return the
user.id. no exceptions were raised, so pyramid_tm commits the transaction.
if i run this another time -- and everything works fine, but there was an
unknown database error
* everything is fine, so pyramid_tm tries to commit. something on the
database won't allow it, so it catches the error and issues a rollback. an
error is raised.
if i run this a third time -- but now there is a unique constraint on the
user name --
* when I flush() the transaction, sqlalchemy raises an error. i didn't
catch it, so it bubbles up. eventually pyramid_tm catches it, and issues a
rollback.
--
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/pylons-discuss.
For more options, visit https://groups.google.com/d/optout.