Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Reconnecting after a database restart [RSS Feed]

#1 Jan. 10, 2011 15:26:43

Brennan S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Reconnecting after a database restart


Hi folks,

We're using the Django database layer as part of a Twisted application
, and we're having some problems with Django failing to reconnect to
the database after the database server restarts. It works great
otherwise, but once the database server restarts, the Django database
layer throws exceptions on every database touch. Is there any way to
configure Django (or psycopg2) to automatically reconnect?

The traceback we're seeing is below . The problem is reliably
reproducible by restarting Postgres, then triggering an action in our
Twisted application that touches the database. It persists until we
restart the application.

Versions:
Django 1.2.1
Psycopg2 2.0.13
Postgresql 8.4.5
Ubuntu 10.04

Thanks,

-Brennan

We're planning on adding a real Django app in the near future, and
wanted to maintain the same database abstraction throughout the system.

Traceback:

File "/usr/lib/pymodules/python2.6/gjeter/executive/Executive.py", line
164, in robotPair
p.save()
File "/usr/lib/pymodules/python2.6/django/db/models/base.py", line 435, in save
self.save_base(using=using, force_insert=force_insert,
force_update=force_update)
File "/usr/lib/pymodules/python2.6/django/db/models/base.py", line 528, in
save_base
result = manager._insert(values, return_id=update_pk, using=using)
File "/usr/lib/pymodules/python2.6/django/db/models/manager.py", line 195, in
_insert
return insert_query(self.model, values, **kwargs)
File "/usr/lib/pymodules/python2.6/django/db/models/query.py", line 1479, in
insert_query
return query.get_compiler(using=using).execute_sql(return_id)
File "/usr/lib/pymodules/python2.6/django/db/models/sql/compiler.py", line
783, in execute_sql
cursor = super(SQLInsertCompiler, self).execute_sql(None)
File "/usr/lib/pymodules/python2.6/django/db/models/sql/compiler.py", line
727, in execute_sql
cursor.execute(sql, params)
File "/usr/lib/pymodules/python2.6/django/db/backends/util.py", line 15, in
execute
return self.cursor.execute(sql, params)
File
"/usr/lib/pymodules/python2.6/django/db/backends/postgresql_psycopg2/base.py",
line 44, in execute
return self.cursor.execute(query, args)
django.db.utils.DatabaseError: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Jan. 10, 2011 17:09:59

Euan G.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Reconnecting after a database restart


As far as I know Django maintains a persistent connection to the
database server and restarting the server without restarting the
client isn't possible. We never restart our live database server in
production (we fail over to a secondary server so that the app is
essentially only vulnerable for a few seconds whilst the failover
happens and a subsequent app restart). I think the only thing you can
do is to restart the app as soon as you've restarted the DB server.

All that said, it may be possible to get Django to reconnect, but from
reading the source in the DB layer, I don't recall seeing anything
like that.

On Jan 10, 3:25 pm, Brennan Sellner <bsell...@seegrid.com> wrote:
> Hi folks,
>
> We're using the Django database layer as part of a Twisted application
> , and we're having some problems with Django failing to reconnect to
> the database after the database server restarts.  It works great
> otherwise, but once the database server restarts, the Django database
> layer throws exceptions on every database touch.  Is there any way to
> configure Django (or psycopg2) to automatically reconnect?
>
> The traceback we're seeing is below .  The problem is reliably
> reproducible by restarting Postgres, then triggering an action in our
> Twisted application that touches the database.  It persists until we
> restart the application.
>
> Versions:
>   Django 1.2.1
>   Psycopg2 2.0.13
>   Postgresql 8.4.5
>   Ubuntu 10.04
>
> Thanks,
>
> -Brennan
>
> We're planning on adding a real Django app in the near future, and
> wanted to maintain the same database abstraction throughout the system.
>
> Traceback:
>
> File "/usr/lib/pymodules/python2.6/gjeter/executive/Executive.py", line
> 164, in robotPair
>    p.save()
>  File "/usr/lib/pymodules/python2.6/django/db/models/base.py", line 435, in
> save
>    self.save_base(using=using, force_insert=force_insert,
> force_update=force_update)
>  File "/usr/lib/pymodules/python2.6/django/db/models/base.py", line 528, in
> save_base
>    result = manager._insert(values, return_id=update_pk, using=using)
>  File "/usr/lib/pymodules/python2.6/django/db/models/manager.py", line 195,
> in _insert
>    return insert_query(self.model, values, **kwargs)
>  File "/usr/lib/pymodules/python2.6/django/db/models/query.py", line 1479, in
> insert_query
>    return query.get_compiler(using=using).execute_sql(return_id)
>  File "/usr/lib/pymodules/python2.6/django/db/models/sql/compiler.py", line
> 783, in execute_sql
>    cursor = super(SQLInsertCompiler, self).execute_sql(None)
>  File "/usr/lib/pymodules/python2.6/django/db/models/sql/compiler.py", line
> 727, in execute_sql
>    cursor.execute(sql, params)
>  File "/usr/lib/pymodules/python2.6/django/db/backends/util.py", line 15, in
> execute
>    return self.cursor.execute(sql, params)
>  File
> "/usr/lib/pymodules/python2.6/django/db/backends/postgresql_psycopg2/base.py",
> line 44, in execute
>    return self.cursor.execute(query, args)
> django.db.utils.DatabaseError: server closed the connection unexpectedly
> This probably means the server terminated abnormally
> before or while processing the request.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 Jan. 10, 2011 17:46:53

Jirka V.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Reconnecting after a database restart


Hi Brennan,

You can force Django to reopen the database connection by:

>>> from django.db import connection
>>> connection.close()

Django will then automatically reconnect to the database when it needs
it. Obviously, it's up to you to figure out where to put this code in
your application (it might be difficult). And, I just realized that I
don't know what happens if the database is down when you call
connection.close(), so you'd have to test it.

(the code is not multi-db aware, but you can easily adapt that).

Cheers

Jirka

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 Jan. 11, 2011 12:38:57

Brennan S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Reconnecting after a database restart


On Mon, 2011-01-10 at 09:09 -0800, Euan Goddard wrote:
> As far as I know Django maintains a persistent connection to the
> database server and restarting the server without restarting the
> client isn't possible. We never restart our live database server in
> production (we fail over to a secondary server so that the app is
> essentially only vulnerable for a few seconds whilst the failover
> happens and a subsequent app restart). I think the only thing you can
> do is to restart the app as soon as you've restarted the DB server.

Okay, good to know. It looks like the right solution may be to try to
get a hook into Postgres to trigger an app restart. FYI, this behavior
was also triggered when an emacs process went rogue and OOM'd the
server: postgres reinitialized itself (without actually restarting)
after the kernel killed emacs, which nuked Django's connection.

> All that said, it may be possible to get Django to reconnect, but from
> reading the source in the DB layer, I don't recall seeing anything
> like that.

Right, I wasn't finding anything either. If I can't get a restart hook
in place, I'll give Jirka's suggestion a go.

Thanks everyone!

-Brennan


--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

  • Root
  • » Django
  • » Reconnecting after a database restart [RSS Feed]

Board footer

Moderator control

Enjoy the 13th of December
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support