[ZODB-Dev] Zope and ZODB database connections

MKinsley@empirecorp.org MKinsley@empirecorp.org
Thu, 15 Aug 2002 14:28:04 -0400


Hello everyone,

We are currently developing a web application utilizing Zope and our own
ZODB database.
We have a few DTML documents that call external methods (written in Python).

We would like to open a ZODB connection to a database and keep it open while
passing the actual connection instance to other external methods which
process/manipulate/return the dynamic data of the web pages. We have tried
to use Netscape cookies to store a ZODB connection instance returned from an
external method 'dbOpenConn' and pass the value of the cookie (ZODB
connection instance) as a parameter to the 'dbCloseConn'. This only works in
the following DTML document. 

<dtml-var standard_html_header>
<h3><dtml-var welcomeMCIF></h3>
<form action="" name="form1" method="post">
 <dtml-let retobj=dbOpenConn>
  <dtml-call "RESPONSE.setCookie('wrkaround',retobj)">
  <dtml-var
"dbCloseConn(REQUEST.RESPONSE.cookies.get('wrkaround')['value'])">
 </dtml-let>
 <input type="hidden" name="rt_from" value="1">
 <input type="hidden" name="rt_to" value="1">
 <p><input type="submit" name="home2.html:action" value="Request CU
Information"></p>
 <table name="home1">
   <tr valign="top">
       <td align="left"><a href="mrm">Member Service
Tracking</a>&nbsp;|</td>
       <td align="left"><a href="mkt">Marketing Tracking</a></td>
   </tr>
 </table>
</form>
<dtml-var standard_html_footer>



We cannot pass the ZODB connection instance to the next page and then as a
parameter to a 'dbCloseConn' on that page because ZODB instances cannot be
stored in a session variable, Python cookie, or another class attribute
(result: Unpickleable error). We can use a volatile attribute _v_ to store
the instance temporarily, but not throughout the session. 

We have also tried to pass a string representation to pages and call
'dbReturnDtml' to try and evaluate the string and put it in a command line
to execute. The command line is 
'dbCloseConn(<ZODB.Connection.Connection instance at 0xc098804>)' .  Using
rexec.RExec r_exec  gives us the following error:


               Zope Error

               Zope has encountered an error while publishing this resource.

               Error Type: SyntaxError
               Error Value: invalid syntax (line 1)



               Troubleshooting Suggestions

                     The URL may be incorrect. 
                     The parameters passed to this resource may be
incorrect. 
                     A resource that this resource relies on may be
encountering an error. 

               For more detailed information about the error, please refer
to the HTML source for this page. 

               If the error persists please contact the site maintainer.
Thank you for your patience. 




Traceback (innermost last):
  File /usr/local/Zope-2.5.1-src/lib/python/ZPublisher/Publish.py, line 150,
in publish_module
  File /usr/local/Zope-2.5.1-src/lib/python/ZPublisher/Publish.py, line 114,
in publish
  File /usr/local/Zope-2.5.1-src/lib/python/Zope/__init__.py, line 159, in
zpublisher_exception_hook
    (Object: mcif)
  File /usr/local/Zope-2.5.1-src/lib/python/ZPublisher/Publish.py, line 98,
in publish
  File /usr/local/Zope-2.5.1-src/lib/python/ZPublisher/mapply.py, line 88,
in mapply
    (Object: home2.html)
  File /usr/local/Zope-2.5.1-src/lib/python/ZPublisher/Publish.py, line 39,
in call_object
    (Object: home2.html)
  File /usr/local/Zope-2.5.1-src/lib/python/OFS/DTMLDocument.py, line 127,
in __call__
    (Object: home2.html)
  File /usr/local/Zope-2.5.1-src/lib/python/DocumentTemplate/DT_String.py,
line 473, in __call__
    (Object: home2.html)
  File /usr/local/Zope-2.5.1-src/lib/python/DocumentTemplate/DT_Let.py, line
76, in render
    (Object: strrobj="_['wrkaround']")
  File /usr/local/Zope-2.5.1-src/lib/python/DocumentTemplate/DT_Let.py, line
75, in render
    (Object: robj="dbReturnDtml(strrobj)")
  File /usr/local/Zope-2.5.1-src/lib/python/DocumentTemplate/DT_Util.py,
line 159, in eval
    (Object: dbReturnDtml(strrobj))
    (Info: dbReturnDtml)
  File <string>, line 2, in f
  File
/usr/local/Zope-2.5.1-src/lib/python/Products/ExternalMethod/ExternalMethod.
py, line 198, in __call__
    (Object: dbReturnDtml)
    (Info: (('<ZODB.Connection.Connection instance at 0xc098804>',), {},
None))
  File /usr/local/Zope-2.5.1-src/Extensions/mcif_driver.py, line 42, in
dbReturnDtml
  File /usr/local/lib/python2.1/rexec.py, line 264, in r_exec
SyntaxError: (see above)




Is there a way to get this to work or can anyone suggest a better way or
better yet, the right way? The Python external method code we are using is
provide below.

Your input is much appreciated. Thank you in advance for your support! 





##mcif_driver.py (Zope external methods source)##

import os
import rexec 
import modAccounts
import Globals
import string
from Products.PythonScripts.standard import DTML

def dbOpenConn(fname='/usr/local/Zope-2.5.1-src/var/mcif.fs'):
    manager = modAccounts.ManageMCIF()
    print "Set connection to MCIF database"
    conn = manager.conDatabase(fname)
    print "Set!"
    print "Set instances to MCIF database"
    manager.iniInstances(conn)
    print "Set!"
    #manager.storeSession(conn)
    return conn


def dbCloseConn(dbconObj,fname='/usr/local/Zope-2.5.1-src/var/mcif.fs'):
    manager = modAccounts.ManageMCIF()
    manager.cloDatabase(dbconObj,fname)
    print "Connection closed successfully!!!"

    
def dbReturnDtml(objstr):
    
    myString = '<dtml-var expr="dbCloseConn(' + objstr +')">'
    cmdline = eval("'dbCloseConn(' + objstr + ')'")
    print eval("cmdline")
    cotrll = rexec.RExec()
    cotrll.r_exec(cmdline)  #error occurs with r_exec
    myDTML = DTML(myString)
    return myDTML()


Marco Kinsley
Web Applications Developer
_____________________
Empire Corporate FCU
1021 Watervliet-Shaker Rd
Albany NY 12205
(518) 292-3800/800-253-0053
e-mail: mkinsley@empirecorp.org

NOTICE:  This e-mail may contain confidential or legally privileged
information and is intended solely for delivery to the specific person
identified as the recipient.  Any review, re-transmission, dissemination or
other use or taking of any action in reliance upon this e-mail by persons
other than the intended recipient is prohibited.  If you receive this e-mail
in error, please contact us at (mkinsley@empirecorp.org) and delete from
your computer system, or otherwise from you records, the information, which
was transmitted to you in error.