On Sep 22, 2014, at 12:14 PM, Milind Vaidya <kava...@gmail.com> wrote:

> HI 
> 
> I have following code,
> 
> 
> 
> 
>     @db_error_handler
>     def execute_query(self, query_str, data=None):
>         '''Execute DB query and retry for common DB interaction errors'''
>         errors_concerned = [1213, 1205, 2013, 2006]
>         attempts = 1 
>         while(True):
>             try:
>                 result = self.session.execute(query_str, data)
>                 #result.close() : Trying to commonize insert/select
>                 self.session.commit()
>                 return result
>             except OperationalError as oe: 
>                 self.session.rollback()
>                 if oe.orig[0] in errors_concerned:
>                     if attempts <= 3:
>                         logger.error("Error occured in attempts %s: %s" % 
> (attempts, oe.orig))
>                         time.sleep(2 * attempts)
>                     attempts += 1
>                         continue
>                     else:
>                         logger.error("Error occured, attempts exhausted %s: 
> %s" % (attempts, oe.orig))
>             break
>                 else:
>             raise
> 
> 
> I am constructing the queries outside and passing to this function. I want to 
> return result in case of select queries. But I can not do that if I call 
> result.close() before returning. Without closing the result, insert queries 
> have a problem.
> 
> ERROR: DB operation failure: (ProgrammingError) (2014, "Commands out of sync; 
> you can't run this command now") None None

if you want database operations entirely self contained in a function like 
that, you have to copy the result set into a list and return the list.   an 
active result set is part of the state of the transaction in progress 
otherwise.    the error you see there is due to MySQLDb's particularly bad 
handling of errant cursor state outside the scope of a transaction that's been 
ended, but in any case once the transaction is complete you can't be calling 
methods on cursors.


> 
> 
> So I was thinking of using 
> 
> is_insert¶
> True if this ResultProxy is the result of a executing an expression language 
> compiled expression.insert() construct.
> 
> When True, this implies that the inserted_primary_key attribute is 
> accessible, assuming the statement did not include a user defined "returning" 
> construct.
> 
> 
> to check if the query resulted in insert and then call close skip otherwise ?

a ResultProxy that came from an insert statement is already closed 
automatically, unless you called returning() on the insert() construct 
explicitly.  there's should be no reason to call result.close() for an insert.  
 your errors are probably due to passing out cursors with SELECT results still 
on them which you try to consume after the transaction has been ended on the 
parent connection.





-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to