如果我们使用SQLAlchemy的session.begin_nested,为什么不提交更改?

时间:2011-09-20 06:42:49

标签: python mysql session sqlalchemy

我在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

file: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)

然后我想知道为什么DB中没有条目。我检查了日志,发现有SAVEPOINT sa_savepoint_1和RELEASE SAVEPOINT sa_savepoint_1但没有提交语句。所以我在文件中添加了一个额外的提交,所以最后几行是。

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)

如果我们启动保存点,这对于提交2次的意义是什么?或者是sqlalchemy的begin_nested的这种行为。

1 个答案:

答案 0 :(得分:2)

编辑由于答案已被接受,因此我进行了编辑,以便在以下评论所定义的新信息视图中更加清晰。

除非您使用autocommit=True,否则交易会自动启动,因此您无需明确调用session.begin()