Don't catch exception within session
Bug #1262065 reported by
ChangBo Guo(gcb)
This bug affects 1 person
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
oslo-incubator |
Fix Released
|
Medium
|
Ben Nemec |
Bug Description
In code review, some projects are using try/except within the context
managed by session. That's wrong, need add the note in doc string,
hope others put try/except in right place.
Error will be raised in session's __exit__ handler, and any try/except
within the context will drop the ROLLBACK, need catch the error out of
the context.
Solution:
Need catch exeption out of session , except developer want to recover from failure and continue commit part of stuff of one session.
Changed in oslo: | |
assignee: | nobody → ChangBo Guo (guochbo) |
status: | New → In Progress |
Changed in oslo: | |
assignee: | ChangBo Guo (guochbo) → Ben Nemec (bnemec) |
Changed in oslo: | |
importance: | Undecided → Medium |
Changed in oslo: | |
milestone: | none → icehouse-2 |
Changed in oslo: | |
status: | Fix Committed → Fix Released |
Changed in oslo: | |
milestone: | icehouse-2 → 2014.1 |
To post a comment you must log in.
[root@guochbo- 10-7-0- 151 test]# cat ./sqlalchemy_ test.py
#!/usr/bin/env python
# This is a sqlalchemy sample code
# author: guochbo ChangBo Guo
import sqlalchemy; ext.declarative import declarative_base
from sqlalchemy import create_engine
from sqlalchemy import Table, Column, Integer, String
from sqlalchemy.
from sqlalchemy.orm import sessionmaker
from sqlalchemy.exc import IntegrityError
Base = declarative_base()
# Create engine 'mysql: //nova: nova@localhost/ nova?charset= utf8' 'sqlite: ///:memory: ' engine( connect, echo=True) bind=engine)
#connect=
connect=
engine = create_
Session = sessionmaker(
session = Session()
# Create one table
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(String(36))
def __init__(self, id, name):
self.id = id
self.name = name
Base.metadata. create_ all(engine)
user1= User(1, 'ed1') begin(subtransa ctions= True):
session. add(user1)
session. add(user2)
print( 'get exception1 in with block')
user2 = User(1, 'ed2')
# duplicated entry will rollback
try:
with session.
try:
except Exception :
except IntegrityError:
print('get IntegrityError2 out of with block')
# catch exception in contex won't rollback begin(subtransa ctions= True):
session1. add(user1)
dict1= {}
dict1. pop('test' )
session1. add(user2)
print( 'get exception3 in with block')
session1= Session()
try:
with session1.
try:
except Exception :
except IntegrityError:
print('get IntegrityError4 out of with block')
[root@guochbo- 10-7-0- 151 test]# ./sqlalchemy_ test.py engine. base.Engine PRAGMA table_info("users") engine. base.Engine () engine. base.Engine
2013-12-17 23:52:06,547 INFO sqlalchemy.
2013-12-17 23:52:06,548 INFO sqlalchemy.
2013-12-17 23:52:06,548 INFO sqlalchemy.
CREATE TABLE users (
id INTEGER NOT NULL,
name VARCHAR(36),
PRIMARY KEY (id)
)
2013-12-17 23:52:06,548 INFO sqlalchemy. engine. base.Engine () engine. base.Engine COMMIT engine. base.Engine BEGIN (implicit) engine. base.Engine INSERT INTO users (id, name) VALUES (?, ?) engine. base.Engine ((1, 'ed1'), (1, 'ed2')) engine. base.Engine ROLLBACK engine. base.Engine BEGIN (implicit) engine. base.Engine INSERT INTO users (id, name) VALUES (?, ?) engine. base.Engine (1, 'ed1')
2013-12-17 23:52:06,549 INFO sqlalchemy.
2013-12-17 23:52:06,550 INFO sqlalchemy.
2013-12-17 23:52:06,550 INFO sqlalchemy.
2013-12-17 23:52:06,550 INFO sqlalchemy.
2013-12-17 23:52:06,551 INFO sqlalchemy.
get IntegrityError2 out of with block
get exception3 in with block
2013-12-17 23:52:06,551 INFO sqlalchemy.
2013-12-17 23:52:06,552 INFO sqlalchemy.
2013-12-17 23:52:06,552 INFO sqlalchemy.