Re: postgresql connection is not closed

2012-05-14 Thread akaariai
On May 14, 9:05 am, shariq  wrote:
> Hi,
> I am running django-1.2 with postgresql.
> I am facing the problem.
> Django is not closing the connection and the connection is
> retained by db-server until DBA goes and kill the connection manually.

Are you using multiple databases? Is this happening in "batch job"
processing? That is, the connection is left open in situations where
you are not doing normal HTTP request serving.

When using connections outside of request serving you are responsible
for closing the connections manually yourself. However, in request
processing situations Django will close the connections automatically
when the request is processed.

There is definitely some room for improving the documentation of how
to use Django connections safely in batch job processing. Leaving open
transactions behind in multidb situations is too easy to do currently,
and closing connections isn't too well documented either.

Currently you will need to do something like this:
from django.db import connections
def close_connections():
for conn in connections.all():
conn.close()
from django.db.transactions import commit_on_success
#repeat for every DB you are going to use in your batch job.
@commit_on_success(using='default')
def _my_batch_job():
# real code here

def my_batch_job():
try:
_my_batch_job()
finally:
close_connections()

It would be nicer if you could do this instead:
@commit_on_success(using=['default', 'other'], close_connections=True)
def my_batch_job():
#real code here

In addition I would like to have an "explicit transactions only" mode,
where using a database which you haven't explicitly mentioned in
commit_on_success/commit_manually etc decorator will be treated as an
error. The reason for this is:
@commit_on_success
def my_batch_job():
for SomeModel.objects.all():
do something here...
the above code looks safe, but it might be that
SomeModel.objects.all() actually queries some other database than the
default on - and you have IDLE IN TX error right there.

The above went a little off-topic. So in short: you are responsible
for closing connections manually except in request processing, where
connections are closed automatically.

 - Anssi

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



postgresql connection is not closed

2012-05-14 Thread shariq
Hi,
I am running django-1.2 with postgresql.
I am facing the problem.
Django is not closing the connection and the connection is
retained by db-server until DBA goes and kill the connection manually.

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