开发者

Is it possible to extend SQLAlchemy to add the ability to lock the database?

开发者 https://www.devze.com 2023-03-20 01:41 出处:网络
I\'m beginning to develop a site in Pyramid and, before I commit to using SQLAlchemy, would like to know if it\'s possible to wrap/extend it to add in \'database lock\' functionality.

I'm beginning to develop a site in Pyramid and, before I commit to using SQLAlchemy, would like to know if it's possible to wrap/extend it to add in 'database lock' functionality.

One quick example as to why I'd like this functionality is for write throttling. My wrapper will be able to detect if a user is flooding the database with writes and, if they are, they'll be prevented from further writes for X amount of time.

I was looking into extending sqlalchemy.org.session.Session and overriding the add method which woul开发者_开发技巧d perform this throttle check. If the user passes the check, it would simply pass the query off to super(MyWrapper, self).query(*args, **kargs)

This is easy enough to do. However, it only adds the throttle functionality to DBSession.query. If somewhere in my code I use DBSession.execute, the throttle check is bypassed.

Is there a cleaner way to accomplish this?


Detecting excessive network traffic from particular clients is something you might be doing outside the ORM, even outside the Python app, like at the network or database client configuration level.

If within the Python app, definitely not in the ORM. add() doesn't correspond very cleanly to a SQL statement in any case (no SQL is emitted until flush(), and only if the given object was previously pending. add() also cascades to many objects and can result in any number of INSERT statements).

For a simple count on statements, cursor execute events are the best way to go. This gives you a hook at the point of calling execute() on the DBAPI cursor. See before_cursor_execute() at http://www.sqlalchemy.org/docs/core/events.html#connection-events.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号