[PEAK] Newbie Question

William Trenker wtrenker at shaw.ca
Sun Dec 7 06:12:58 EST 2003


[Vital Lobachevsky pointed out that:]
the database example in the wiki needs to have the database access wrapped in a transaction.

I tried that and now the example works great.  Thanks Vital.

[Phillip J. Eby observed that:]
most of the time when I write DB code, the transaction has been created far outside the code that does the DB work

That's a good point.  As a learner, I'm creating snippets in rather contrived contexts.  But that has helped me to learn about the environment that PEAK components need to operate in.  In this "minimalist" spirit, for the purposes of learning, I've run into a curious side-effect.  The following snippet incorporates what you've explained and the rows from my table now print just fine:

from peak.api import *
class myDB(binding.Component):
	db = binding.Obtain('sqlite:test.db')
root = config.makeRoot()
storage.beginTransaction(root)
rows = myDB(root).db('select name,description from aTable')
for row in rows:
	print row.name, row.description
storage.commitTransaction(root)

(The rows from aTable are printed, as expected.)

But if I move the commitTransaction invocation up to just before the "for" statement, the "rows" rowStruct is empty, like this:

from peak.api import *
class myDB(binding.Component):
	db = binding.Obtain('sqlite:test.db')
root = config.makeRoot()
storage.beginTransaction(root)
rows = myDB(root).db('select name,description from aTable')
storage.commitTransaction(root)
for row in rows:
	print row.name, row.description

(Nothing is printed)

Why is the rows rowStruct empty in the 2nd scenario?  Has this got something to do with PEAK's lazy binding feature?  Am I missing yet another key concept here?

Thanks again,
Bill





More information about the PEAK mailing list