Ján ONDREJ (SAL) schrieb:
> On Nov 9, 2:36 pm, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote:
>> Ján ONDREJ (SAL) schrieb:
>>
>>>   what is a replacement for DBSession.commit() in current TG2 (beta1)?
>>> In some discussions I found, that I have to use transaction.commit(),
>>> but it does not work for me:
>>> UnboundExecutionError: Instance <User at 0xa8d728c> is not bound to a
>>> Session; attribute refresh operation cannot proceed
>>> Why I need manual commit:
>>>   1. I need to commit partial data to use a proper auto_increment
>>> value later. After commit I can use auto incremented value to store
>>> more records. I know, that this can be solved by changing my model and
>>> use interaction between tables.
>>>   2. One more important thing is to show proper error message. When an
>>> transaction fails to store (for example because there is another
>>> unique column in database) I need to know, which error has raised and
>>> flash proper message. I don't know, how to fix this in general.
>>> I can remove ZopeTransactionExtension() from mi sessionmaker, but I
>>> don't know, what it means and how it applies to other TG2 code.
>>> DBSession.commit is working well without this extension.
>> You shouldn't use DBSession.commit. Instead, you need DBSession.flush.
>> This will send all outstanding SQL to the DB, resulting in the creation
>> of ids.
> 
> With only flush this exception is raised:
>   InvalidRequestError: The transaction is closed
> 
> This is my code, which worked before some updates with TG2:
> 
> def save_transaction(data=None, delete=False):
>     '''
>     Try to save transaction, return False on fail.
>     '''
>     try:
>       if delete==True:
>         DBSession.delete(data)
>       elif delete:
>         DBSession.delete(delete)
>       elif data:
>         DBSession.add(data)
>       DBSession.flush()
>       DBSession.commit()
>     except SQLError, es:
>       # try to reset transaction
>       logging.getLogger('datatypes').info("SQLError: %s" % es)
>       pylons.c.error_block = _("SQLError: %s!") % es.orig
>       #DBSession.expunge(data)
>       DBSession.rollback()
>       return False
>     return True
> 
> Without commit (and with flush) and exception in raised. With
> transaction.commit() another exception is raised.
> Can you fix this code to to store data and fill pylons.c.error_block
> with right error code?
> Storing data into DB without handling error codes is not a good
> solution.
> 
>> Or, even better, use objects instead of ids. This is of course not
>> *always* feasible, e.g. if you want to render a freshly created object's
>> id into HTML.
> 
> This can help me to solve my first problem, but not second. :-(
> 
>> Do not remove the ZTE.
> 
> Can you explain, why?

Because it is needed by repoze.tm middleware that manages the 
transactions for you.

The bottomline is that you *shouldn't*  need (or even further aren't 
allowed) to begin and commit/rollback transactions manually.

This works for me without a hitch. Maybe you need to adjust the setup of 
the DBSession to match whatever the current TG2 is doing (under the 
assumption that your app has been developed over the course of a few 
TG2-subreleases/trunk-versions)



Diez

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears Trunk" 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-trunk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to