[mod_python] Re:mod_python and connection to database. (Rodion)

Graham Dumpleton graham.dumpleton at gmail.com
Sat Oct 13 20:06:15 EDT 2007


Hmm, I not sure if this is an answer to your own question or you are
replying to a really old post, as I don't remember the question.

Either way, the code being presented as an example on the way to solve
this is unsafe for a number of reasons.

The first reason is that it isn't thread protected and thus isn't safe
in a multithread process such as when Apache UNIX worker MPM or
Windows winnt MPM is used. This is because multiple handlers could be
executing concurrently and both decide the global database handle
needs to be created. This could result in wasted resources.

A second possible problem which can occur is where the global database
handle and the code for creating it, is placed in a code module which
is the subject of automatic reloading. So, once threading issues are
dealt with then okay, but do not go putting it in the same code file
as your mod_python handlers, or in general anywhere in the document
tree. Instead put it in a module somewhere else on sys.path outside of
the document tree. For an example of why this can be a problem see:

  http://groups.google.com/group/sqlalchemy/browse_thread/thread/5193bc7598f045fb#

Also ensure you read documentation for import_module() in:

  http://www.modpython.org/live/current/doc-html/pyapi-apmeth.html

It mentions a bit about resource leakage and transferring data from
old module to new module. Ensure you are using mod_python 3.3.1
though, as older versions were a bit less predictable.

  http://www.dscpl.com.au/wiki/ModPython/Articles/ModuleImportingIsBroken

Graham

On 14/10/2007, Thimios Katsoulis <thkatsou at yahoo.gr> wrote:
> Hello.
>
> Sorry for my poor english. Here is my mod_python code. This is a simple
> URL shorter:
>
> from mod_python import apache, util
> import psycopg2 as psycopg
>
> def handler(req):
>      req.content_type = "text/plain"
>      url_id = req.args
>      connection = psycopg.connect("dbname=my_db")
>      cursor = connection.cursor()
>      cursor.execute("""SELECT myurl FROM urls WHERE myid=%s""",(url_id))
>      original_url = cursor.fetchone()[0]
>      connection.close()
>      util.redirect(req,original_url)
>      req.status = apache.DONE
>      return apache.DONE
>
> This programme connects database everytime, but I want(need) force him
> to connect it continously
>
> This script connects database everytime (psycopg.connect("dbname=my_db")
> , but I nedd force to stay conneceted with it continously. It is
> possible to make that in mod_python ? I have mod_python 3.2.10
>
> Thanks in advance.
> rdn
> ---------------
>
> Yes mod_python can maintain global variables per interpreter, so you can open once the connection
> and all subsequent requests will use the opened connection.
> You have to declare your connection variable as global e.g. :
>
>
>     def getConn(self):
>
>         try:
>             if _conn == None:
>                 self.openConn()
>
>
>         except NameError:
>             self.openConn()
>         return _conn
>
>     def openConn(self):
>
>         global _conn
>         _conn = connect(self.ConnStr)
>
>
> So when you want to access it you call self.getConn().
> The _conn global variable will instantiate once for each mod_python apache process.
> You can include some apache.log_error calls in the code above to watch
> for yourself when the  _conn varible gets instantiated and when it is retreived as global.
> Please take notice from what I have observed that while you maintain open connections to DB (postgresql too in my case)
> you cannot change structure of the tables etc in the DB..
> Of course if you are using modules and not objects you have to alter  the code to  support (removing self ..) modules.
>
>
>
>
>
>
>
>
>
> ___________________________________________________________
> Χρησιμοποιείτε Yahoo!;
> Βαρεθήκατε τα ενοχλητικά μηνύματα (spam); Το Yahoo! Mail
> διαθέτει την καλύτερη δυνατή προστασία κατά των ενοχλητικών
> μηνυμάτων http://login.yahoo.com/config/mail?.intl=gr
>
>
>
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python
>



More information about the Mod_python mailing list