how to set autocommit = 1 in a sqlalchemy.engine.Connection

后端 未结 5 2169
旧巷少年郎
旧巷少年郎 2021-02-20 00:01

In sqlalchemy, I make the connection:

 conn = engine.connect()

I found this will set autocommit = 0 in my mysqld log. Now I want to set autocom

5条回答
  •  面向向阳花
    2021-02-20 00:25

    From The SQLAlchemy documentation: Understanding autocommit

    conn = engine.connect()
    conn.execute("INSERT INTO users VALUES (1, 'john')")  # autocommits
    

    The “autocommit” feature is only in effect when no Transaction has otherwise been declared. This means the feature is not generally used with the ORM, as the Session object by default always maintains an ongoing Transaction.

    Full control of the “autocommit” behavior is available using the generative Connection.execution_options() method provided on Connection, Engine, Executable, using the “autocommit” flag which will turn on or off the autocommit for the selected scope. For example, a text() construct representing a stored procedure that commits might use it so that a SELECT statement will issue a COMMIT:

    engine.execute(text("SELECT my_mutating_procedure()").execution_options(autocommit=True))
    

提交回复
热议问题