| 1 | """ |
|---|
| 2 | Example which triggers a re-connection issue in adbapi. |
|---|
| 3 | Run with a mysql wait_timeout which is less than DELAY e.g. in /etc/my.ini: |
|---|
| 4 | |
|---|
| 5 | [mysqld] |
|---|
| 6 | wait_timeout=2 |
|---|
| 7 | """ |
|---|
| 8 | |
|---|
| 9 | from twisted.enterprise import adbapi |
|---|
| 10 | from twisted.internet import reactor |
|---|
| 11 | from twisted.python import log |
|---|
| 12 | import sys |
|---|
| 13 | |
|---|
| 14 | DELAY = 3 |
|---|
| 15 | |
|---|
| 16 | dbpool = adbapi.ConnectionPool( |
|---|
| 17 | 'MySQLdb', |
|---|
| 18 | db='mysql', |
|---|
| 19 | host='localhost', |
|---|
| 20 | user='root', |
|---|
| 21 | cp_noisy=True, |
|---|
| 22 | cp_reconnect=True, |
|---|
| 23 | cp_min=1, |
|---|
| 24 | cp_max=1 |
|---|
| 25 | ) |
|---|
| 26 | |
|---|
| 27 | def select_something(): |
|---|
| 28 | def p(r): |
|---|
| 29 | log.msg("query returned", r) |
|---|
| 30 | reactor.callLater(DELAY, select_something) |
|---|
| 31 | return dbpool.runQuery("select 'something'").addCallback(p) |
|---|
| 32 | |
|---|
| 33 | log.startLogging(sys.stdout) |
|---|
| 34 | reactor.callLater(DELAY, select_something) |
|---|
| 35 | reactor.run() |
|---|