(self, config)
| 54 | @contextlib.contextmanager |
| 55 | @require(config = 'ddserver.config:Config') |
| 56 | def cursor(self, config): |
| 57 | # Ensure we have a connection for this thread |
| 58 | if not hasattr(self.thread_local, 'connection'): |
| 59 | connection = MySQLdb.connect(host = config.db.host, |
| 60 | port = config.db.port, |
| 61 | user = config.db.username, |
| 62 | passwd = config.db.password, |
| 63 | db = config.db.name, |
| 64 | cursorclass = MySQLdb.cursors.DictCursor, |
| 65 | charset = 'utf8') |
| 66 | setattr(self.thread_local, 'connection', connection) |
| 67 | |
| 68 | else: |
| 69 | # Use existing connection |
| 70 | connection = getattr(self.thread_local, 'connection') |
| 71 | |
| 72 | # Reconnect if connection is down |
| 73 | connection.ping(True) |
| 74 | |
| 75 | cursor = connection.cursor() |
| 76 | |
| 77 | try: |
| 78 | yield cursor |
| 79 | |
| 80 | except: |
| 81 | connection.rollback() |
| 82 | raise |
| 83 | |
| 84 | else: |
| 85 | connection.commit() |
| 86 | |
| 87 | finally: |
| 88 | cursor.close() |
no outgoing calls
no test coverage detected