2. The Code

First you'll need to get an instance of ZenEventManager OR an instance of a class that extends DbAccessBase. Within Zenoss, a ZenEventManager should already be instantiated.

Next is the "try" block which should include ANY DB calls. This is where you'll get a connection from the pool with the connect() method. You may pass this around to other methods or create a cursor and make some DB transactions. The try block MUST be completed with a "finally" block that includes the close() method. You MUST pass the connection object to the close() method. This will insure that even if the code within the "try" breaks, we are not leaking DB connections. If you create more than one connection (more than one connect() call in your try block) you will need to have a corresponding close() call. There is ALWAYS a one-to-one relationship between connect() and close() calls.

Here is a block of code that illustrates best practices for using the DbConnectionPool


...
zem = self.dmd.ZenEventManager
try:
	conn1 = zem.connect()
	conn2 = zem.connect()
	curs1 = conn1.cursor()
	...
	curs2 = conn2.cursor()
	...
	# do work
	...
	curs3 = conn1.cursor()
	...
finally:
	zem.close(conn1)
	zem.close(conn2)
	...
...

Take a look at EventManagerBase.py for some examples of code using the DbConnectionPool.