def get_connection(path, indexer=False, callback=None): """Get a connection to the database. This function reuses already existing connections. """ global _index_connection, _search_connections try: _connection_attemts = _new = 0 connection = None while _connection_attemts <= 3: try: if indexer: if _index_connection is None: _new = True _index_connection = IndexerConnection(path) connection = _index_connection else: thread = get_current_thread() if thread not in _search_connections: _new = True _search_connections[ thread] = connection = SearchConnection(path) else: connection = _search_connections[thread] except (xapian.DatabaseOpeningError, xapian.DatabaseLockError): time.sleep(0.5) _connection_attemts += 1 else: break if callback: callback(connection) if not _new: connection.reopen() yield connection finally: if connection is not None: connection.close() _index_connection = None
def __init__(self, dirname): self.dbPath = os.path.abspath(dirname) self.conn = SearchConnection(self.dbPath)