[sqlalchemy] Re: session.flush() closing connection

2006-10-20 Thread Michael Bayer

hey François -

are you using the SessionTransaction explicitly ?  or connection.begin 
() ?   Ive tried many combinations, and the only way I can reproduce  
the problem is by doing something incorrect:

 c = engine.connect()
 s = create_session(bind_to=c)

 tran = c.begin()
 session_tran = s.create_transaction()

 s.save(User())
 s.flush()
 u = User()
 s.save(u)
 s.user_name = 'some user'

 tran.commit()
 session_tran.commit()

the reason the above is incorrect is because the Transaction and  
SessionTransaction are not nested properly.

The fix you have wouldnt be correct since the SessionTransaction is  
being closed (if not the underlying connection, which was the  
original bug), so it should remove its association from its host  
Session.

On Oct 20, 2006, at 6:59 AM, François Wautier wrote:


 Hi Michael,

 It seems I spoke too quickly.

 The problem is now when I try to flush a  second time with a new  
 object.
 Something like this

   newguy=dbPeople()
   session.save(newguy)
   newguy.Lastname=Doe
   newguy.Firstname=John
   newguy.gender=Ambiguous
   session.flush()
   newguy=dbPeople()
   session.save(newguy)
   newguy.Lastname=Doe
   newguy.Firstname=Jane
   newguy.gender=Sheila
   session.flush()

 The last session flush results in a new record being written to the  
 database,
 but an exception is raised, with the error message

   This transaction is inactive

 If one were to try to add more dbPeople, the records won't be saved  
 into the
 database for the session keeps on using the same key value (the  
 table uses
 an auto_increment)


 I hacked the code a bit and I solved the problem but I am far  
 from sure
 that I did the right thing for all cases

 In  lib/sqlalchemy/orm/session.py  around line 67 I changed

for t in self.connections.values():
 if (t[2]):
 t[0].close()
   self.session.transaction = None

 into
  keeptransaction=False
for t in self.connections.values():
 if (t[2]):
 t[0].close()
 else:
 keeptransaction=True
   if not keeptransaction=False:
   self.session.transaction = None

 I wonder if something like this would not be preferable (but I  
 again, I have
 no clue as to what the consequences of my code is)

   closeall=False
   for t in self.connections.values():
 if (t[2]):
   closeall=True
   if closeall:
   for t in self.connections.values():
 t[0].close()
   self.session.transaction = None


 Regards,
   François



 


--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/sqlalchemy
-~--~~~~--~~--~--~---



[sqlalchemy] Re: session.flush() closing connection

2006-10-19 Thread François Wautier

Thanks for the patch... it works... so far

And sorry for the double post... my original email was held for a very long 
time on some google host

Cheers,
François


 thats a bug.  its because the flush() is closing the connection you
 passed to your session.

 heres a patch that fixes it, which i will try to commit later today but
 i want to work up some test cases:

 Index: lib/sqlalchemy/orm/session.py
 ===
 --- lib/sqlalchemy/orm/session.py   (revision 1852)
 +++ lib/sqlalchemy/orm/session.py   (working copy)
 @@ -37,7 +37,7 @@
  e = connectable.engine
  c = connectable.contextual_connect()
  if not self.connections.has_key(e):
 -self.connections[e] = (c, c.begin())
 +self.connections[e] = (c, c.begin(), c is not connectable)
  return self.connections[e][0]
  def commit(self):
  if self.parent is not None:
 @@ -58,7 +58,8 @@
  if self.parent is not None:
  return
  for t in self.connections.values():
 -t[0].close()
 +if (t[2]):
 +t[0].close()
  self.session.transaction = None

  class Session(object):




--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/sqlalchemy
-~--~~~~--~~--~--~---