【 使用环境 】测试环境
【 OB or 其他组件 】ob
【 使用版本 】V4.0.0
【问题描述】使用 sqlalchemy 操作 数据库, session.rollback() 无效
【复现路径】
#!/usr/bin/python3
from sqlalchemy import create_engine, VARCHAR
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from datetime import datetime
from sqlalchemy import BOOLEAN, DATETIME, INTEGER, Column, text
username = ‘root’
password = ‘mysql’
host = ‘xxx’
port = ‘2881’
database = ‘xxx’
connection_string = f’mysql+pymysql://{username}:{password}@{host}:{port}/{database}’
engine = create_engine(connection_string)
Session = sessionmaker(bind=engine)
Base = declarative_base()
class GroupEntity(Base):
abstract = True
tablename = “t_group”
group_name = Column(VARCHAR(255), nullable=False)
group_type = Column(INTEGER, nullable=True, default=1)
session = Session()
try:
session.begin(subtransactions=True)
entity = GroupEntity(**{“group_name”: “12345”, “group_type”: 1})
session.add(entity)
session.flush()
a = 1/0
session.commit()
except Exception as e:
session.rollback()
raise e
session.close()
【附件及日志】