Reputation: 1512
I need to update a sharded counter and return the current count. I'm following the example here: https://developers.google.com/appengine/articles/sharding_counters
But I'm not sure if you can indirectly perform a transactional query via a function. Here is the example code:
@db.transactional
def updateCounter(name):
increment(name)
get_count(name)
updateCounter(TOTAL_USERS)
Will this return the current count incremented? or is this approach not valid as the database query sits outside the @db.transactional function?
Upvotes: 0
Views: 172
Reputation: 101149
It's not possible* to get a transactional count of a sharded counter. The way sharded counters work is that you can update them in a transaction, and sum them up outside the transaction. Generally, this isn't an issue, because sharded counters are used where the count is updated many times a second; any transactional record of the count would be incorrect virtually the moment it was calculated.
* You could do this for up to 5 shards by using XG transactions, but it's a really bad idea. First, because of the reasons above, and second because it would impose the very same scalability issues you're trying to avoid by using sharded counters in the first place.
Upvotes: 3
Reputation: 80340
You can only use ancestor queries within a transaction.
Since get_count()
uses non-ancestor query his code will produce an error.
Upvotes: 0