import pymysql

from flyers import logger


def transactional(conn: pymysql.Connection):
    def decorator(func):
        def wrapper(*args, **kwargs):
            with conn.cursor() as cursor:
                try:
                    ret = func(cursor, *args, **kwargs)
                    conn.commit()
                    return ret
                except:
                    logger.exception('execute transaction error: ')
                    conn.rollback()

        return wrapper

    return decorator
