Thanks Mike and Connor for your advise.
This is what finally worked for me as a ID generator hook. I tested it
by forcing an exception to verify that the ID generator transaction
and the transaction within my application logic (via the session) are
seperate and independent.
def newid1(ctx):
log.debug('generator.newid1() invoked with context = %s' %ctx)
conn = meta.engine.connect()
txn = conn.begin()
id = conn.execute(select([pkgt.c.next_id],
pkgt.c.table_name=='principals',
for_update=True)).scalar()
conn.execute(pkgt.update(values={pkgt.c.next_id : pkgt.c.next_id
+1}))
#if id == 18:
# raise Exception('ID 18 received.....now you should see a
rollback')
txn.commit()
log.debug('Finished writing back the next id')
return id
On Sep 28, 12:41 pm, Naresh Khalasi <[email protected]> wrote:
> Thanks Mike for all the explanations.
>
> On Mon, Sep 28, 2009 at 7:54 AM, Michael Bayer <[email protected]>
> wrote:
>
> > nkhalasi wrote:
>
> >> However with this I am getting unwanted commits. Essentially when the
> >> newid(ctx) function executes the update it also does an commit which
> >> results into my data committed which I would have otherwise expected
> >> to be committed at some other point. I am trying to figure out how can
> >> this update of next ID be done along with my regular application logic
> >> commit.
>
> > if you are doing something like engine.execute(statement), you'd need to
> > use a transaction. i.e. conn = engine.connect(); trans = conn.begin();
> > conn.execute(statement); trans.commit().
>
> > if you were executing the INSERT via the ORM (i.e. Session.commit()) the
> > connection you receive is within a transaction and no autocommit will
> > occur.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" 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/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---