[Tutor] The case of the missing close

Kent Johnson kent37 at tds.net
Wed Dec 1 04:21:38 CET 2004


I don't know cx_Oracle, but my guess is you want to close the actual connection object, not the 
cursor. Your dbopen() function doesn't expose the connection.

One option would be to return the connection rather than the cursor. Then clients could close the 
connection when done. Another option is to create a class that holds both the connection and the 
cursor, then you can have close() on the class actually close the connection.

Kent


Robert, Andrew wrote:
> Hi everyone,
> 
> I am trying to create a generic db_utils.py module that I will then
> generically import into programs as needed.
> 
> The first function I am trying to implement iis to generically open an
> Oracle database connection based on passed database/authentication
> parameters.
> 
> So far, I have been able to:
> 
> - import the custom module db_utils
> - pass the parameters function dbopen in the custom module
> - open the connection to the requested database
> - pass back the cursor object to the calling program for later
> processing.
> 
> The problem is in closing the connection.
> 
> No matter what I try,  I receive an error stating that the name is not
> defined.
> 
> I receive an error such as:
> 
> Traceback (most recent call last):
>   File "M:\My
> Documents\python_code\oracle\maestrodev_oracle_connector.py", line 55,
> in ?
>     db_utils.close()
> AttributeError: 'module' object has no attribute 'close'
> 
> Does anyone have any ideas on how to properly close the connection?
> 
> 
> 
> The db_utils module code is a rather short
> 
> 
> def dbopen(db,uname,passwd):
>     #
>     # Import required system modules needed specifically for function
>     #
>     import cx_Oracle
> 
>     #
>     # Connect to remote database
>     #   
>     connection = cx_Oracle.connect(dsn=db,user=uname,password=passwd)
>   
>     #
>     # Return cursor object to calling program
>     #
>     return(connection.cursor())
> 
> 
> The code that calls the db_utils module is
> 
> #!c:\Python23\python
> #
> # File name: maestrodev_oracle_connector.py
> # Author: Andrew Robert
> # Date: 11/26/04
> #
> #
> # Modification History
> #
> # Version          Programmer                   Description
> # 1.0               AAR                          Creation
> # 1.1               AAR                 Shift database opens to called
> module
> # 1.2               AAR                 Fixed database link close - now
> works
> #
> #
> # Note on modules
> #
> # The imported db_utils module was designed by AAR to make standard
> database
> # routines available to all python programs as callable functions.
> #
> # Called functions are prefaced with the module name and then the
> function
> # within the module.
> #
> 
> 
> import sys,db_utils
> 
> #
> # Make connection to Oracle development database and assign to object
> #
> 
> print 'Establishing connection to remote database\n'
> cursobj = db_utils.dbopen('test_d','FOO','foo')
> 
> #
> # Formulate sample querry
> #
> cursobj.execute('SELECT userid, name, role, desk_phone, pager FROM
> contacts')
> 
> #
> # Extract querry results 
> #
> results=cursobj.fetchall()
> 
> for row in results:
>     print row
> 
> #
> # Break connection to Oracle development database
> #
> print '\n\n\nDisconnecting from remote database'
> 
> db_utils.close()
> 
> raw_input("\n\n\t\tPress Enter To Continue")
> 
> 
> Any help you can provide on this would be greatly appreciated.
> 
> 
> 
> 
> Thank you,
> Andrew Robert
> Systems Architect
> Information Technology 
> Massachusetts Financial Services
> Phone:  617-954-5882
> Pager:   781-764-7321
> E-mail:  arobert at mfs.com
> Linux User Number: #201204
> 
> 
> "MFS Relay Service" made the following
>  annotations on 11/30/2004 11:11:35 AM
> ------------------------------------------------------------------------------
> This email communication and any attachments may contain proprietary, confidential, or privileged information.  If you are not the intended recipient, you are hereby notified that you have received this email in error and that any review, disclosure, dissemination, distribution or copying of it or its contents is prohibited.  The sender does not waive confidentiality or any privilege by mistransmission.  If you have received this email in error, please notify the sender immediately, delete this email, and destroy all copies and any attachments.
> ==============================================================================
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


More information about the Tutor mailing list