[Zope-dev] Now; - problems using ZODB/ZCatalog in Python

Shane Hathaway shane@digicool.com
Tue, 25 Jul 2000 08:06:56 -0400


Thomas Weholt wrote:
> 
> First off, sorry for posting a huge, stinkin' post like this, but instead of
> posting ten times trying to explain what I do, I post this once and hope
> somebody can point out what's wrong and I'll leave you all alone, at least
> for a while.

Let me cut out some of the code to show you what's wrong.

> ### OK, the ZODB-API together with a Catalog-Class
> class Application:
>     def __init__( self, file='test.fs' ):
>         self.file= file
>         self.db  = ZODB.DB( FileStorage( file ) )
>         self.co  = self.db.open()
>         self.root= self.co.root()
>         (...)

So, every time you create an Application object you're opening Data.fs. 
Therefore the intention is that only one Application object exists at a
time.

> if __name__ == '__main__':
>     a= Application()
>     p1= Person( 'marian','kelc','marian.kelc@ruhr-uni-bochum.de' )
>     p2= Person( 'tanja', 'kreierhoff','tanja.kreierhoff@web.de', p1 )
>     p1.friend= p2
>     a.storePerson( p1 )
>     a.storePerson( p2 )
> 
>     ### test2.py
>     a= Application()
>     (...)

See the problem here?  Two Application objects are being created.  You
might think that the first Application object is automatically deleted
when replaced by the second Application object, but there's no guarantee
of that.

So the problem is that this program is trying to open the database
twice.  You're not allowed to create multiple FileStorage objects that
point to the same file.  You are, however, allowed to open multiple
connections in different threads to the same file.

The fix is easy: remove the offending line.  Patched code follows.  Good
luck!

> if __name__ == '__main__':
>     a= Application()
>     p1= Person( 'marian','kelc','marian.kelc@ruhr-uni-bochum.de' )
>     p2= Person( 'tanja', 'kreierhoff','tanja.kreierhoff@web.de', p1 )
>     p1.friend= p2
>     a.storePerson( p1 )
>     a.storePerson( p2 )
> 
>     ### test2.py
>     # Don't re-open the database.  a= Application()
> 
>     ### perform searches with keyword-arguments
>     ids= a.searchPerson( name='kelc' )
> 
>     for i in ids:
>         i.identify()
>         print "Friend is:"
>         i.friend.identify()
>         i.friend.friend.identify()
>         print str( id( i ) )
>         print str( id( i.friend.friend ) )

Shane