我在mysql中有一个表,引擎是InnoDB。
系统配置

mysql --version
mysql  Ver 14.14 Distrib 5.1.58, for redhat-linux-gnu (x86_64) using readline 5.1
python --version
Python 2.7

文件:test.py
from sqlalchemy import create_engine
engine = create_engine('mysql://test:test@localhost/test1', echo=True)

from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()

from sqlalchemy import Column, Integer, String

class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
    name = Column(String(length=20))
    fullname = Column(String(length=10))
    password = Column(String(length=20))

    def __init__(self, name, fullname, password):
        self.name = name
        self.fullname = fullname
        self.password = password

    def __repr__(self):
       return "<User('%s','%s', '%s')>" % (self.name, self.fullname, self.password)

Base.metadata.create_all(engine)

from sqlalchemy.orm import sessionmaker
Session = sessionmaker(bind=engine)

session=Session()

session.begin_nested()
ed_user = User('ed', 'Ed Jones', 'edspassword')
session.add(ed_user)
session.commit()

之后我就上了控制台
2011-09-20 12:03:02,067 INFO sqlalchemy.engine.base.Engine SELECT DATABASE()
2011-09-20 12:03:02,067 INFO sqlalchemy.engine.base.Engine ()
2011-09-20 12:03:02,070 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'character_set%%'
2011-09-20 12:03:02,071 INFO sqlalchemy.engine.base.Engine ()
2011-09-20 12:03:02,072 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'lower_case_table_names'
2011-09-20 12:03:02,072 INFO sqlalchemy.engine.base.Engine ()
2011-09-20 12:03:02,073 INFO sqlalchemy.engine.base.Engine SHOW COLLATION
2011-09-20 12:03:02,074 INFO sqlalchemy.engine.base.Engine ()
2011-09-20 12:03:02,079 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'sql_mode'
2011-09-20 12:03:02,080 INFO sqlalchemy.engine.base.Engine ()
2011-09-20 12:03:02,081 INFO sqlalchemy.engine.base.Engine DESCRIBE `users`
2011-09-20 12:03:02,082 INFO sqlalchemy.engine.base.Engine ()
2011-09-20 12:03:02,091 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
2011-09-20 12:03:02,092 INFO sqlalchemy.engine.base.Engine SAVEPOINT sa_savepoint_1
2011-09-20 12:03:02,092 INFO sqlalchemy.engine.base.Engine ()
2011-09-20 12:03:02,095 INFO sqlalchemy.engine.base.Engine INSERT INTO users (name, fullname, password) VALUES (%s, %s, %s)
2011-09-20 12:03:02,095 INFO sqlalchemy.engine.base.Engine ('ed', 'Ed Jones', 'edspassword')
2011-09-20 12:03:02,120 INFO sqlalchemy.engine.base.Engine RELEASE SAVEPOINT sa_savepoint_1
2011-09-20 12:03:02,120 INFO sqlalchemy.engine.base.Engine ()

然后我在数据库中检查这个条目并得到
mysql test1 -p
mysql> use test1
Database changed
mysql> select * from users;
Empty set (0.00 sec)

然后我想知道为什么数据库里没有条目。我检查了日志,发现有SAVEPOINT sa_SAVEPOINT_1和RELEASE SAVEPOINT sa_SAVEPOINT_1,但没有commit语句。所以我在文件中添加了一个额外的提交,所以最后几行是。
session.begin_nested()
ed_user = User('ed', 'Ed Jones', 'edspassword')
session.add(ed_user)
session.commit()
session.commit()

然后将日志转换为
2011-09-20 12:09:16,847 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
2011-09-20 12:09:16,847 INFO sqlalchemy.engine.base.Engine SAVEPOINT sa_savepoint_1
2011-09-20 12:09:16,848 INFO sqlalchemy.engine.base.Engine ()
2011-09-20 12:09:16,849 INFO sqlalchemy.engine.base.Engine INSERT INTO users (name, fullname, password) VALUES (%s, %s, %s)
2011-09-20 12:09:16,850 INFO sqlalchemy.engine.base.Engine ('ed', 'Ed Jones', 'edspassword')
2011-09-20 12:09:16,871 INFO sqlalchemy.engine.base.Engine RELEASE SAVEPOINT sa_savepoint_1
2011-09-20 12:09:16,871 INFO sqlalchemy.engine.base.Engine ()
2011-09-20 12:09:16,872 INFO sqlalchemy.engine.base.Engine COMMIT

现在我在数据库里找到了
mysql> use test1
Database changed
mysql> select * from users;
+----+------+----------+-------------+
| id | name | fullname | password    |
+----+------+----------+-------------+
|  2 | ed   | Ed Jones | edspassword |
+----+------+----------+-------------+
1 row in set (0.00 sec)

如果我们启动savepoint,这对commit 2次有意义吗?或者这是sqlalchemy的begin_嵌套行为。

最佳答案

编辑:由于答案已被接受,我进行了编辑,以便在下面的注释定义的新信息视图中更清楚地显示出来。
除非使用autocommit=True,否则事务将自动启动,因此不需要显式调用session.begin()

08-27 18:37