I think the confusion is that you use dbPool like a Python module, not a Webware specific thing.
In Python Modules are executed and loaded once, no matter how many times they are imported. Due to this simple fact you can create a class or varriable in a module and that class will exist once, regardless of how many times its called.
So you should create dbPool in a seperate module. 1) create a module called configuration.py that contains: from MiscUtils.DBPool import DBPool from pyPgSQL import PgSQL
# set up the database connection pool
datapool = DBPool(PgSQL, 5, 'localhost::databasename:user:pass')
#The actual syntax of 'localhost::databasename:user:pass' will depend upon your database connector
2) When you want to use a connection: from configuration import datapool class selCity(SecurePage):
def writeHTML(self):
conn = datapool.getConnection()
c = conn.cursor()
c.execute('select "CalledCity" from "UniqueCalledCities"')
rsCity = c.fetchall()
c.execute('select "CalledState" from "UniqueCalledStates"')
rsState = c.fetchall()
c.close()
conn.close()
.....A better way would be to put all of the database work into another module and call that from a servlet.
CSData.py: from configuration import datapool,clientpool
def summaryStats(user):
summary={}
conn=datapool.getConnection()
c=conn.cursor()
c.execute("Select numrows from numrows")
r=c.fetchone()
summary['numrows'] = r.numrows
c.close()
return summaryand in your servlet:
import CSData
class servlet....:
def writeContent(self):
dataStats=CSData.summaryStats(user)
self..writeln("Summary: %s" % dataStats)You should be able to get dbPool working outside of webware.
Write a simple test module that pulls some data out using dbPool and get that running furst. You may need to add the webware directory to your pythonpath first
-Aaron
Adam Shimali wrote:
Hello all,
Just like to say first of all how much I love Webware.
Well personal confessions out of the way, I'd like to ask if anyone can help me get how to use DBPool.
I noticed an earlier post (
http://sourceforge.net/mailarchive/forum.php?thread_id=2054471&forum_id=3505
), but I have to admit I didn't really get the
solution.
Where should you instantiate a DBPool? Can you initialise one for the use of all servlets in your application?
For example let's say I'm using WebKit, have a
SitePage that all my Webware servlets inherit from and
I have created a number of servlets based on the
SitePage e.g. Login.py , Main.py, AddItem.py etc. that
all need to interact with a given database. I gather
that I could just get my SitePage base class (or even
the derived servlets if not all of them need database
access) to just open and hold a connection which
persists as long as the servlet does. So in the
example above my application would have an open
connection for each of the above three servlets.
Now what if instead of the base or derived servlets
opening a database connection, I wanted to create a
pool of connections and then any of the servlets could
grab a connection from the pool, use it and release
when done. Where would I create the pool, and how
would each servlet communicate with it? Do you create
one in the __init__.py file for the package context?
I think I'm exposing some deep conceptual confusion about the use of a DBPool in Webware. However I think that in some sense (probably incorrectly) I'm expecting it work like pooled database connections in AOLServer. That is, the pool exists within the execution environment of the webserver and so any "script" executing in that environment can make a call to the webserver's API that returns a connection to the database, which can be released when done with.
Can something similar be done in Webware using the DBPool?
Thanks Adam Shimali
__________________________________________________ Yahoo! Plus - For a better Internet experience http://uk.promotions.yahoo.com/yplus/yoffer.html
-------------------------------------------------------
This SF.Net email sponsored by: Free pre-built ASP.NET sites including
Data Reports, E-commerce, Portals, and Forums are available now.
Download today and enter to win an XBOX or Visual Studio .NET.
http://aspnet.click-url.com/go/psa00100006ave/direct;at.asp_061203_01/01
_______________________________________________
Webware-discuss mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/webware-discuss
-- -Aaron http://www.MetroNY.com/ "I don't know what's wrong with my television set. I was getting C-Span and the Home Shopping Network on the same station. I actually bought a congressman." - Bruce Baum
------------------------------------------------------- This SF.Net email sponsored by: Free pre-built ASP.NET sites including Data Reports, E-commerce, Portals, and Forums are available now. Download today and enter to win an XBOX or Visual Studio .NET. http://aspnet.click-url.com/go/psa00100006ave/direct;at.asp_061203_01/01 _______________________________________________ Webware-discuss mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/webware-discuss
