peewee.OperationalError: server closed the connection unexpectedly

1,298 views
Skip to first unread message

seyed...@noaa.gov

unread,
Oct 18, 2016, 6:51:03 PM10/18/16
to peewee-orm
Similar to this post

I am getting the following error


[2016-10-18 13:22:50,350] ERROR in wsgi: Application exception:
Traceback (most recent call last):
 
File "/var/www/wsgi-scripts/BudgetDB/miniconda3/envs/budgetdb-py34-env/lib/python3.4/site-packages/playhouse/postgres_ext.py", line 374, in execute_sql
    cursor
.execute(sql, params or ())
psycopg2
.InternalError: current transaction is aborted, commands ignored until end of transaction block




During handling of the above exception, another exception occurred:


Traceback (most recent call last):
 
File "/var/www/wsgi-scripts/BudgetDB/miniconda3/envs/budgetdb-py34-env/lib/python3.4/site-packages/weppy/wsgi.py", line 94, in error_handler
   
return static_handler(app, environ, start_response)
 
File "/var/www/wsgi-scripts/BudgetDB/miniconda3/envs/budgetdb-py34-env/lib/python3.4/site-packages/weppy/wsgi.py", line 86, in static_handler
   
return dynamic_handler(app, environ, start_response)
 
File "/var/www/wsgi-scripts/BudgetDB/miniconda3/envs/budgetdb-py34-env/lib/python3.4/site-packages/weppy/wsgi.py", line 35, in dynamic_handler
    app
.route.dispatch()
 
File "/var/www/wsgi-scripts/BudgetDB/miniconda3/envs/budgetdb-py34-env/lib/python3.4/site-packages/weppy/expose.py", line 231, in dispatch
    route
.func(**reqargs)
 
File "/var/www/wsgi-scripts/BudgetDB/miniconda3/envs/budgetdb-py34-env/lib/python3.4/site-packages/weppy/handlers.py", line 24, in handled_function
    output
= handler.wrap_call(f)(*args, **kwargs)
 
File "/var/www/wsgi-scripts/BudgetDB/miniconda3/envs/budgetdb-py34-env/lib/python3.4/site-packages/weppy/expose.py", line 257, in wrap
    output
= func(*args, **kwargs)
 
File "/var/www/wsgi-scripts/BudgetDB/miniconda3/envs/budgetdb-py34-env/lib/python3.4/site-packages/weppy/handlers.py", line 24, in handled_function
    output
= handler.wrap_call(f)(*args, **kwargs)
 
File "/var/www/wsgi-scripts/BudgetDB/miniconda3/envs/budgetdb-py34-env/lib/python3.4/site-packages/weppy/handlers.py", line 24, in handled_function
    output
= handler.wrap_call(f)(*args, **kwargs)
 
File "/var/www/wsgi-scripts/BudgetDB/webapp/budget_ldap.py", line 34, in login
    LDAP_connect_redirect
(connection,form.params.username,form.params.password)
 
File "/var/www/wsgi-scripts/BudgetDB/webapp/budget_ldap.py", line 111, in LDAP_connect_redirect
    check_lockout
(username)
 
File "/var/www/wsgi-scripts/BudgetDB/webapp/budget_ldap.py", line 91, in check_lockout
    user
= Users.select().where(Users.user_name == username).get()
 
File "/var/www/wsgi-scripts/BudgetDB/miniconda3/envs/budgetdb-py34-env/lib/python3.4/site-packages/peewee.py", line 3090, in get
   
return next(clone.execute())
 
File "/var/www/wsgi-scripts/BudgetDB/miniconda3/envs/budgetdb-py34-env/lib/python3.4/site-packages/peewee.py", line 3142, in execute
   
self._qr = ResultWrapper(model_class, self._execute(), query_meta)
 
File "/var/www/wsgi-scripts/BudgetDB/miniconda3/envs/budgetdb-py34-env/lib/python3.4/site-packages/peewee.py", line 2826, in _execute
   
return self.database.execute_sql(sql, params, self.require_commit)
 
File "/var/www/wsgi-scripts/BudgetDB/miniconda3/envs/budgetdb-py34-env/lib/python3.4/site-packages/playhouse/postgres_ext.py", line 381, in execute_sql
   
self.commit()
 
File "/var/www/wsgi-scripts/BudgetDB/miniconda3/envs/budgetdb-py34-env/lib/python3.4/site-packages/peewee.py", line 3507, in __exit__
    reraise
(new_type, new_type(*exc_args), traceback)
 
File "/var/www/wsgi-scripts/BudgetDB/miniconda3/envs/budgetdb-py34-env/lib/python3.4/site-packages/peewee.py", line 134, in reraise
   
raise value.with_traceback(tb)
 
File "/var/www/wsgi-scripts/BudgetDB/miniconda3/envs/budgetdb-py34-env/lib/python3.4/site-packages/playhouse/postgres_ext.py", line 374, in execute_sql
    cursor
.execute(sql, params or ())
peewee
.InternalError: current transaction is aborted, commands ignored until end of transaction block




[2016-10-18 15:31:22,707] ERROR in wsgi: Application exception:
Traceback (most recent call last):
 
File "/var/www/wsgi-scripts/BudgetDB/miniconda3/envs/budgetdb-py34-env/lib/python3.4/site-packages/playhouse/postgres_ext.py", line 374, in execute_sql
    cursor
.execute(sql, params or ())
psycopg2
.OperationalError: server closed the connection unexpectedly
       
This probably means the server terminated abnormally
        before
or while processing the request.




During handling of the above exception, another exception occurred:


Traceback (most recent call last):
 
File "/var/www/wsgi-scripts/BudgetDB/miniconda3/envs/budgetdb-py34-env/lib/python3.4/site-packages/weppy/wsgi.py", line 94, in error_handler
   
return static_handler(app, environ, start_response)
 
File "/var/www/wsgi-scripts/BudgetDB/miniconda3/envs/budgetdb-py34-env/lib/python3.4/site-packages/weppy/wsgi.py", line 86, in static_handler
   
return dynamic_handler(app, environ, start_response)
 
File "/var/www/wsgi-scripts/BudgetDB/miniconda3/envs/budgetdb-py34-env/lib/python3.4/site-packages/weppy/wsgi.py", line 35, in dynamic_handler
    app
.route.dispatch()
 
File "/var/www/wsgi-scripts/BudgetDB/miniconda3/envs/budgetdb-py34-env/lib/python3.4/site-packages/weppy/expose.py", line 231, in dispatch
    route
.func(**reqargs)
 
File "/var/www/wsgi-scripts/BudgetDB/miniconda3/envs/budgetdb-py34-env/lib/python3.4/site-packages/weppy/handlers.py", line 24, in handled_function
    output
= handler.wrap_call(f)(*args, **kwargs)
 
File "/var/www/wsgi-scripts/BudgetDB/miniconda3/envs/budgetdb-py34-env/lib/python3.4/site-packages/weppy/expose.py", line 257, in wrap
    output
= func(*args, **kwargs)
 
File "/var/www/wsgi-scripts/BudgetDB/miniconda3/envs/budgetdb-py34-env/lib/python3.4/site-packages/weppy/handlers.py", line 24, in handled_function
    output
= handler.wrap_call(f)(*args, **kwargs)
 
File "/var/www/wsgi-scripts/BudgetDB/miniconda3/envs/budgetdb-py34-env/lib/python3.4/site-packages/weppy/handlers.py", line 24, in handled_function
    output
= handler.wrap_call(f)(*args, **kwargs)
 
File "/var/www/wsgi-scripts/BudgetDB/webapp/budget_ldap.py", line 34, in login
    LDAP_connect_redirect
(connection,form.params.username,form.params.password)
 
File "/var/www/wsgi-scripts/BudgetDB/webapp/budget_ldap.py", line 111, in LDAP_connect_redirect
    check_lockout
(username)
 
File "/var/www/wsgi-scripts/BudgetDB/webapp/budget_ldap.py", line 91, in check_lockout
    user
= Users.select().where(Users.user_name == username).get()
 
File "/var/www/wsgi-scripts/BudgetDB/miniconda3/envs/budgetdb-py34-env/lib/python3.4/site-packages/peewee.py", line 3090, in get
   
return next(clone.execute())
 
File "/var/www/wsgi-scripts/BudgetDB/miniconda3/envs/budgetdb-py34-env/lib/python3.4/site-packages/peewee.py", line 3142, in execute
   
self._qr = ResultWrapper(model_class, self._execute(), query_meta)
 
File "/var/www/wsgi-scripts/BudgetDB/miniconda3/envs/budgetdb-py34-env/lib/python3.4/site-packages/peewee.py", line 2826, in _execute
   
return self.database.execute_sql(sql, params, self.require_commit)
 
File "/var/www/wsgi-scripts/BudgetDB/miniconda3/envs/budgetdb-py34-env/lib/python3.4/site-packages/playhouse/postgres_ext.py", line 381, in execute_sql
   
self.commit()
 
File "/var/www/wsgi-scripts/BudgetDB/miniconda3/envs/budgetdb-py34-env/lib/python3.4/site-packages/peewee.py", line 3507, in __exit__
    reraise
(new_type, new_type(*exc_args), traceback)
 
File "/var/www/wsgi-scripts/BudgetDB/miniconda3/envs/budgetdb-py34-env/lib/python3.4/site-packages/peewee.py", line 134, in reraise
   
raise value.with_traceback(tb)
 
File "/var/www/wsgi-scripts/BudgetDB/miniconda3/envs/budgetdb-py34-env/lib/python3.4/site-packages/playhouse/postgres_ext.py", line 374, in execute_sql
    cursor
.execute(sql, params or ())
peewee
.OperationalError: server closed the connection unexpectedly
       
This probably means the server terminated abnormally


I'm using Weppy framework and for database connection I'm doing the following in a Python master file that is imported in my models python file I import the "database" variable
database = PooledPostgresqlExtDatabase('DB', max_connections=8, stale_timeout=300,register_hstore=False, **{'user': 'USERNAME', 'host': 'HOST', 'password': 'PASSWORD', 'port': PORT})

 then my main application python files import the peewee models as such
from models import X,Y,Z


I don't automatically open or close any DB connection and the application runs fine for a day or two then throws that error repeatedly. From the other Bottle thread I could not figure out how to fix my problem...any thoughts?



Charles Leifer

unread,
Oct 18, 2016, 8:53:14 PM10/18/16
to peewe...@googlegroups.com
It's hard to say what might be happening, but if you don't manage your connections then the pool is not helping you.  I'm not familiar with the "weppy" framework but I imagine there's a way to add pre/post request hooks. It's a very, very common pattern since the Python server is essentially stateful for the duration of it's process lifetime.

I'd suggest you look into adding request hooks and going from there.

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

seyed...@noaa.gov

unread,
Oct 18, 2016, 11:07:55 PM10/18/16
to peewee-orm
Hi Charles,

Yes there are pre/post request hooks in Weppy. My question is what would I do in the pre/post hooks? 
Do I literally do this? (as shown here http://docs.peewee-orm.com/en/latest/peewee/database.html)

def before_request_handler():
    database.connect()

def after_request_handler():
    database.close()
To unsubscribe from this group and stop receiving emails from it, send an email to peewee-orm+...@googlegroups.com.

seyed...@noaa.gov

unread,
Oct 18, 2016, 11:08:37 PM10/18/16
to peewee-orm
And instead of connection pool, use the following?

db = PostgresqlDatabase(
    'database_name',  # Required by Peewee.
    user='postgres',  # Will be passed directly to psycopg2.
    password='secret',  # Ditto.
    host='db.mysite.com',  # Ditto.
)

 
File <span style="color:#080"

Charles Leifer

unread,
Oct 18, 2016, 11:12:33 PM10/18/16
to peewe...@googlegroups.com

Yep! That's it. Whether you're using the pool or just plain PostgresqlDatabase, peewee will handle it correctly.


To unsubscribe from this group and stop receiving emails from it, send an email to peewee-orm+unsubscribe@googlegroups.com.

seyed...@noaa.gov

unread,
Oct 18, 2016, 11:52:14 PM10/18/16
to peewee-orm
Awesome, thanks Charles.

seyed...@noaa.gov

unread,
Oct 20, 2016, 3:11:56 PM10/20/16
to peewee-orm
Hi Charles,

I'm thinking of switching from Connection Pool to the plain postgresdatabase to hopefully fix this issue that I'm getting now

[2016-10-20 12:10:36,914] ERROR in wsgi: Application exception:

Traceback (most recent call last):

 
File "/var/www/wsgi-scripts/BudgetDB/miniconda3/envs/budgetdb-py34-env/lib/python3.4/site-packages/weppy/wsgi.py", line 94, in error_handler
   
return static_handler(app, environ, start_response)
 
File "/var/www/wsgi-scripts/BudgetDB/miniconda3/envs/budgetdb-py34-env/lib/python3.4/site-packages/weppy/wsgi.py", line 86, in static_handler
   
return dynamic_handler(app, environ, start_response)
 
File "/var/www/wsgi-scripts/BudgetDB/miniconda3/envs/budgetdb-py34-env/lib/python3.4/site-packages/weppy/wsgi.py", line 35, in dynamic_handler
    app
.route.dispatch()
 
File "/var/www/wsgi-scripts/BudgetDB/miniconda3/envs/budgetdb-py34-env/lib/python3.4/site-packages/weppy/expose.py", line 231, in dispatch
    route
.func(**reqargs)
 
File "/var/www/wsgi-scripts/BudgetDB/miniconda3/envs/budgetdb-py34-env/lib/python3.4/site-packages/weppy/handlers.py", line 24, in handled_function
    output
= handler.wrap_call(f)(*args, **kwargs)
 
File "/var/www/wsgi-scripts/BudgetDB/miniconda3/envs/budgetdb-py34-env/lib/python3.4/site-packages/weppy/expose.py", line 257, in wrap
    output
= func(*args, **kwargs)
 
File "/var/www/wsgi-scripts/BudgetDB/miniconda3/envs/budgetdb-py34-env/lib/python3.4/site-packages/weppy/handlers.py", line 24, in handled_function
    output
= handler.wrap_call(f)(*args, **kwargs)

 
File "/var/www/wsgi-scripts/BudgetDB/miniconda3/envs/budgetdb-py34-env/lib/python3.4/site-packages/weppy/handlers.py", line 23, in handled_function
    handler
.on_start()
 
File "/var/www/wsgi-scripts/BudgetDB/webapp/budget.py", line 56, in on_start
    database
.connect()
 
File "/var/www/wsgi-scripts/BudgetDB/miniconda3/envs/budgetdb-py34-env/lib/python3.4/site-packages/peewee.py", line 3593, in connect
   
**self.connect_kwargs)
 
File "/var/www/wsgi-scripts/BudgetDB/miniconda3/envs/budgetdb-py34-env/lib/python3.4/site-packages/playhouse/pool.py", line 133, in _connect
   
raise ValueError('Exceeded maximum connections.')
ValueError: Exceeded maximum connections.

Reply all
Reply to author
Forward
0 new messages