[Zope] ZODB question, again

Michel Pelletier michel@digicool.com
Thu, 16 Mar 2000 12:05:50 -0800


Garry Hodgson wrote:
> 
> ok, persistent fool that i am, i'm still trying to figure out
> how to use ZODB in an External Method to do some simple (ha!)
> database stuff.
> 
> so i built a very simple app, to implement a counter.
> each time you invoke the "Count" method, you get a number
> back, and it increments.
> 
> running the script (see below) by hand, it works fine.
> referencing it from Zope, using this dtml fragment:
> 
>         <dtml-var "Count( 'chickens' )">
> 
> or just pointing my browser at:
> http://kestrel.sage.att.com:8080/Garry/ZODB/Count?name=chickens
> 
> to reference an External Method called Count, which
> refers to the Count function in this script, i get:
> 
>    Error Type: AttributeError
>    Error Value: Counter
> 
> if i then click the reload button, i get:
> 
>    Could not lock the database file. There must be another
>    process that has opened the file.
> 
> and i'm locked out until i restart zope.
> can someone please help?  this is getting really
> discouraging.  a simple explanation of where i'm
> going wrong would be greatly appreciated.

You cannot open the object database twice.  In this case, Zope opens it
first, and then you open it second (why you are getting the
AttributeError I don't know, a traceback there would help).

Basically what you are doing is sort-of a no-no unless you know what you
are doing.  First, a database cannot be opened twice, even from the same
process.  Second, you do not need to open the database again, because it
is already open and accessible to you from your external method:

def anExtMeth(self):
  connection = self._p_jar

All persistent objects (in this case, 'self' being the container of the
external method, probably a Folder) have an _p_jar attribute which is
their connection to the database.  Always use that instead of opening
your own.

Also, you need to be very careful with what you are doing.  You are
inserting objects right into the root level of the object database, this
is not really a good idea.  For example, let's say you fix your code to
not open the database again and you get it working.  Then (don't!) try
this URL:

http://kestrel.sage.att.com:8080/Garry/ZODB/Count?name=Application

Doh!  Guess what?  You just trampled your entire Zope, hosed the entire
enchilada, because Zope also lives in that top level namespace, and its
name is 'Application'.

Pick your own name (like GarryHodgsonApplication) and assign your
counter objects as sub-attributes of that.

-Michel