Re: [sqlalchemy] Passing sessions around

2011-02-09 Thread Michael Bayer

On Feb 9, 2011, at 1:59 PM, Brent McConnell wrote:

> Thanks for the quick reply.  I've tried to include more specific info from my 
> program for you to look over as well as the log.
> 
>def approve_request(self, request_ids, session=None):
>if session is None:
>session = scopedSession()
> 
>try:
>for request_id in request_ids:
>request = self.get_request(request_id)
>if request is None:
>raise RequestStoreError("ID does not exist")
>#set request to True
>request.status = True
>session.add(request)
>store_logger.debug("approve_reqeuest before: session=%s, 
> session dirty=%s" % (id(session), session.dirty))
>#add to the corresponding url as exception to standard rules
>filter_store.find_or_create_exception_url(request.owned_by_id, 
> request.request_url, session=session)
>store_logger.debug("approve_request after: session=%s, session 
> dirty=%s" % (id(session), session.dirty))
>session.commit()
>except Exception as e:
>print e
>return False
>finally:
>session.close()
>return True  
> 
>def find_or_create_exception_url(self, fg, url, displayName=None, 
> session=None):
>if session is None:
>session = scopedSession()
> 
>result = self.get_exception_url(fg, url)
>store_logger.debug("result=%s" % result)
>if result is None:
>store_logger.debug("result not Found")
>banned = self.get_banned_url(fg, url)
>if banned is not None:
>session.delete(banned)
>session.flush()
>result = ExceptionUrl(url, fg, displayName)
>session.add(result)
>store_logger.debug("adding ExceptionUrl=%s", result)
> 
>store_logger.debug("find_or_create_exception_url: session=%s, 
> session_dirty=%s" % (id(session), session.dirty))
>return result
> 
> Notice that I have some debug statements in each function.  When the 
> approve_request function is called with an array of ids, the logging results 
> are below.  The session is always the same which is what is expected but a 
> couple of unexpected things happen
> 
> 1)  When in the find_or_create_exception_url function the IdentifySet of the 
> session.dirty is empty even though those records ACTUALLY DO get created in 
> the database.
> 2)  Upon returning to approve_request the IdentifySet is empty which means 
> the status of the request is never set to True.
> 
> 2011-02-09 13:36:42,817 - surf.store - DEBUG - approve_reqeuest before: 
> session=4335326160, session dirty=IdentitySet([ 'http://test19.com', '330eee30-381c-48f7-837e-34be7d2cb1c0', 'True', 'None', 
> 'None', 'None')>])
> 2011-02-09 13:36:42,985 - surf.store - DEBUG - result=None
> 2011-02-09 13:36:42,985 - surf.store - DEBUG - result not Found
> 2011-02-09 13:36:43,116 - surf.store - DEBUG - adding 
> ExceptionUrl=http://test19.com','67868150974853021936294845903799103936','None')>
> 2011-02-09 13:36:43,117 - surf.store - DEBUG - find_or_create_exception_url: 
> session=4335326160, session_dirty=IdentitySet([])
> 2011-02-09 13:36:43,117 - surf.store - DEBUG - approve_request after: 
> session=4335326160, session dirty=IdentitySet([])
> monitor running
> 2011-02-09 13:36:43,365 - surf.store - DEBUG - approve_reqeuest before: 
> session=4335326160, session dirty=IdentitySet([ 'http://test18.com', '330eee30-381c-48f7-837e-34be7d2cb1c0', 'True', 'None', 
> 'None', 'None')>])
> ['186', '185'] approved
> 2011-02-09 13:36:43,540 - surf.store - DEBUG - result=None
> 2011-02-09 13:36:43,540 - surf.store - DEBUG - result not Found
> 2011-02-09 13:36:43,665 - surf.store - DEBUG - adding 
> ExceptionUrl=http://test18.com','67868150974853021936294845903799103936','None')>
> 2011-02-09 13:36:43,666 - surf.store - DEBUG - find_or_create_exception_url: 
> session=4335326160, session_dirty=IdentitySet([])
> 2011-02-09 13:36:43,666 - surf.store - DEBUG - approve_request after: 
> session=4335326160, session dirty=IdentitySet([])
> 
> Any guidance on this?  

Have to turn on SQL logging.  The request.request_url call could very well be 
autoflushing which is why you'd see nothing in session.dirty subsequent to 
that.The code above doesn't illustrate any other failures to me since I 
don't know what it's supposed to do.   You'd need to narrow down what you think 
is the erroneous behavior on the part of the Session into an 
application-agnostic test script that calls two sample functions.   In the 
likely case that an isolated test works fine, you'd work backwards to see 
what's different between the isolated test and your code.   pdb is always 
helpful here.







> 
> Brent
> 
> On Feb 9, 2011, at 12:09 PM, Michael Bayer wrote:
> 
>> 
>> On Feb 9, 2011, at 12:01 PM, Brent McConnell wrote:
>> 
>

Re: [sqlalchemy] Passing sessions around

2011-02-09 Thread Brent McConnell
Thanks for the quick reply.  I've tried to include more specific info from my 
program for you to look over as well as the log.

def approve_request(self, request_ids, session=None):
if session is None:
session = scopedSession()

try:
for request_id in request_ids:
request = self.get_request(request_id)
if request is None:
raise RequestStoreError("ID does not exist")
#set request to True
request.status = True
session.add(request)
store_logger.debug("approve_reqeuest before: session=%s, 
session dirty=%s" % (id(session), session.dirty))
#add to the corresponding url as exception to standard rules
filter_store.find_or_create_exception_url(request.owned_by_id, 
request.request_url, session=session)
store_logger.debug("approve_request after: session=%s, session 
dirty=%s" % (id(session), session.dirty))
session.commit()
except Exception as e:
print e
return False
finally:
session.close()
return True  

def find_or_create_exception_url(self, fg, url, displayName=None, 
session=None):
if session is None:
session = scopedSession()

result = self.get_exception_url(fg, url)
store_logger.debug("result=%s" % result)
if result is None:
store_logger.debug("result not Found")
banned = self.get_banned_url(fg, url)
if banned is not None:
session.delete(banned)
session.flush()
result = ExceptionUrl(url, fg, displayName)
session.add(result)
store_logger.debug("adding ExceptionUrl=%s", result)

store_logger.debug("find_or_create_exception_url: session=%s, 
session_dirty=%s" % (id(session), session.dirty))
return result

Notice that I have some debug statements in each function.  When the 
approve_request function is called with an array of ids, the logging results 
are below.  The session is always the same which is what is expected but a 
couple of unexpected things happen

1)  When in the find_or_create_exception_url function the IdentifySet of the 
session.dirty is empty even though those records ACTUALLY DO get created in the 
database.
2)  Upon returning to approve_request the IdentifySet is empty which means the 
status of the request is never set to True.

2011-02-09 13:36:42,817 - surf.store - DEBUG - approve_reqeuest before: 
session=4335326160, session dirty=IdentitySet([http://test19.com', '330eee30-381c-48f7-837e-34be7d2cb1c0', 'True', 'None', 
'None', 'None')>])
2011-02-09 13:36:42,985 - surf.store - DEBUG - result=None
2011-02-09 13:36:42,985 - surf.store - DEBUG - result not Found
2011-02-09 13:36:43,116 - surf.store - DEBUG - adding 
ExceptionUrl=http://test19.com','67868150974853021936294845903799103936','None')>
2011-02-09 13:36:43,117 - surf.store - DEBUG - find_or_create_exception_url: 
session=4335326160, session_dirty=IdentitySet([])
2011-02-09 13:36:43,117 - surf.store - DEBUG - approve_request after: 
session=4335326160, session dirty=IdentitySet([])
monitor running
2011-02-09 13:36:43,365 - surf.store - DEBUG - approve_reqeuest before: 
session=4335326160, session dirty=IdentitySet([http://test18.com', '330eee30-381c-48f7-837e-34be7d2cb1c0', 'True', 'None', 
'None', 'None')>])
['186', '185'] approved
2011-02-09 13:36:43,540 - surf.store - DEBUG - result=None
2011-02-09 13:36:43,540 - surf.store - DEBUG - result not Found
2011-02-09 13:36:43,665 - surf.store - DEBUG - adding 
ExceptionUrl=http://test18.com','67868150974853021936294845903799103936','None')>
2011-02-09 13:36:43,666 - surf.store - DEBUG - find_or_create_exception_url: 
session=4335326160, session_dirty=IdentitySet([])
2011-02-09 13:36:43,666 - surf.store - DEBUG - approve_request after: 
session=4335326160, session dirty=IdentitySet([])

Any guidance on this?  

Brent

On Feb 9, 2011, at 12:09 PM, Michael Bayer wrote:

> 
> On Feb 9, 2011, at 12:01 PM, Brent McConnell wrote:
> 
>> I'm having issues with the sqlalchemy session and completely realize this is 
>> from my own lack of understanding but can't seem to find a solution.  My 
>> plan was to create a library with functions like so...
>> 
>> def func1 (id, session=None):
>>  result = 
>>  session.add(something)
>>  return result
>> 
>> def func2 (id, session=None):
>>  result = 
>>  session.add(something)
>>  return result
>> 
>> then from someplace else use the functions like so...
>> 
>> session = scoped_session()
>> result = func1(1, session)
>> result2=func2(2, session)
>> session.commit()
>> session.close()
>> 
>> This doesn't work though.  Do I need to return the session for something 
>> like this to work?  Any advice on the correct pattern to use when trying to 
>> create a tr

Re: [sqlalchemy] Passing sessions around

2011-02-09 Thread Michael Bayer

On Feb 9, 2011, at 12:01 PM, Brent McConnell wrote:

> I'm having issues with the sqlalchemy session and completely realize this is 
> from my own lack of understanding but can't seem to find a solution.  My plan 
> was to create a library with functions like so...
> 
> def func1 (id, session=None):
>   result = 
>   session.add(something)
>   return result
> 
> def func2 (id, session=None):
>   result = 
>   session.add(something)
>   return result
> 
> then from someplace else use the functions like so...
> 
> session = scoped_session()
> result = func1(1, session)
> result2=func2(2, session)
> session.commit()
> session.close()
> 
> This doesn't work though.  Do I need to return the session for something like 
> this to work?  Any advice on the correct pattern to use when trying to create 
> a transaction across several functions?

Looks fine to me.   "doesn't work" doesn't carry a lot of detail.

I'd note that the purpose of scoped_session() is so that "session" is safe to 
use as a global variable, though there's no reason you can't pass it explicitly 
to functions as well.


-- 
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.