Cannot operate on a closed database

139 views
Skip to first unread message

Manuele Pesenti

unread,
Jun 24, 2011, 11:31:48 AM6/24/11
to web2py@googlegroups.com >> web2py-users
dear all,
I've added a unique field to some existing tables and w2p gave me an
error saing that he cannot add unique fields to a table so I deleted my
db and restarted as a new application but I obtained this:

ProgrammingError: Cannot operate on a closed database.

it's sufficient to comment out the property unique=True and no errors
occurs.

Here under you can find the complete traceback.
Any suggestion??

Thanks a lot

Manuele


Traceback (most recent call last):
File
"/home/manuele/Dropbox/sviluppo/web2py-1.96.4/gluon/restricted.py", line
192, in restricted
exec ccode in environment
File
"/home/manuele/Dropbox/sviluppo/web2py-1.96.4/applications/wind2pow2/controllers/appadmin.py",
line 410, in <module>
File "/home/manuele/Dropbox/sviluppo/web2py-1.96.4/gluon/globals.py",
line 137, in <lambda>
self._caller = lambda f: f()
File
"/home/manuele/Dropbox/sviluppo/web2py-1.96.4/applications/wind2pow2/controllers/appadmin.py",
line 126, in insert
form = SQLFORM(db[table], ignore_rw=ignore_rw)
File "/home/manuele/Dropbox/sviluppo/web2py-1.96.4/gluon/sqlhtml.py",
line 817, in __init__
inp = self.widgets.options.widget(field, default)
File "/home/manuele/Dropbox/sviluppo/web2py-1.96.4/gluon/sqlhtml.py",
line 206, in widget
options = requires[0].options()
File
"/home/manuele/Dropbox/sviluppo/web2py-1.96.4/gluon/validators.py", line
438, in options
self.build_set()
File
"/home/manuele/Dropbox/sviluppo/web2py-1.96.4/gluon/validators.py", line
426, in build_set
records = self.dbset.select(*fields, **dd)
File "/home/manuele/Dropbox/sviluppo/web2py-1.96.4/gluon/dal.py",
line 5382, in select
return self.db._adapter.select(self.query,fields,attributes)
File "/home/manuele/Dropbox/sviluppo/web2py-1.96.4/gluon/dal.py",
line 1159, in select
rows = response(sql)
File "/home/manuele/Dropbox/sviluppo/web2py-1.96.4/gluon/dal.py",
line 1149, in response
self.execute(sql)
File "/home/manuele/Dropbox/sviluppo/web2py-1.96.4/gluon/dal.py",
line 1234, in execute
return self.log_execute(*a, **b)
File "/home/manuele/Dropbox/sviluppo/web2py-1.96.4/gluon/dal.py",
line 1229, in log_execute
ret = self.cursor.execute(*a,**b)
ProgrammingError: Cannot operate on a closed database.

Massimo Di Pierro

unread,
Jun 24, 2011, 2:13:02 PM6/24/11
to web2py-users
Is this is a sqlite or other?

On Jun 24, 10:31 am, Manuele Pesenti <manuele.pese...@gmail.com>
wrote:
> dear all,
> I've added a unique field to some existing tables and w2p gave me an
> error saing that he cannot add unique fields to a table so I deleted my
> db and restarted as a new application but I obtained this:
>
> ProgrammingError: Cannot operate on a closed database.
>
> it's sufficient to comment out the property unique=True and no errors
> occurs.
>
> Here under you can find the complete traceback.
> Any suggestion??
>
> Thanks a lot
>
>         Manuele
>
> Traceback (most recent call last):
>    File
> "/home/manuele/Dropbox/sviluppo/web2py-1.96.4/gluon/restricted.py", line
> 192, in restricted
>      exec ccode in environment
>    File
> "/home/manuele/Dropbox/sviluppo/web2py-1.96.4/applications/wind2pow2/contro llers/appadmin.py",
> line 410, in <module>
>    File "/home/manuele/Dropbox/sviluppo/web2py-1.96.4/gluon/globals.py",
> line 137, in <lambda>
>      self._caller = lambda f: f()
>    File
> "/home/manuele/Dropbox/sviluppo/web2py-1.96.4/applications/wind2pow2/contro llers/appadmin.py",

Manuele Pesenti

unread,
Jun 24, 2011, 2:17:39 PM6/24/11
to web...@googlegroups.com
On 24/06/2011 20:13, Massimo Di Pierro wrote:
> Is this is a sqlite or other?

yes, it was sqlite. At the moment I solved using postgres witrh
pool_size=100 but I didn't try if this parameter is really necessary.

what's the difference between the two db in this case??

Manuele

pbreit

unread,
Jun 24, 2011, 2:20:34 PM6/24/11
to web...@googlegroups.com
I don't think SQLite does pools. SQLite DBs are easy to deal with...just delete everything in the "databases" directory and start over. Might want to double-check that your connection string is pointing to the right place.

Massimo Di Pierro

unread,
Jun 24, 2011, 2:40:01 PM6/24/11
to web2py-users
When you deleted the db, did you also delete the associated .table
files? You should have

On Jun 24, 10:31 am, Manuele Pesenti <manuele.pese...@gmail.com>
wrote:
> dear all,
> I've added a unique field to some existing tables and w2p gave me an
> error saing that he cannot add unique fields to a table so I deleted my
> db and restarted as a new application but I obtained this:
>
> ProgrammingError: Cannot operate on a closed database.
>
> it's sufficient to comment out the property unique=True and no errors
> occurs.
>
> Here under you can find the complete traceback.
> Any suggestion??
>
> Thanks a lot
>
>         Manuele
>
> Traceback (most recent call last):
>    File
> "/home/manuele/Dropbox/sviluppo/web2py-1.96.4/gluon/restricted.py", line
> 192, in restricted
>      exec ccode in environment
>    File
> "/home/manuele/Dropbox/sviluppo/web2py-1.96.4/applications/wind2pow2/contro llers/appadmin.py",
> line 410, in <module>
>    File "/home/manuele/Dropbox/sviluppo/web2py-1.96.4/gluon/globals.py",
> line 137, in <lambda>
>      self._caller = lambda f: f()
>    File
> "/home/manuele/Dropbox/sviluppo/web2py-1.96.4/applications/wind2pow2/contro llers/appadmin.py",

Manuele Pesenti

unread,
Jun 24, 2011, 2:50:35 PM6/24/11
to web...@googlegroups.com
On 24/06/2011 20:40, Massimo Di Pierro wrote:
> When you deleted the db, did you also delete the associated .table
> files? You should have

everything in the databases directory...

Manuele

Massimo Di Pierro

unread,
Jun 24, 2011, 2:54:56 PM6/24/11
to web2py-users
I do not know. This is odd. Can you send me your app (confidentally)?

Massimo Di Pierro

unread,
Jun 26, 2011, 2:03:54 PM6/26/11
to web2py-users
I found the cause it this problem. It is not a bug in web2py but
something to be careful about in your programs.

You have a models/meteo.py which imports modules/meteo.py.

models/meteo.py does

current.database = db

and modules/mete.py does

db = current.database

the problem is that the latter statement is done at top leve therefor
it is only executed once when the module is imported by the first http
request. The module is then cached in ram and that line is no longer
executed. Therefore db is not the current thread local database
object but it is a reference to the db object of the first http
request which by now may be closed. You cannot extract objects from
the thread local "current" when you import the module, you have to do
it inside functions called when a controller is executed.

Hope this makes sense.



On Jun 24, 10:31 am, Manuele Pesenti <manuele.pese...@gmail.com>
wrote:
> dear all,
> I've added a unique field to some existing tables and w2p gave me an
> error saing that he cannot add unique fields to a table so I deleted my
> db and restarted as a new application but I obtained this:
>
> ProgrammingError: Cannot operate on a closed database.
>
> it's sufficient to comment out the property unique=True and no errors
> occurs.
>
> Here under you can find the complete traceback.
> Any suggestion??
>
> Thanks a lot
>
>         Manuele
>
> Traceback (most recent call last):
>    File
> "/home/manuele/Dropbox/sviluppo/web2py-1.96.4/gluon/restricted.py", line
> 192, in restricted
>      exec ccode in environment
>    File
> "/home/manuele/Dropbox/sviluppo/web2py-1.96.4/applications/wind2pow2/contro llers/appadmin.py",
> line 410, in <module>
>    File "/home/manuele/Dropbox/sviluppo/web2py-1.96.4/gluon/globals.py",
> line 137, in <lambda>
>      self._caller = lambda f: f()
>    File
> "/home/manuele/Dropbox/sviluppo/web2py-1.96.4/applications/wind2pow2/contro llers/appadmin.py",

Manuele Pesenti

unread,
Jun 26, 2011, 3:49:26 PM6/26/11
to web...@googlegroups.com
On 26/06/2011 20:03, Massimo Di Pierro wrote:
> Hope this makes sense.

thanks a lot
I got it now :)

Manuele

appydev

unread,
Sep 6, 2011, 12:30:05 PM9/6/11
to web...@googlegroups.com
Why not so with: request = current.request?

Is it safe to use the following?
# modules/myobjects.py
from gluon import *
request,session,response,T,cache = current.request,current.session,current.response,current.T,current.cache

and then use some class method :
if form.accepts(request, session):

Or rather it is better to use directly:
if form.accepts(current.request, current.session):


Do not think of something analogous to this affirmation made for the db object?
the problem is that the latter statement is done at top leve therefor
it is only executed once when the module is imported by the first http
request. The module is then cached in ram and that line is no longer
executed. Therefore db is not the current thread local database
object but it is a reference to the db object of the first http
request which by now may be closed. You cannot extract objects from
the thread local "current" when you import the module, you have to do
it inside functions called when a controller is executed.

I apologize for my English.



2011/6/26 Massimo Di Pierro <massimo....@gmail.com>
Reply all
Reply to author
Forward
0 new messages