[sqlalchemy] Re: "InvalidRequestError: Can't reconnect until invalid transaction is rolled back" error during "SELECT" query

2013-08-20 Thread Jim Carroll
I'm having the same issue with a Pyramid web app.
Did anyone find a solution?

I've tried Flup and Waitress as the containers for the pyramid app, and 
I've tried Lighttpd and Nginx in front... no difference.  My latest 
exceptions look like:

Could not convert return value of the view callable function 
cornice.pyramidhook.handle_exceptions into a response object. The value 
returned was AttributeError("'Connection' object has no attribute 
'_Connection__connection'",).


and then on the next try


ValueError: Could not convert return value of the view callable function 
cornice.pyramidhook.handle_exceptions into a response object. The value 
returned was AssertionError('Transaction must be committed using the 
transaction manager',).



I'm using the latest of everything:

alembic0.6.0amqp1.0.13anyjson0.3.3argparse1.2.1Beaker1.6.4beautifulsoup4
4.2.1billiard2.7.3.31celery3.0.21Chameleon2.11cornice0.14coverage3.6
distribute0.7.3flup1.0.3.dev-20110405kombu2.5.12Mako0.8.1MarkupSafe0.18meld3
0.6.10mock1.0.1MySQL-python1.2.4nose1.3.0openpyxl1.6.2paramiko1.11.0
PasteDeploy1.5.0pip1.1pycrypto2.6Pygments1.6pyramid1.4.3pyramid-beaker0.8
pyramid-debugtoolbar1.0.6pyramid-mailer0.13pyramid-tm0.7Python2.7
python-dateutil2.1pytz2013brepoze.lru0.6repoze.sendmail4.1selenium2.33.0
Server0.0.6setuptools0.9.8simplejson3.3.0six1.3.0SQLAlchemy0.8.2supervisor
3.0transaction1.4.1translationstring1.1venusian1.0a8waitress0.8.5WebOb1.2.3
WebTest2.0.6wsgiref0.1.2zope.deprecation4.0.2zope.interface4.0.5
zope.sqlalchemy0.7.2


-- 
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/groups/opt_out.


[sqlalchemy] Re: InvalidRequestError: get() can only be used against a single mapped class.

2011-12-28 Thread Kent
in fact, I modified our Query class after .first() was being abused
out of laziness:

def first(self):
raise ProgrammingError("Never use .first(); please use .get()
or .one()\n"
"  .one() makes sure there is only one return and .get()
returns None if doesn't exist.\n"
"  .get() is probably better since it avoids database
roundtrip when the object is already loaded.")

-- 
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: InvalidRequestError: get() can only be used against a single mapped class.

2011-12-28 Thread Kent


On Dec 28, 12:07 pm, Michael Bayer  wrote:
> On Dec 28, 2011, at 11:34 AM, Kent wrote:
>
> > Was it your intention to no longer allow this type of query().get()?
>
> > session.query(cls.orderid).get(orderid)
>
> it was !   yes.
>
>
>
> > I get "InvalidRequestError: get() can only be used against a single mapped 
> > class." but the wording is such that I'm not sure you intended to limit 
> > that use case (there is only a single mapped class in that query).
>
> From the POV of that message there's just a Column, and no mapped class, 
> actually being passed.  I knew this wouldn't necessarily be very clear but 
> there was no better message I could think of.
>
>
>
> > I'll change such queries, just wanted to bring it up to see if you intended 
> > it that way.
>
> > (Admittedly, if I recall correctly, when I first added it, I think I was 
> > slightly surprised it worked as I expected...)
>
> amazingly it took me just one second to find the originating issue, which was 
> that the identity map was still being searched, thereby causing the wrong 
> result:
>
> http://www.sqlalchemy.org/trac/ticket/2144

I suspected that subsequent .get() invocations would return just the
column instead of the object, is that what was happening?

>
> When something is used in a way I didn't anticipate, my first instinct is 
> often to first make that unanticipated case raise an error.   That means it's 
> covered and people won't do it.   Later, if we decide it should do something, 
> that can be re-introduced.   It's always easier to unlock a dead end with a 
> well thought out approach, than to make multiple, small modifications to an 
> existing behavior.

That is the right approach, I'm convinced.  I always feel I'm fighting
an eternal battle at work against the other guys' mindset of "make the
program not crash if something unexpected happens, so at least there
is a chance it will keep running..."  Can't stand it.  Things like
"try: catch Exception: pass".  !  It's far better to blow up before
doing damage, even if that damage is only speculative because it
wasn't used in the designed use case.  Later, when that case comes up
you can analyze it and open the dead end, agreed.

Thanks, I've certainly got no issues with the semantics of get()
meaning "get the instance".

-- 
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: InvalidRequestError: Unknown PG numeric type: 1043

2011-05-16 Thread Craig Swank
Indeed!  That's what I get for defining my tables based on the pdf my
manager gave me, instead of looking at the tables in psql directly.
Thanks, I was stuck looking at my code for the problem.

Craig


On May 16, 7:53 am, Michael Bayer  wrote:
> On May 16, 2011, at 9:23 AM, Craig Swank wrote:
>
>
>
>
>
> > Hello,
> > I am declaratively defining a table and am having trouble with adding
> > a float column.  I get this error:
>
> > InvalidRequestError: Unknown PG numeric type: 1043
>
> > when I add a column with either:
>
> > from sqlalchemy.dialects.postgresql import NUMERIC
>
> > or
>
> > from sqlalchemy import Float
>
> > and the column definition looks like:
>
> > turbine_ok_hours = Column(Float(5))
>
> > I am using sqlalchemy-0.6.7, posgresql 9.0, psycopg2--2.4.
>
> So that particular type needs to check the PostgreSQL type code in order to 
> know how to handle what's in the result set.   The DBAPI, usually psycopg2, 
> sends back this code within every result set.  1043 as it turns out is 
> VARCHAR, so can't be handled numerically.   Your turbine_ok_hours is likely a 
> VARCHAR.
>
> pg8000's source code has a nice table of all these values:
>
> # py type -> pg array typeoid
> py_array_types = {
>     float: 1022,
>     bool: 1000,
>     str: 1009,      # TEXT[]
>     unicode: 1009,  # TEXT[]
>     decimal.Decimal: 1231, # NUMERIC[]
>
> }
>
> pg_types = {
>     16: {"bin_in": boolrecv},
>     17: {"bin_in": bytearecv},
>     19: {"bin_in": varcharin}, # name type
>     20: {"bin_in": int8recv},
>     21: {"bin_in": int2recv},
>     23: {"bin_in": int4recv},
>     25: {"bin_in": varcharin}, # TEXT type
>     26: {"txt_in": numeric_in}, # oid type
>     700: {"bin_in": float4recv},
>     701: {"bin_in": float8recv},
>     829: {"txt_in": varcharin}, # MACADDR type
>     1000: {"bin_in": array_recv}, # BOOL[]
>     1003: {"bin_in": array_recv}, # NAME[]
>     1005: {"bin_in": array_recv}, # INT2[]
>     1007: {"bin_in": array_recv}, # INT4[]
>     1009: {"bin_in": array_recv}, # TEXT[]
>     1014: {"bin_in": array_recv}, # CHAR[]
>     1015: {"bin_in": array_recv}, # VARCHAR[]
>     1016: {"bin_in": array_recv}, # INT8[]
>     1021: {"bin_in": array_recv}, # FLOAT4[]
>     1022: {"bin_in": array_recv}, # FLOAT8[]
>     1042: {"bin_in": varcharin}, # CHAR type
>     1043: {"bin_in": varcharin}, # VARCHAR type
>     1082: {"txt_in": date_in},
>     1083: {"txt_in": time_in},
>     1114: {"bin_in": timestamp_recv},
>     1184: {"bin_in": timestamptz_recv}, # timestamp w/ tz
>     1186: {"bin_in": interval_recv},
>     1231: {"bin_in": array_recv}, # NUMERIC[]
>     1263: {"bin_in": array_recv}, # cstring[]
>     1700: {"bin_in": numeric_recv},
>     2275: {"bin_in": varcharin}, # cstring
>
>
>
> }
>
> > This is the 6th table I've added so far for this project, and all the
> > others are working fine.  Any idea what may be wrong?  I'm stumped.
>
> > Thanks,
>
> > Craig
>
> > --
> > 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 
> > athttp://groups.google.com/group/sqlalchemy?hl=en.

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



Re: [sqlalchemy] Re: InvalidRequestError

2011-03-14 Thread Michael Bayer

On Mar 14, 2011, at 6:14 PM, Ajay wrote:

> Thanks Mike for your quick response.
> 
> I removed the zope.sqlalchemy extension and I am no longer getting the
> zope error.
> 
> Since we are using a scoped_session, we don't think it's a threading
> issue although we are running in a multi-threaded env.
> 
> I am trying to create this exception to occur in my test env.
> 
> What state do we have to leave the Session object in - inorder to
> generate an 'InvalidRequestError' exception.
> Again, assuming that it is not a multithreaded concurrency issue given
> I am using sessions produced from ScopedSession.
> 
> My assumption is that since I am using ScopedSession, each thread gets
> it's own session object and session objects are not shared among
> threads.
> 
> Again appreciate your help with this.

the issue is just when an error occurs during flush, which results in a 
rollback of the transaction - the Session is then left in a state where it 
requires that you call rollback().   Any operations with the session before 
rollback is called will emit this exception.The condition is not inherently 
related to threading, but can be a side effect of concurrent access to a single 
Session.






> 
> thanks!
> 
> Ajay
> 
> 
> 
> 
> On Mar 12, 11:38 am, Michael Bayer  wrote:
>> On Mar 11, 2011, at 7:51 PM, AJAY PATTNI wrote:
>> 
>> 
>> 
>>> We use sqlalchemy 0.4.6 with Elixir 0.5.2 and zope.sqlalchemy
>> 
>>> Every once a while we get this error(see below):
>>> In a previous thread somebody said, just 'rollback your session when
>>> the exception is raised'?
>> 
>>> We use a global session as follows:
>> 
>>> __session__ =
>>> scoped_session(sessionmaker(twophase=False,transactional=True,autoflush=True,extension=ZopeTransactionExtension()))
>> 
>>> All the rest of the transactional machinery is handled by Elixiry
>>> Entity object.
>> 
>>> So my question - how can we rollback in this situation?
>> 
>>> We tried to do __session__.remove() which seems to work but then zope
>>> seems to have a problem after it completes the request.
>> 
>> A description of what this error means from the perspective of SQLAlchemy 
>> only, not that of zope.sqlalchemy for which you should consult their mailing 
>> list, is here:
>> 
>>
>> http://www.sqlalchemy.org/trac/wiki/FAQ#Thetransactionisinactivedueto...
>> 
>> However, note that version 0.4.6 of SQLAlchemy is extremely old, and the 
>> mechanics of session and transaction have been largely reworked since then.  
>>  The basic idea that a rollback needs to occur is consistent in that 
>> version, however.
>> 
>> Its very likely that some artifact of zope.sqlalchemy is involved in the 
>> production of your error here.
>> 
>>> ==
>>> 2011-03-11T10:41:22 ERROR Zope.SiteErrorLog
>>> http://dragon.initiatesystems.com:8080/ati/txm_inject_orm
>>> Traceback (innermost last):
>>>  Module ZPublisher.Publish, line 121, in publish
>>>  Module Zope2.App.startup, line 238, in commit
>>>  Module transaction._manager, line 89, in commit
>>>  Module transaction._transaction, line 329, in commit
>>>  Module transaction._transaction, line 446, in _commitResources
>>>  Module zope.sqlalchemy.datamanager, line 76, in tpc_vote
>>>  Module sqlalchemy.orm.session, line 263, in commit
>>>  Module sqlalchemy.orm.session, line 176, in _assert_is_open
>>> InvalidRequestError: The transaction is closed
>>> =
>> 
>>> Appreciate any help I can get on this.
>> 
>>> 
>>> Mar 4 12:56:23 pbrk4.den05.accenx.com [err]
>>> https://acm.accenx.com/mapsrvcluster/ati/acm/tdm/group/19150/broker/2...
>>> (innermost last):#012 Module ZPublisher.Publish, line 115, in
>>> publish#012 Module ZPublisher.mapply, line 88, in mapply#012 Module
>>> ZPublisher.Publish, line 41, in call_object#012 Module
>>> Shared.DC.Scripts.Bindings, line 313, in __call__#012 Module
>>> Shared.DC.Scripts.Bindings, line 350, in _bindAndExec#012 Module
>>> Products.PythonScripts.PythonScript, line 326, in _exec#012 Module
>>> None, line 90, in processMsg#012 - >> processMsg used for /ati/acm/tdm/group/19150/broker/25150/site/
>>> 705077435>#012 - Line 90#012 Module Shared.DC.Scripts.Bindings, line
>>> 313, in __call__#012 Module Shared.DC.Scripts.Bindings, line 350, in
>>> _bindAndExec#012 Module Products.PythonScripts.PythonScript, line 326,
>>> in _exec#012 Module None, line 131, in translate#012 - >> at /ati/acm/tdm/group/19150/broker/25150/site/705077435/outbound/
>>> translate>#012 - Line 131#012 Module Shared.DC.Scripts.Bindings, line
>>> 313, in __call__#012 Module Shared.DC.Scripts.Bindings, line 350, in
>>> _bindAndExec#012 Module Products.PythonScripts.PythonScript, line 326,
>>> in _exec#012 Module None, line 99, in callTxm#012 - >> ati/acm/tdm/group/19150/broker/25150/site/705077435/outbound/
>>> callTxm>#012 - Line 99#012 Module Shared.DC.Scripts.Bindings, line
>>> 313, in __call__#012 Module Shared.DC.Scripts.Bindings, line 350, in
>>> _bindAndExec#012 Module Products

[sqlalchemy] Re: InvalidRequestError

2011-03-14 Thread Ajay
Thanks Mike for your quick response.

I removed the zope.sqlalchemy extension and I am no longer getting the
zope error.

Since we are using a scoped_session, we don't think it's a threading
issue although we are running in a multi-threaded env.

I am trying to create this exception to occur in my test env.

What state do we have to leave the Session object in - inorder to
generate an 'InvalidRequestError' exception.
Again, assuming that it is not a multithreaded concurrency issue given
I am using sessions produced from ScopedSession.

My assumption is that since I am using ScopedSession, each thread gets
it's own session object and session objects are not shared among
threads.

Again appreciate your help with this.

thanks!

Ajay




On Mar 12, 11:38 am, Michael Bayer  wrote:
> On Mar 11, 2011, at 7:51 PM, AJAY PATTNI wrote:
>
>
>
> > We use sqlalchemy 0.4.6 with Elixir 0.5.2 and zope.sqlalchemy
>
> > Every once a while we get this error(see below):
> > In a previous thread somebody said, just 'rollback your session when
> > the exception is raised'?
>
> > We use a global session as follows:
>
> > __session__ =
> > scoped_session(sessionmaker(twophase=False,transactional=True,autoflush=True,extension=ZopeTransactionExtension()))
>
> > All the rest of the transactional machinery is handled by Elixiry
> > Entity object.
>
> > So my question - how can we rollback in this situation?
>
> > We tried to do __session__.remove() which seems to work but then zope
> > seems to have a problem after it completes the request.
>
> A description of what this error means from the perspective of SQLAlchemy 
> only, not that of zope.sqlalchemy for which you should consult their mailing 
> list, is here:
>
>        
> http://www.sqlalchemy.org/trac/wiki/FAQ#Thetransactionisinactivedueto...
>
> However, note that version 0.4.6 of SQLAlchemy is extremely old, and the 
> mechanics of session and transaction have been largely reworked since then.   
> The basic idea that a rollback needs to occur is consistent in that version, 
> however.
>
> Its very likely that some artifact of zope.sqlalchemy is involved in the 
> production of your error here.
>
> > ==
> > 2011-03-11T10:41:22 ERROR Zope.SiteErrorLog
> >http://dragon.initiatesystems.com:8080/ati/txm_inject_orm
> > Traceback (innermost last):
> >  Module ZPublisher.Publish, line 121, in publish
> >  Module Zope2.App.startup, line 238, in commit
> >  Module transaction._manager, line 89, in commit
> >  Module transaction._transaction, line 329, in commit
> >  Module transaction._transaction, line 446, in _commitResources
> >  Module zope.sqlalchemy.datamanager, line 76, in tpc_vote
> >  Module sqlalchemy.orm.session, line 263, in commit
> >  Module sqlalchemy.orm.session, line 176, in _assert_is_open
> > InvalidRequestError: The transaction is closed
> > =
>
> > Appreciate any help I can get on this.
>
> > 
> > Mar 4 12:56:23 pbrk4.den05.accenx.com [err]
> >https://acm.accenx.com/mapsrvcluster/ati/acm/tdm/group/19150/broker/2...
> > (innermost last):#012 Module ZPublisher.Publish, line 115, in
> > publish#012 Module ZPublisher.mapply, line 88, in mapply#012 Module
> > ZPublisher.Publish, line 41, in call_object#012 Module
> > Shared.DC.Scripts.Bindings, line 313, in __call__#012 Module
> > Shared.DC.Scripts.Bindings, line 350, in _bindAndExec#012 Module
> > Products.PythonScripts.PythonScript, line 326, in _exec#012 Module
> > None, line 90, in processMsg#012 -  > processMsg used for /ati/acm/tdm/group/19150/broker/25150/site/
> > 705077435>#012 - Line 90#012 Module Shared.DC.Scripts.Bindings, line
> > 313, in __call__#012 Module Shared.DC.Scripts.Bindings, line 350, in
> > _bindAndExec#012 Module Products.PythonScripts.PythonScript, line 326,
> > in _exec#012 Module None, line 131, in translate#012 -  > at /ati/acm/tdm/group/19150/broker/25150/site/705077435/outbound/
> > translate>#012 - Line 131#012 Module Shared.DC.Scripts.Bindings, line
> > 313, in __call__#012 Module Shared.DC.Scripts.Bindings, line 350, in
> > _bindAndExec#012 Module Products.PythonScripts.PythonScript, line 326,
> > in _exec#012 Module None, line 99, in callTxm#012 -  > ati/acm/tdm/group/19150/broker/25150/site/705077435/outbound/
> > callTxm>#012 - Line 99#012 Module Shared.DC.Scripts.Bindings, line
> > 313, in __call__#012 Module Shared.DC.Scripts.Bindings, line 350, in
> > _bindAndExec#012 Module Products.PythonScripts.PythonScript, line 326,
> > in _exec#012 Module None, line 44, in txm_ampiWrapperOrm#012 -
> >  > BrokerMapFunctions/TxmFunctions/txm_ampiWrapperOrm>#012 - Line 44#012
> > Module Shared.DC.Scripts.Bindings, line 313, in __call__#012 Module
> > Shared.DC.Scripts.Bindings, line 350, in _bindAndExec#012 Module
> > Products.PythonScripts.PythonScript, line 326, in _exec#012 Module
> > None, line 22, in getTransactionTypes#012 -  > tdm/group/19150/broker/25150/BrokerMapFunctions/TxmFunctions/
> > getTransactionTypes>#012 - Line 22#012 

[sqlalchemy] Re: InvalidRequestError

2009-06-12 Thread Michael Bayer

chingi wrote:
>
> Hello,
>
>  In my application I have AJAX function which sends around 20
> requests every 5 seconds to Server to update my web page.
>
>  But few requests fail to get values because of
> "invalidRequestError : The transaction is inactive due to a rollback
> in a subtransaction and should be closed"
>
> After investigtion I found that  "SessionTransaction" class which is
> responsible for creating sessions is not thread safe and i guess that
> this is creating the problem.
>
> Please help me  to sort out this issue . Any Help is appreciated.


this is correct, Session and its internals are documented as
non-threadsafe.  To manage sessions on a per thread basis, you should be
using the ScopedSession class as described in
http://www.sqlalchemy.org/docs/05/session.html#contextual-thread-local-sessions
.

--~--~-~--~~~---~--~~
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: "InvalidRequestError: The transaction is inactive due to a rollback..." using sqlite with multiple commits.

2009-04-30 Thread Daniel

Hello Michael,

Thank you for your replies.  I've just tried to create a testcase but
proven to myself that it does raise an exception.  I'll go back and
double check where I might be catching and passing on the exception.

Daniel

On Apr 29, 3:19 pm, Michael Bayer  wrote:
> how is it that you know this is due to the SQLite timeout  ?  did you  
> create a test case ?  creating a fully reproducible test case would be  
> the next step.
>
> On Apr 29, 2009, at 5:15 PM, Daniel wrote:
>
>
>
> > I'm not catching it or re-raising it.  Where else could I look to
> > solve this.
>
> > On Apr 29, 11:57 am, "Michael Bayer"  wrote:
> >> Daniel wrote:
>
> >>> I've learned a bit more.  Apparently the sqlite database  
> >>> occasionally
> >>> gets locked by another process and that lock lasts longer than the
> >>> five second default timeout.  SQL Alchemy quietly issues a ROLLBACK,
> >>> but doesn't say anything more about it.  The result is that the
> >>> session is no longer active and eventually produces the error I
> >>> mentioned above.  Here are the log messages showing the insert  
> >>> failing
> >>> after the five second default timeout.
>
> >>> 2009-04-28 15:10:48,118 INFO sqlalchemy.engine.base.Engine.0x...ae70
> >>> INSERT INTO table ("id", "value") VALUES (?, ?)
> >>> 2009-04-28 15:10:48,118 INFO sqlalchemy.engine.base.Engine.0x...ae70
> >>> [1, 'my value']
> >>> 2009-04-28 15:10:53,148 INFO sqlalchemy.engine.base.Engine.0x...ae70
> >>> ROLLBACK
>
> >>> Is there some way to configure SQL Alchemy to raise an exception  
> >>> when
> >>> it issues the rollback, or when it finds the database locked?
>
> >> SQLAlchemy definitely raises an exception if the DBAPI sends one.  a
> >> ROLLBACK does not occur if there was no exception thrown.   check if
> >> you're catching it and not re-raising.
--~--~-~--~~~---~--~~
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: "InvalidRequestError: The transaction is inactive due to a rollback..." using sqlite with multiple commits.

2009-04-29 Thread Michael Bayer

how is it that you know this is due to the SQLite timeout  ?  did you  
create a test case ?  creating a fully reproducible test case would be  
the next step.


On Apr 29, 2009, at 5:15 PM, Daniel wrote:

>
> I'm not catching it or re-raising it.  Where else could I look to
> solve this.
>
> On Apr 29, 11:57 am, "Michael Bayer"  wrote:
>> Daniel wrote:
>>
>>> I've learned a bit more.  Apparently the sqlite database  
>>> occasionally
>>> gets locked by another process and that lock lasts longer than the
>>> five second default timeout.  SQL Alchemy quietly issues a ROLLBACK,
>>> but doesn't say anything more about it.  The result is that the
>>> session is no longer active and eventually produces the error I
>>> mentioned above.  Here are the log messages showing the insert  
>>> failing
>>> after the five second default timeout.
>>
>>> 2009-04-28 15:10:48,118 INFO sqlalchemy.engine.base.Engine.0x...ae70
>>> INSERT INTO table ("id", "value") VALUES (?, ?)
>>> 2009-04-28 15:10:48,118 INFO sqlalchemy.engine.base.Engine.0x...ae70
>>> [1, 'my value']
>>> 2009-04-28 15:10:53,148 INFO sqlalchemy.engine.base.Engine.0x...ae70
>>> ROLLBACK
>>
>>> Is there some way to configure SQL Alchemy to raise an exception  
>>> when
>>> it issues the rollback, or when it finds the database locked?
>>
>> SQLAlchemy definitely raises an exception if the DBAPI sends one.  a
>> ROLLBACK does not occur if there was no exception thrown.   check if
>> you're catching it and not re-raising.
> >


--~--~-~--~~~---~--~~
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: "InvalidRequestError: The transaction is inactive due to a rollback..." using sqlite with multiple commits.

2009-04-29 Thread Daniel

I'm not catching it or re-raising it.  Where else could I look to
solve this.

On Apr 29, 11:57 am, "Michael Bayer"  wrote:
> Daniel wrote:
>
> > I've learned a bit more.  Apparently the sqlite database occasionally
> > gets locked by another process and that lock lasts longer than the
> > five second default timeout.  SQL Alchemy quietly issues a ROLLBACK,
> > but doesn't say anything more about it.  The result is that the
> > session is no longer active and eventually produces the error I
> > mentioned above.  Here are the log messages showing the insert failing
> > after the five second default timeout.
>
> > 2009-04-28 15:10:48,118 INFO sqlalchemy.engine.base.Engine.0x...ae70
> > INSERT INTO table ("id", "value") VALUES (?, ?)
> > 2009-04-28 15:10:48,118 INFO sqlalchemy.engine.base.Engine.0x...ae70
> > [1, 'my value']
> > 2009-04-28 15:10:53,148 INFO sqlalchemy.engine.base.Engine.0x...ae70
> > ROLLBACK
>
> > Is there some way to configure SQL Alchemy to raise an exception when
> > it issues the rollback, or when it finds the database locked?
>
> SQLAlchemy definitely raises an exception if the DBAPI sends one.  a
> ROLLBACK does not occur if there was no exception thrown.   check if
> you're catching it and not re-raising.
--~--~-~--~~~---~--~~
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: "InvalidRequestError: The transaction is inactive due to a rollback..." using sqlite with multiple commits.

2009-04-29 Thread Michael Bayer

Daniel wrote:
>
> I've learned a bit more.  Apparently the sqlite database occasionally
> gets locked by another process and that lock lasts longer than the
> five second default timeout.  SQL Alchemy quietly issues a ROLLBACK,
> but doesn't say anything more about it.  The result is that the
> session is no longer active and eventually produces the error I
> mentioned above.  Here are the log messages showing the insert failing
> after the five second default timeout.
>
> 2009-04-28 15:10:48,118 INFO sqlalchemy.engine.base.Engine.0x...ae70
> INSERT INTO table ("id", "value") VALUES (?, ?)
> 2009-04-28 15:10:48,118 INFO sqlalchemy.engine.base.Engine.0x...ae70
> [1, 'my value']
> 2009-04-28 15:10:53,148 INFO sqlalchemy.engine.base.Engine.0x...ae70
> ROLLBACK
>
> Is there some way to configure SQL Alchemy to raise an exception when
> it issues the rollback, or when it finds the database locked?

SQLAlchemy definitely raises an exception if the DBAPI sends one.  a
ROLLBACK does not occur if there was no exception thrown.   check if
you're catching it and not re-raising.



--~--~-~--~~~---~--~~
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: "InvalidRequestError: The transaction is inactive due to a rollback..." using sqlite with multiple commits.

2009-04-29 Thread Daniel

I've learned a bit more.  Apparently the sqlite database occasionally
gets locked by another process and that lock lasts longer than the
five second default timeout.  SQL Alchemy quietly issues a ROLLBACK,
but doesn't say anything more about it.  The result is that the
session is no longer active and eventually produces the error I
mentioned above.  Here are the log messages showing the insert failing
after the five second default timeout.

2009-04-28 15:10:48,118 INFO sqlalchemy.engine.base.Engine.0x...ae70
INSERT INTO table ("id", "value") VALUES (?, ?)
2009-04-28 15:10:48,118 INFO sqlalchemy.engine.base.Engine.0x...ae70
[1, 'my value']
2009-04-28 15:10:53,148 INFO sqlalchemy.engine.base.Engine.0x...ae70
ROLLBACK

Is there some way to configure SQL Alchemy to raise an exception when
it issues the rollback, or when it finds the database locked?  It's a
bit confusing to see a ROLLBACK with no indication of why (and if I
didn't know that the sqlite3 default timeout was five seconds, I might
still be scratching my head).

Meanwhile, my application works with an increased timeout, but that
may change when the system is under high load.

Thanks for your input...

On Apr 28, 6:27 pm, Michael Bayer  wrote:
> squelching typically means one of two things.
>
> either you're doing this:
>
> try:
>      # do stuff with session
> except:
>     print "error !"
>     # .. keep going
>
> or, you are allowing concurrent access to a single session with  
> multiple threads, one of your threads is throwing an exception  
> (usually due to the corrupted state of the session, since the session  
> is not mutexed) and the other thread gets this error.
>
> On Apr 28, 2009, at 4:53 PM, Daniel wrote:
>
>
>
> > Hello,
>
> > In my application I have a function that looks more or less like this
>
> >    def run(self):
> >        # process first object in sequence
> >        for firstObject in firstObjects:
> >            self.session.add(firstObject)
> >        self.session.commit()
>
> >        # process second object in sequence
> >        # lots of these, so break along the way
> >        count = 0
> >        for secondObject in secondObjects:
> >            self.session.add(secondObject)
> >            count += 1
> >            if (count > 100):
> >                #**
> >                self.session.commit()
> >                #**
> >                count = 0
> >                time.sleep(1)   # pause to let other process access
> > the db
> >        self.session.commit()
>
> >        # process third objects
> >        for thirdObject in thirdObjects:
> >            self.session.add(thirdObject)
> >        self.session.commit()
>
> > The commit nested inside the second loop (highlighted by asterisks) is
> > potentially called many times (occasionally there are thousands of
> > objects to deal with).  intermittently that commit will produce the
> > following error:
>
> > Traceback (most recent call last):
> >  File "C:\Aptina\pop\tester\AptinaStagingService.py", line 106, in
> > __init__
> >    self.run(self.pushPath,self.stagingPath)
> >  File "C:\Aptina\pop\tester\AptinaStagingService.py", line 231, in
> > run
> >    self.session.commit()
> >  File "c:\python25\Lib\site-packages\sqlalchemy\orm\session.py", line
> > 673, in commit
> >    self.transaction.commit()
> >  File "c:\python25\Lib\site-packages\sqlalchemy\orm\session.py", line
> > 378, in commit
> >    self._prepare_impl()
> >  File "c:\python25\Lib\site-packages\sqlalchemy\orm\session.py", line
> > 351, in _prepare_impl
> >    self._assert_is_active()
> >  File "c:\python25\Lib\site-packages\sqlalchemy\orm\session.py", line
> > 247, in _assert_is_active
> >    "The transaction is inactive due to a rollback in a "
> > InvalidRequestError: The transaction is inactive due to a rollback in
> > a subtransaction.  Issue rollback() to cancel the transaction.
>
> > I've read elsewhere in this group (http://groups.google.com/group/
> > sqlalchemy/browse_thread/thread/b87af73232998fe4) about this error
> > message, but I'm not sure what they mean by "squelching the original
> > exception somewhere".  Can someone please help me understand why I'm
> > getting this error and ideas on how to fix it.
>
> > Thanks.
--~--~-~--~~~---~--~~
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: "InvalidRequestError: The transaction is inactive due to a rollback..." using sqlite with multiple commits.

2009-04-28 Thread Michael Bayer

squelching typically means one of two things.

either you're doing this:

try:
 # do stuff with session
except:
print "error !"
# .. keep going

or, you are allowing concurrent access to a single session with  
multiple threads, one of your threads is throwing an exception  
(usually due to the corrupted state of the session, since the session  
is not mutexed) and the other thread gets this error.


On Apr 28, 2009, at 4:53 PM, Daniel wrote:

>
> Hello,
>
> In my application I have a function that looks more or less like this
>
>def run(self):
># process first object in sequence
>for firstObject in firstObjects:
>self.session.add(firstObject)
>self.session.commit()
>
># process second object in sequence
># lots of these, so break along the way
>count = 0
>for secondObject in secondObjects:
>self.session.add(secondObject)
>count += 1
>if (count > 100):
>#**
>self.session.commit()
>#**
>count = 0
>time.sleep(1)   # pause to let other process access
> the db
>self.session.commit()
>
># process third objects
>for thirdObject in thirdObjects:
>self.session.add(thirdObject)
>self.session.commit()
>
> The commit nested inside the second loop (highlighted by asterisks) is
> potentially called many times (occasionally there are thousands of
> objects to deal with).  intermittently that commit will produce the
> following error:
>
> Traceback (most recent call last):
>  File "C:\Aptina\pop\tester\AptinaStagingService.py", line 106, in
> __init__
>self.run(self.pushPath,self.stagingPath)
>  File "C:\Aptina\pop\tester\AptinaStagingService.py", line 231, in
> run
>self.session.commit()
>  File "c:\python25\Lib\site-packages\sqlalchemy\orm\session.py", line
> 673, in commit
>self.transaction.commit()
>  File "c:\python25\Lib\site-packages\sqlalchemy\orm\session.py", line
> 378, in commit
>self._prepare_impl()
>  File "c:\python25\Lib\site-packages\sqlalchemy\orm\session.py", line
> 351, in _prepare_impl
>self._assert_is_active()
>  File "c:\python25\Lib\site-packages\sqlalchemy\orm\session.py", line
> 247, in _assert_is_active
>"The transaction is inactive due to a rollback in a "
> InvalidRequestError: The transaction is inactive due to a rollback in
> a subtransaction.  Issue rollback() to cancel the transaction.
>
> I've read elsewhere in this group (http://groups.google.com/group/
> sqlalchemy/browse_thread/thread/b87af73232998fe4) about this error
> message, but I'm not sure what they mean by "squelching the original
> exception somewhere".  Can someone please help me understand why I'm
> getting this error and ideas on how to fix it.
>
> Thanks.
>
> >


--~--~-~--~~~---~--~~
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: InvalidRequestError and ConcurrentModification problems

2008-10-28 Thread Michael Bayer


On Oct 28, 2008, at 1:57 PM, Doug Farrell wrote:

>
> Michael,
>
> I'm not sure why the ConcurrentModification is occurring at all, my
> application doesn't use threads. I'll look into issuing the rollback()
> call and see how that helps my app.

the "concurrent" ness of the error only means two different actors  
deleted the same row.  they can be in the same thread.

> this module to create their own session objects. By the way, with
> locally scoped session variables should I do a session.close() or not?

sessions close themselves implicitly when they fall out of scope.   
close() is useful just to ensure the connection resources and objects  
are freed immediately.


--~--~-~--~~~---~--~~
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?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: InvalidRequestError and ConcurrentModification problems

2008-10-28 Thread Doug Farrell

Michael,

I'm not sure why the ConcurrentModification is occurring at all, my
application doesn't use threads. I'll look into issuing the rollback()
call and see how that helps my app.

I'm sure there are better ways to structure my code and use of SA, this
is the result of prototyping along in an effort to learn, understand and
use SA. The session.expire_all() was injected recently as the result of
reading some stuff during a Google Search and just casting about to
solve the thrown exceptions. I also had a job = session.merge(job) in
there at one time. I have one module called managerdb.py where I create
the engine and the first session object. All other modules reference
this module to create their own session objects. By the way, with
locally scoped session variables should I do a session.close() or not?

Thanks,
Doug

> -Original Message-
> From: sqlalchemy@googlegroups.com [mailto:[EMAIL PROTECTED]
> On Behalf Of Michael Bayer
> Sent: Tuesday, October 28, 2008 10:20 AM
> To: sqlalchemy@googlegroups.com
> Subject: [sqlalchemy] Re: InvalidRequestError and
> ConcurrentModification problems
> 
> 
> the message you're getting is due to an exception being raised,but
> rollback() not being called on the session.   When you catch those
> ConcurrentModification exceptions, you have to issue a rollback().
> 
> I dont see anything else with the code that would suggest the same row
> being deleted in two places, although it does seem like theres likely
> a more succinct way of structuring that code, and I also dont see the
> purpose that the session.expire_all() accomplishes.   Things missing
> here include what scope is the session managed under (i.e. where is it
> created/destroyed/shared among threads) as well as what other
> concurrency exists within this section of the application.
> 
> On Oct 27, 2008, at 9:15 PM, Doug Farrell wrote:
> 
> >
> > Hi all,
> >
> > I'm using Python 2.5.1 with SqlAlchemy 0.5rc2 with Sqlite on Windows
> > Server 2003 and I'm having a problem with my application throwing
> > InvalidRequestError and ConcurrentModification exceptions. Here is
my
> > simplified declarative class:
> >
> > class Job(srsmanagerdb.Base):
> >STATUS_INIT = 0
> >STATUS_RUN = 1
> >STATUS_DONE = 2
> >STATUS_FAIL = 3
> >__tablename__ = "jobs"
> >id= Column(Integer, primary_key=True,
> > autoincrement=True)
> >nas   = Column(String(12), default=None)
> >filename  = Column(String(64), default=None, index=True)
> >filesize  = Column(Integer, default=None)
> >created   = Column(DateTime, default=None)
> >job_id= Column(String(32), default=None)
> >productType   = Column(String(1), default=None)
> >contentType   = Column(String(10), default=None)
> >priorityType  = Column(String(10), default=None)
> >priority  = Column(Integer, default=None)
> >assignedPress = Column(Integer, default=None)
> >status= Column(Integer, default=None)
> >
> >def __init__(self, objrefs, fileDetails):
> >nas, filename, filesize, ctime = fileDetails
> >self.nas = nas
> >self.filename = filename
> >self.filesize = filesize
> >self.created =
> > datetime.datetime(*time.strptime(ctime[:ctime.find(".")], "%Y-%m-%d
> > %H:%M:%S")[0:6])
> >
> > This object is used to track state information about jobs being
> > handled
> > by a looping state machine. I keep a list of all active jobs in a
> Jobs
> > collection class, so there are many active intances of the above
> > class.
> > The simplified Jobs collection class looks like this:
> >
> > class Jobs(AppContext):
> >def __init__(self, objrefs):
> >self._logger = __logger__
> >self._jobs = []
> >self._markedForRemoval = []
> >def markForRemoval(self, job):
> > self._markedForRemoval.append(job)
> >def removeMarkedJobs(self):  # throws exception
in
> > here
> >session = srsmanagerdb.Session()
> >for markedJob in self._markedForRemoval:
> >try:
> >session.expire_all()
> >session.delete(markedJob)
> >session.commit()
> >self._jobs.remove(markedJob)
> >except sqlalchemy.exceptions.ConcurrentModificationError,
> > e:
> >self._logger.warn("%s threw exception %s" %
> > (job.filename, e))
> >self._markedForRemoval = []
> >

[sqlalchemy] Re: InvalidRequestError and ConcurrentModification problems

2008-10-28 Thread Michael Bayer

the message you're getting is due to an exception being raised,but  
rollback() not being called on the session.   When you catch those  
ConcurrentModification exceptions, you have to issue a rollback().

I dont see anything else with the code that would suggest the same row  
being deleted in two places, although it does seem like theres likely  
a more succinct way of structuring that code, and I also dont see the  
purpose that the session.expire_all() accomplishes.   Things missing  
here include what scope is the session managed under (i.e. where is it  
created/destroyed/shared among threads) as well as what other  
concurrency exists within this section of the application.

On Oct 27, 2008, at 9:15 PM, Doug Farrell wrote:

>
> Hi all,
>
> I'm using Python 2.5.1 with SqlAlchemy 0.5rc2 with Sqlite on Windows
> Server 2003 and I'm having a problem with my application throwing
> InvalidRequestError and ConcurrentModification exceptions. Here is my
> simplified declarative class:
>
> class Job(srsmanagerdb.Base):
>STATUS_INIT = 0
>STATUS_RUN = 1
>STATUS_DONE = 2
>STATUS_FAIL = 3
>__tablename__ = "jobs"
>id= Column(Integer, primary_key=True,
> autoincrement=True)
>nas   = Column(String(12), default=None)
>filename  = Column(String(64), default=None, index=True)
>filesize  = Column(Integer, default=None)
>created   = Column(DateTime, default=None)
>job_id= Column(String(32), default=None)
>productType   = Column(String(1), default=None)
>contentType   = Column(String(10), default=None)
>priorityType  = Column(String(10), default=None)
>priority  = Column(Integer, default=None)
>assignedPress = Column(Integer, default=None)
>status= Column(Integer, default=None)
>
>def __init__(self, objrefs, fileDetails):
>nas, filename, filesize, ctime = fileDetails
>self.nas = nas
>self.filename = filename
>self.filesize = filesize
>self.created =
> datetime.datetime(*time.strptime(ctime[:ctime.find(".")], "%Y-%m-%d
> %H:%M:%S")[0:6])
>
> This object is used to track state information about jobs being  
> handled
> by a looping state machine. I keep a list of all active jobs in a Jobs
> collection class, so there are many active intances of the above  
> class.
> The simplified Jobs collection class looks like this:
>
> class Jobs(AppContext):
>def __init__(self, objrefs):
>self._logger = __logger__
>self._jobs = []
>self._markedForRemoval = []
>def markForRemoval(self, job):
>   self._markedForRemoval.append(job)
>def removeMarkedJobs(self):  # throws exception in
> here
>session = srsmanagerdb.Session()
>for markedJob in self._markedForRemoval:
>try:
>session.expire_all()
>session.delete(markedJob)
>session.commit()
>self._jobs.remove(markedJob)
>except sqlalchemy.exceptions.ConcurrentModificationError,  
> e:
>self._logger.warn("%s threw exception %s" %
> (job.filename, e))
>self._markedForRemoval = []
>def process(self):
> for job for self._jobs:
>job.process()
>if job.status == Job.STATUS_DONE:
>self.markForRemoval(job)
>self.removeMarkedJobs()
>
> The above simplified code runs for awhile (10s of minutes) with  
> hundreds
> of jobs and then it throws the exception below in the  
> removeMarkedJobs()
> method. I've worked really hard trying to figure out what's going  
> wrong
> here. This is the only place where I delete jobs and commit that  
> delete
> to the database. One question I have is if it's a good idea to keep a
> list of active Job instances (database rows) in a Python list. In my
> removeMarkedJobs() I'm deleting the job instances, and then removing  
> the
> job instance from the list. Is this necessary or good practice? I
> haven't figured out if just deleting the job instance from the list
> (self._jobs.remove(markedJob)) will also delete the job from the
> database or not. Anyway, here's the traceback of the exception I'm
> getting. Any help would be appreciated.
>
> Thanks, Doug
>
> 2008-10-27 18:15:54 srsmanager   ERRORunexpected error,  
> restarting:
> Traceback (most recent call last):
> File "c:\cygwin\home\ripadmin\dev\srsmanager\srsprocess.py", line 154,
> in runjobs isActive = self._jobs.process()
> File "c:\cygwin\home\ripadmin\dev\srsmanager\jobs.py", line 436, in
> process self.removeMarkedJobs()
> File "c:\cygwin\home\ripadmin\dev\srsmanager\jobs.py", line 397, in
> removeMarkedJobs self._logger.warn("%s threw exception %s" %
> (markedJob.filename, e))
> File
> "c:\python\2.5\lib\site-packages\SQLAlchemy-0.5.0rc1-py2.5.egg 
> \sqlalchem
> y\orm\attributes.py", line 135, in __get__ return
> self.impl.get(instance_state(instance))
> File
> "c:\python\2.5\lib\site-packages\

[sqlalchemy] Re: InvalidRequestError

2008-02-08 Thread Paul Johnston

Hi,

>I do a session.clear(), all time, after of
>session.save_or_update([obj])
>Can this be the problem?
>  
>
That is almost certainly your problem. Try removing the session.clear() 
and see if it then works.

>When is advisable do a session.clear() ?
>  
>
When you're done with a batch of processing. In web applications this 
fits nicely at the end of a request.

Paul

--~--~-~--~~~---~--~~
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?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: InvalidRequestError when I try to save object

2008-01-27 Thread dezhong liu
hello, how are you,i use the delete() method ,but it have the same
exception, i see that you use the save_or_update() method , i look for the
document, but can't find a right method to do it,
thanks


2008/1/15, Rick Morrison <[EMAIL PROTECTED]>:
>
> Well, the my_service.get_people() most likely uses a query to retrieve
> those people from the database, right? That query would have the effect of
> putting those results into the session as persistent objects. When you call
> session.save() on one of those already-persisted objects, the session
> checks to see if that object is already being tracked. It is, because it was
> just loaded from the database as a persistent object.  So,that triggers
> the error.
>
> If you know for sure that the person object was loaded from a query, then
> there's no need to .save() the object at all: it's already being tracked.
>
> However, there is no harm in using session.save_or_update (), as that
> would check to see if the object was already persisted, find that it was,
> and then do nothing anyway.
>
>
> >
>

--~--~-~--~~~---~--~~
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?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: InvalidRequestError

2008-01-20 Thread VitaminJ

Thanks for the great support and all the work!

Merge is exactly what I had in mind. The documentation for merge()
states what I was looking for:
"This method is useful for bringing in objects which may have been
restored from a serialization,
such as those stored in an HTTP session, where the object may be
present in the session already"

Thanks, Jan



--~--~-~--~~~---~--~~
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?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: InvalidRequestError

2008-01-20 Thread Michael Bayer


On Jan 20, 2008, at 10:03 AM, VitaminJ wrote:

>
> Hi!
>
> Is there a way to find out about the state an object is in? I am
> particular interested to find out if there is the need to call
> session.update() for an object or if the identidy is already contained
> in the session. I cannot manage to do this, but get an Invalid Request
> Errror.
>
> So the example to get the InvalidRequestErrro goes as follows:
>
>session = Session()
>obj1 = session.query(Advertisment).get(1)
>session.expunge(obj1)
>obj2 = session.query(Advertisment).get(1)
>session.update(obj1)  # raises InvalidRequestError
>
> I would like to do something like:
>
> if not obj1 in session:
>session.update(obj1)
>
> Any help would be appreciated very much.
> Thanks, Jan

well actually you *can* do exactly "if obj1 not in session:  
session.update(obj1)", but what that does is tell you if "obj1"  
specifically is in the session, not any object with the same identity  
as obj1.

if you want to check for a certain identity, use the identity_map  
dictionary directly, using "session.identity_key(instance=obj1) in  
session.idenitity_map".

But i think what suits your case above even better is merge(); if you  
wanted to turn "obj1" into "obj2", and copy all the changes present on  
obj1 into the session, you could say:

obj1 = session.merge(obj1)

the merge() call will return the current persistent object if present,  
else will load it from the database using obj1's identity.  In both  
cases all of obj1's attributes are merged into the persistent instance.




--~--~-~--~~~---~--~~
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?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: InvalidRequestError when I try to save object

2008-01-14 Thread Rick Morrison
Well, the my_service.get_people() most likely uses a query to retrieve those
people from the database, right? That query would have the effect of putting
those results into the session as persistent objects. When you call
session.save() on one of those already-persisted objects, the session checks
to see if that object is already being tracked. It is, because it was just
loaded from the database as a persistent object.  So,that triggers the
error.

If you know for sure that the person object was loaded from a query, then
there's no need to .save() the object at all: it's already being tracked.

However, there is no harm in using session.save_or_update(), as that would
check to see if the object was already persisted, find that it was, and then
do nothing anyway.

--~--~-~--~~~---~--~~
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?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: InvalidRequestError when I try to save object

2008-01-14 Thread maxi

But, is my design mistake ?
I must use ever save_or_update()  function?

In my case, I have two process working over the same "class of
bussines object".
I do a query, which return me an object list and I call to other
function which pass one parameter (id) and this function do other
query over the same class.
i.e.:

Table -> Person

person_list = my_service.get_people()
for p in person_list:
process_person(p.id)


def process_person(id):
 person = my_service.get_person()
 
 do something with person
 

 session.save(person)   # here, raise exception:
InvalidRequestError: Instance... already persistent
 session.flush([person])


Now, I chage session.save_or_update(person), and this work fine.




















--~--~-~--~~~---~--~~
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?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: InvalidRequestError when I try to save object

2008-01-14 Thread Rick Morrison
flush() works exactly as before.

the difference that Mike is pointing out is that 0.3 allowed
already-persisted objects to be re-added, which was a bug that is now fixed.
Using save_or_update() instead of save() will help you avoid triggering the
error when the object you're trying to .save() is already added.

--~--~-~--~~~---~--~~
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?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: InvalidRequestError when I try to save object

2008-01-14 Thread maxi

I must use save_update alone or save_update + flush ?





--~--~-~--~~~---~--~~
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?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: InvalidRequestError when I try to save object

2008-01-14 Thread Michael Bayer


On Jan 14, 2008, at 2:32 PM, maxi wrote:

>
> Hi,
> Follwing with my 0.3 to 0.4 upgrade, now I get the next problem...
>
> When I try to save an object across my session, I get an
> InvalidRequestError exception with the message:
>
>  File "c:\programs\python25\lib\site-packages\sqlalchemy-0.4.2p3-
> py2.5.egg\sqlalchemy\orm\session.py", line 988, in _save_impl
>raise exceptions.InvalidRequestError("Instance '%s' is already
> persistent" % mapperutil.instance_str(instance))
> InvalidRequestError: Instance '[EMAIL PROTECTED]' is already
> persistent
>
>
> Why with 0.3 I had not this error?

use session.save_or_update() for an instance where you arent sure if  
its persistent or not.  this is the same API as in 0.3, 0.3 just has a  
bug where no error is raised.


--~--~-~--~~~---~--~~
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?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: InvalidRequestError: This transaction is inactive

2007-02-13 Thread Mike the Unsung Hero



On Jan 22, 1:56 pm, Jose Soares <[EMAIL PROTECTED]> wrote:
> Hello,
>
> Could someone please, explain me what's wrong with thistransaction?
> 
>  File 
> "/usr/lib/python2.4/site-packages/TurboGears-1.0-py2.4.egg/turbogears/database.py",
>  line 303, in sa_rwt>   transaction.commit()
>   File "build/bdist.linux-i686/egg/sqlalchemy/orm/session.py", line 54, in 
> commit
>   File "build/bdist.linux-i686/egg/sqlalchemy/engine/base.py", line 389, in 
> commitInvalidRequestError: Thistransactionisinactive
> -
>
> def save_user(data,user,anagrafica):
>transaction= session.create_transaction()
>try:
>for f in user.ruoli:
>session.delete(f)
>session.flush()
>for item in data.get('dettaglio'):
>if item.get('cod_ruolo'):
>user.ruoli.append(UserGroup(
>   id_operatore = item.get('id_operatore'),
>   )
>)
>user.data_inizio_attivita = data.get('data_inizio_attivita')
>anagrafica.nome   = data.get('display_name')
>session.save(user)
>session.save(anagrafica)
>session.flush()
>transaction.commit()
>except:
>transaction.rollback()
>
> jo





Jose, there has been some discussion of this on the TurboGears Google
group.
The thread is at

http://groups.google.com/group/turbogears/browse_thread/thread/a81bfa81143c3a82

(SQLAlchemy and run_with_transaction)

Essentially, the @expose() decorator wraps the session you are using
in a
transaction (sa_rwt) within which the controller method runs.  When
the
controller raises an exception and rolls back, the commit() that
happens after the controller code block is trying to commit an already
rolled back
transaction.  The solution I went (from the TurboGears group
discussion)
was to create a decorator to use with my controller methods that need
transactions that overrides this "feature" of @expose().

Mike S.


--~--~-~--~~~---~--~~
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?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: InvalidRequestError: This transaction is inactive

2007-01-24 Thread Michael Bayer

then its likely a threading issue.  not sure what your setup is but  
you should make sure that the session is used in only a single  
thread, typically via using a thread local variable (or using  
SessionContext which provides this service).


On Jan 24, 2007, at 8:28 AM, Jose Soares wrote:

>
> Michael Bayer ha scritto:
>> not sure about that, i thought maybe the multiple flush()es are
>> breaking something but I just added a test case and it doesnt
>> reproduce.  make sure youre on the most recent versions since that  
>> was
>> broken a few versions back...
>>
>>
> This error happens some times, not every time.
> my versione is SQLAlchemy-0.3.0
>
> jo
>
>
>
> >


--~--~-~--~~~---~--~~
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?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: InvalidRequestError: This transaction is inactive

2007-01-24 Thread Jose Soares

Jose Soares ha scritto:
> Michael Bayer ha scritto:
>   
>> not sure about that, i thought maybe the multiple flush()es are
>> breaking something but I just added a test case and it doesnt
>> reproduce.  make sure youre on the most recent versions since that was
>> broken a few versions back...
>>
>>   
>> 
> This error happens some times, not every time.
> my versione is SQLAlchemy-0.3.0
>
> jo
>
>
>   
Hi Mike,
I upgraded to 0.3.4
but this function works only without create_transaction()...

def add_user(self,data):
#transaction = session.create_transaction()
try:
anagrafica = Anagrafica(
nome = data.get('display_name'),
email= data.get('email'),
)
session.save(anagrafica)
session.flush()
user = User(
id_anagrafica= anagrafica.id, #ultimo record 
inserito in anagrafica
data_inizio_attivita = data.get('data_inizio_attivita'),
data_fine_attivita   = data.get('data_fine_attivita'),
)
for item in data.get('dettaglio'):
if item.get('cod_ruolo'):
user.ruoli.append(UserGroup(
   id_operatore = item.get('id_operatore'),
   group_id = item.get('cod_ruolo'),
   id_asl   = item.get('id_asl'),
   )
)
session.save(user)
session.flush()
#transaction.commit()
return "inserito il record %d!"%user.id
except:
#transaction.rollback()


jo

--~--~-~--~~~---~--~~
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?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: InvalidRequestError: This transaction is inactive

2007-01-24 Thread Jose Soares

Michael Bayer ha scritto:
> not sure about that, i thought maybe the multiple flush()es are
> breaking something but I just added a test case and it doesnt
> reproduce.  make sure youre on the most recent versions since that was
> broken a few versions back...
>
>   
This error happens some times, not every time.
my versione is SQLAlchemy-0.3.0

jo



--~--~-~--~~~---~--~~
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?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: InvalidRequestError: This transaction is inactive

2007-01-22 Thread Michael Bayer

not sure about that, i thought maybe the multiple flush()es are
breaking something but I just added a test case and it doesnt
reproduce.  make sure youre on the most recent versions since that was
broken a few versions back...


--~--~-~--~~~---~--~~
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?hl=en
-~--~~~~--~~--~--~---