[ZCM] [ZC] 844/ 3 Comment "DA instances open connection too early"

"Collector: Zope Bugs, Features, and Patches ..." <[email protected]> Tue, 11 Sep 2007 13:23:36 -0400
Newsgroups gmane.comp.web.zope.devel.collector-monitor
Message-ID <[email protected]>
Issue #844 Update (Comment) "DA instances open connection too early"
 Status Pending, Database/bug medium
To followup, visit:
  http://www.zope.org/Collectors/Zope/844

==============================================================
= Comment - Entry #3 by eikenberry on Sep 11, 2007 1:23 pm


Uploaded:  "setstate.diff"
 - http://www.zope.org/Collectors/Zope/844/setstate.diff/view
The collector screwed up my diff's layout so I'll attache it just to be sure it is clear.

________________________________________
= Comment - Entry #2 by eikenberry on Sep 11, 2007 1:19 pm

I've recently run into issues with this myself while working on updating
ZMySQLDA. My current alternatives are overridding __setstate__ to remove the
connect() call or doing something funky like having connect() fail if
acquisition is not set up yet (ie. a crude setstate detector).

Seeing as the original suggestion to remove it completely hasn't seem to
generate much interested I have an alternative. How about a simple class
attribute or method to allow for subclasses to disable this 'feature'. 

 --- Connection.py.orig  2007-09-11 13:15:32.000000000 -0400
 +++ Connection.py       2007-09-11 13:16:27.000000000 -0400
 @@ -60,6 +60,7 @@
  
      _v_connected=''
      connection_string=''
 +    connect_on_setstate=True
  
      def __init__(self, id, title, connection_string, check=None):
          self.id=str(id)
 @@ -67,7 +68,7 @@
  
      def __setstate__(self, state):
          Globals.Persistent.__setstate__(self, state)
 -        if self.connection_string:
          +        if self.connect_on_setstate and self.connection_string:
              try: self.connect(self.connection_string)
              except:
                  LOG.error('Error connecting to relational database.',
 

________________________________________
= Request - Entry #1 by d.maurer on Mar 10, 2003 4:46 pm

DA instances derived from "Shared.DC.ZRDB.Connection" are very
eager to open a database connection:

  They do so immediately when they are loaded from ZODB.

  If someone looks at a folder containing a DA instance,
  a connection is opened.

  When someone uses the left frame in the ZMI, then
  DA instances too levels deeper than the unfolded tree
  open database connections.

This wastes precious resources.

Moreover, and more importantly, it can lead to very peculiar
behaviour.

   Twice, I had to analyse a behaviour that some
   requests (apparently) non-deterministically took more
   than 4 minutes. In both cases, it turned out
   (after long analysis) that an invisible DA instance
   trying to connect to an offline database was the culprit.
   The 4 minutes are the connection timeout.


Do not open a connection in "__setstate__"!
Do it, when the database is accessed for the first time.
If something goes wrong then, you get a traceback describing
the problem. Now, some requests unrelated to the database
simply take excessive time without any indication with respect
to the problem.

==============================================================