def get_next_id(self):
        try:
            query = {'_id': 'last_id'}
            update = {'$inc': {'id': 1}}
            fn = self.db.internal.find_and_modify
            row = fn(query, update, upsert=True, new=True)
        except Exception as e:
            raise DatabaseException(e)
        return row['id']
def __exit__(self, exc_type, exc_value, traceback):
        connection = get_connection(self.using)
        try:
            self._no_monkey.__exit__(self, exc_type, exc_value, traceback)
        except DatabaseError:
            transaction_states[self.using].rollback()
        else:
            if not connection.closed_in_transaction and exc_type is None and \
                    not connection.needs_rollback:
                if transaction_states[self.using]:
                    transaction_states[self.using].commit()
            else:
                transaction_states[self.using].rollback()