On Thu, Aug 15, 2019, at 10:28 AM, Abhishek Sharma wrote:
> One of my project is built around Django rest framework and Sqlalchemy as ORM.
> 
> This application is thread based so randomly we are seeing one of thread 
> transaction showing connection closed error.
> 
> Randomly when my application is trying to insert the record in table we are 
> seeing exception the resource is closed, Below is stack trace:
> 
> "./modules/managers/contracts/contracts_manager.py", line 745, in 
> submit_fix_contract#012 deal_amn_contract_activity_id = 
> self.save_contract_activity_data(deal_contract_id,fix_request,response.text, 
> 'Submitted', 'Contract Submitted')#012 File 
> "/opt/www/dt_deal_products_api/env/lib/python2.7/site-packages/dtplatform/common/transaction.py",
>  line 123, in inner#012 self.session.commit()#012 File 
> "/opt/www/dt_deal_products_api/env/lib/python2.7/site-packages/sqlalchemy/orm/session.py",
>  line 813, in commit#012 self.transaction.commit()#012 File 
> "/opt/www/dt_deal_products_api/env/lib/python2.7/site-packages/sqlalchemy/orm/session.py",
>  line 392, in commit#012 self._prepare_impl()#012 File 
> "/opt/www/dt_deal_products_api/env/lib/python2.7/site-packages/sqlalchemy/orm/session.py",
>  line 372, in _prepare_impl#012 self.session.flush()#012 File 
> "/opt/www/dt_deal_products_api/env/lib/python2.7/site-packages/sqlalchemy/orm/session.py",
>  line 2027, in flush#012 self._flush(objects)#012 File 
> "/opt/www/dt_deal_products_api/env/lib/python2.7/site-packages/sqlalchemy/orm/session.py",
>  line 2145, in _flush#012 transaction.rollback(_capture_exception=True)#012 
> File 
> "/opt/www/dt_deal_products_api/env/lib/python2.7/site-packages/sqlalchemy/util/langhelpers.py",
>  line 63, in __exit__#012 compat.reraise(type_, value, traceback)#012 File 
> "/opt/www/dt_deal_products_api/env/lib/python2.7/site-packages/sqlalchemy/orm/session.py",
>  line 2145, in _flush#012 transaction.rollback(_capture_exception=True)#012 
> File 
> "/opt/www/dt_deal_products_api/env/lib/python2.7/site-packages/sqlalchemy/orm/session.py",
>  line 408, in rollback#012 self._assert_active(prepared_ok=True, 
> rollback_ok=True)#012 File 
> "/opt/www/dt_deal_products_api/env/lib/python2.7/site-packages/sqlalchemy/orm/session.py",
>  line 223, in _assert_active#012 raise 
> sa_exc.ResourceClosedError(closed_msg)#012ResourceClosedError: This 
> transaction is closed
> 
> So my application is closing the connection after every commit or rollback so 
> as per my understanding after connection close that connection will be return 
> to pool for further use then why sqlalchemy is saying connection close in 
> next transaction.
> 
> Is this error due to alchemy connection pool returning expired connection 
> when requesting due to application is ideal for certain amount of time or 
> connection is expiring at transaction time.

in a threaded application, where the behavior is non-deterministic, this error 
is typically due to sharing a Connection, Session or ORM mapped object across 
threads in some way; the transaction was ended in some thread while another 
tries to continue to work with it. It also might be possible to get this error 
due to exception handling within a rollback that tries to work with the 
connection after the transaction has already ended. I can't provide much more 
information than that without a reproducing example, unfortunately, however 
please also share the database driver you're using as that is often related to 
this kind of thing.




> 
> -- 
> SQLAlchemy - 
> The Python SQL Toolkit and Object Relational Mapper
> 
> http://www.sqlalchemy.org/
> 
> To post example code, please provide an MCVE: Minimal, Complete, and 
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full 
> description.
> --- 
> 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 [email protected].
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sqlalchemy/51595dbe-5574-41ff-8fb7-05b17c9abbee%40googlegroups.com.
> 

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/651c6fc0-5755-4a8d-9a16-ec20d04f4562%40www.fastmail.com.

Reply via email to