delimiter //CREATE PROCEDURE test_func()BEGINSELECT @@AUTOCOMMIT `autocommit`;END//delimiter ;
engine = create_engine(DSN, echo=True).execution_options(autocommit=True)
2014-03-27 14:45:36,956 INFO sqlalchemy.engine.base.Engine SELECT DATABASE()2014-03-27 14:45:36,956 INFO sqlalchemy.engine.base.Engine ()2014-03-27 14:45:37,293 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'character_set%%'2014-03-27 14:45:37,293 INFO sqlalchemy.engine.base.Engine ()2014-03-27 14:45:37,350 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'sql_mode'2014-03-27 14:45:37,350 INFO sqlalchemy.engine.base.Engine ()2014-03-27 14:45:37,416 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)2014-03-27 14:45:37,417 INFO sqlalchemy.engine.base.Engine CALL test_func()2014-03-27 14:45:37,417 INFO sqlalchemy.engine.base.Engine ()
I'm having a problem where I'm doing writes to MySQL that aren't being committed. I'm obviously calling the procedure from an execute(). I'm okay with explicit commits, but since all of my data logic is in procedures (all operations are one step), autocommit is preferable. I'm using 0.9.1 .Until recently, the application was only doing SELECTs, so I hadn't noticed.There seem to be dozens of offered solutions online, but none of them work.
- I've tried setting the autocommit to True at the engine level, but I get the "Commands out of sync" error:engine = create_engine(DSN, echo=True).execution_options(autocommit=True)
- I've tried setting the autocommit at the session level, but the result indicates that autocommit is "0" (the option was ignored):
- I've tried setting the autocommit on a text(), but it's ignored (with the same result as the above):r = session.execute(text(query, autocommit=True))
- I've also tried to do the actual commits, both directly or using the context-manager, but I get the "out of sync" message every time.
OK, you seem to have found the autocommit flag on text(), this is the correct approach in this case.I can’t get your stored proc to run as creating a proc on my machine produces some “thread stack overrun” error I don’t have the time to figure out, but a simple test of just a SELECT with autocommit=True illustrates the COMMIT being called in the log:
can’t reproduce - test script:from sqlalchemy import *e = create_engine("mysql://scott:tiger@localhost/test", echo=True).execution_options(autocommit=True)r = e.execute(text('SELECT 1’))output, includes the COMMIT as expected:2014-03-27 15:11:14,324 INFO sqlalchemy.engine.base.OptionEngine SELECT 12014-03-27 15:11:14,324 INFO sqlalchemy.engine.base.OptionEngine ()2014-03-27 15:11:14,324 INFO sqlalchemy.engine.base.OptionEngine COMMIT
In your case, you should use a simple MySQL-python script first, get the SP to run and commit, without any “out of sync” messages. I think that is the actual problem you’re having.
query = 'SELECT @@AUTOCOMMIT'
dsn = '<DSN string>'
def alchemy_test():e = create_engine(dsn, echo=True)Session = sessionmaker()Session.configure(bind=e)session = Session()r = session.execute(text(query, autocommit=True))print(list(r))
2014-03-27 17:47:16,215 INFO sqlalchemy.engine.base.Engine SELECT DATABASE()2014-03-27 17:47:16,215 INFO sqlalchemy.engine.base.Engine ()2014-03-27 17:47:16,548 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'character_set%%'2014-03-27 17:47:16,548 INFO sqlalchemy.engine.base.Engine ()2014-03-27 17:47:16,604 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'sql_mode'2014-03-27 17:47:16,604 INFO sqlalchemy.engine.base.Engine ()2014-03-27 17:47:16,670 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)2014-03-27 17:47:16,670 INFO sqlalchemy.engine.base.Engine SELECT @@AUTOCOMMIT2014-03-27 17:47:16,670 INFO sqlalchemy.engine.base.Engine ()[(0L,)]
def direct_test():import MySQLdbconn = MySQLdb.connect(host='db.host.com', user='abc', passwd="def", db="ghi", port=3307)conn.autocommit(True)c = conn.cursor()c.execute(query)print(c.fetchone())
(1L,)
Any idea why SA is falling down on this?
Any idea why SA is falling down on this?
Direct example:def direct_test():import MySQLdbconn = MySQLdb.connect(host='db.host.com', user='abc', passwd="def", db="ghi", port=3307)conn.autocommit(True)c = conn.cursor()c.execute(query)print(c.fetchone())Result:(1L,)