/content/work/2012/mysql-sqlalchemy.rst

https://github.com/SAKev/lepture.com · ReStructuredText · 89 lines · 59 code · 30 blank · 0 comment · 0 complexity · eb4a846082f7af25fb60441c61436069 MD5 · raw file

  1. MySQL and SQLAlchemy in tornado
  2. ================================
  3. :folder: work
  4. :date: 2012-02-09 21:15
  5. :github: lepture/tornado.ext
  6. :tags:
  7. - python
  8. - mysql
  9. - tornado
  10. Fix MySQL has gone away
  11. -----------------------
  12. I just started a new project, which is built on tornado with database of mysql.
  13. It's a new project, and there is no user. Therefore, the database can be inactive for a long
  14. time, it then occurred to me that mysql has gone away!
  15. I am using SQLAlchemy as the ORM engine, according to the document_ , I added ``pool_recycle=3600`` , but it didn't work. And I don't know why. It seems that many a people has such a problem
  16. when I searched Stack Overflow.
  17. Then I figured it out, I am using tornado! I can set a PeriodicCallback to ping mysql every
  18. pool recycle time, so that mysql can not go away.
  19. I have written a `wrap for SQLAlchemy <http://lepture.com/work/tornado-ext/>`_ to make it a
  20. little Django like. Then I add this feature to it.
  21. .. sourcecode:: python
  22. if 'pool_recycle' in kwargs:
  23. # ping db, so that mysql won't goaway
  24. PeriodicCallback(self._ping_db, kwargs['pool_recycle'] * 1000).start()
  25. .. sourcecode:: python
  26. def _ping_db(self):
  27. self.session.execute('show variables')
  28. Master Slave in SQLAlchemy
  29. ---------------------------
  30. I hate SQL! I know that orm sucks, but SQL is killing me. I am using SQLAlchemy as the
  31. orm engine, but it's not that easy to implement in a project. I wrote some snippets to
  32. make it easy to use. And I introduced a new feature in the snippet today! (2012-02-28)
  33. Master and Slave support in SQLAlchemy! What a tremendous feature. There are some answers on
  34. Stack Overflow, but I thought mine is more elegant (maybe I am wrong).
  35. .. sourcecode:: python
  36. @property
  37. def Model(self):
  38. if hasattr(self, '_base'):
  39. base = self._base
  40. else:
  41. base = declarative_base(cls=Model, name='Model')
  42. self._base = base
  43. if self.slaves:
  44. slave = random.choice(self.slaves)
  45. base.query = slave.query_property()
  46. else:
  47. base.query = self.session.query_property()
  48. return base
  49. You may have a little confuse, have a better understanding with the source code at Github_ .
  50. **You must read the whole code before continuing!**
  51. After it is integrated in your project! You can read data from slave database with:
  52. ``Member.query.filter_by(username='lepture')``
  53. For writing data into master database, using ``db.session.add(model)`` .
  54. For updating data, you should query the model with:
  55. ``db.session.query(Member).filter_by(username='lepture')``
  56. Always remember, ``db.session`` is master, ``Model.query`` is slave,
  57. ``Model.query`` is read-only!
  58. .. _document: http://docs.sqlalchemy.org/en/latest/dialects/mysql.html
  59. .. _Github: https://github.com/lepture/tornado.ext/blob/master/database.py