Re: [sqlalchemy] Re: ObjectDeletedError: Instance 'xxx' has been deleted

2012-11-21 Thread sajuptpm
Hi, Michael Bayer. 
Thanks, that example is really helpful.

In your example I used 
*Session.object_session*http://docs.sqlalchemy.org/en/latest/orm/session.html#sqlalchemy.orm.session.Session.object_sessionmethod
 to verify that main_method 
and method1 running in two different sessions.
But my test result showing that main_method and method1 running in same 
session.
I think, we can check this using method *Session.object_session** *, I am 
not sure about that.

* added following code in main_method
s1 = Session.object_session(db_obj1)
print ===main_methodsession=, vars(s1)

* added following code in method1
s2 = Session.object_session(db_obj2)
print ===method1session=, vars(s2)


*Result
=*

*===main_methodsession=*  {'autocommit': False, 'autoflush': 
True, 'transaction': sqlalchemy.orm.session.SessionTransaction object at 
0x22a5f90, *'hash_key': 36330896*, 'expire_on_commit': True, '_new': {}, 
'bind': Engine(mysql://root:cvt@localhost/cvt_ee), '_deleted': {}, 
'_flushing': False, 'identity_map': {(class '__main__.A', (1L,)): 
sqlalchemy.orm.state.InstanceState object at 0x22a5c90}, 
'_enable_transaction_accounting': True, 'extensions': [], '_identity_cls': 
class 'sqlalchemy.orm.identity.WeakInstanceDict', 'twophase': False, 
'_Session__binds': {}, '_query_cls': class 'sqlalchemy.orm.query.Query', 
'_mapper_flush_opts': {}}


===method1session= {'autocommit': False, 'autoflush': True, 
'transaction': sqlalchemy.orm.session.SessionTransaction object at 
0x2073190,* 'hash_key': 36330896,* 'expire_on_commit': True, '_new': {}, 
'bind': Engine(mysql://root:cvt@localhost/cvt_ee), '_deleted': {}, 
'_flushing': False, 'identity_map': {(class '__main__.A', (2L,)): 
sqlalchemy.orm.state.InstanceState object at 0x2073550, (class 
'__main__.A', (1L,)): sqlalchemy.orm.state.InstanceState object at 
0x22a5c90}, '_enable_transaction_accounting': True, 'extensions': [], 
'_identity_cls': class 'sqlalchemy.orm.identity.WeakInstanceDict', 
'twophase': False, '_Session__binds': {}, '_query_cls': class 
'sqlalchemy.orm.query.Query', '_mapper_flush_opts': {}}



-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/sqlalchemy/-/qzSbUaBu8UoJ.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] Re: ObjectDeletedError: Instance 'xxx' has been deleted

2012-11-17 Thread Michael Bayer

On Nov 17, 2012, at 1:16 AM, sajuptpm wrote:

 H, Michael Bayer
 Thanks
 
 You are correct, the rollback in method1 rollbacking transaction in 
 main_method.
 I want to isolate transaction in main_method from rollback in method1.
 
 I attached more code.
 
 
 from sqlalchemy.orm import scoped_session, sessionmaker
 maker = sessionmaker(autoflush=True, autocommit=False,expire_on_commit=False,
  extension=ZopeTransactionExtension())
 zopelessmaker = sessionmaker(autoflush=True, \
  autocommit=False, \
  expire_on_commit=False)
 DBSession = scoped_session(maker)
 
 
 
 def main_method():
 db_obj1 = DBModelclass1(Hello)
 DBSession.add(db_obj1)
 DBSession.fush()
 
 for x in lst:
 try:
 method1(db_obj1.id)
 excpt Exception, ex:
 pass
 
 
 
 def method1(id):
 try:
 
 s1 = DBSession()
 s1.begin_nested()
 db_obj2 = DBModelclass2(Test)
 db_obj2.refname = name_%s %(id)
 DBSession.add(db_obj2)
 DBSession.fush()
 
 if some-codition:
 raise Exception(Failedd)
 
 s1.commit()
 except Exception, ex:
 s1.rollback()
 raise ex 

other than the typos, as well as the multiple calls to method1() for every 
x in a list where the x is apparently ignored, there is nothing obviously 
wrong with this code.  If you're using begin_nested(), it has to be on a 
platform that supports SAVEPOINT.  Currently Postgresql and MySQL with InnoDB 
are the two main platforms supported.

As I have already stated in my previous email, turning on echo=True and/or 
echo='debug' will illustrate the SQL being emitted as well as where 
transactions start and end.  This is a *critical* step to being able to 
understand your issue.  I'd also recommend using pdb so that you can step into 
the code and examine the state of the database and program state one line at a 
time.


-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



[sqlalchemy] Re: ObjectDeletedError: Instance 'xxx' has been deleted

2012-11-16 Thread sajuptpm


The code bellow throwing error ObjectDeletedError: Instance 'xxx' has been 
deleted. when a exception throwing from method1.
Eroor in line method1(db_obj1.id),  db_obj1.id failing.

How fix this issue.


def main_method():
DBSession.add(db_obj1)
DBSession.fush()

for x in lst:
try:
method1(db_obj1.id)
excpt Exception, ex:
pass


def method1(id):
try:
s1 = DBSession()
s1.begin_nested()

db_obj2 = create_new_obj(id)
DBSession.add(db_obj1)
DBSession.fush()

if some-codition:
raise Exception(Failedd)

s1.commit()
except Exception, ex:
s1.rollback()
raise ex

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/sqlalchemy/-/ya-rKw3XinwJ.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] Re: ObjectDeletedError: Instance 'xxx' has been deleted

2012-11-16 Thread sajuptpm
H, Michael Bayer
Thanks

You are correct, the rollback in method1 rollbacking transaction in 
main_method.
I want to isolate transaction in main_method from rollback in method1.

I attached more code.


from sqlalchemy.orm import scoped_session, sessionmaker
maker = sessionmaker(autoflush=True, 
autocommit=False,expire_on_commit=False,
 extension=ZopeTransactionExtension())
zopelessmaker = sessionmaker(autoflush=True, \
 autocommit=False, \
 expire_on_commit=False)
DBSession = scoped_session(maker)



def main_method():
db_obj1 = DBModelclass1(Hello)
DBSession.add(db_obj1)
DBSession.fush()

for x in lst:
try:
method1(db_obj1.id)
excpt Exception, ex:
pass



def method1(id):
try:

s1 = DBSession()
s1.begin_nested()
db_obj2 = DBModelclass2(Test)
db_obj2.refname = name_%s %(id)
DBSession.add(db_obj2)
DBSession.fush()

if some-codition:
raise Exception(Failedd)

s1.commit()
except Exception, ex:
s1.rollback()
raise ex 


-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/sqlalchemy/-/uIJwy6KOAdsJ.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.