简史:
从过去的几周开始,我们的金字塔Web应用程序出现了间歇性问题 - 某些数据库事务没有得到承诺。虽然观点成功回应。
我们一直在努力寻找一种模式,却没有成功。 在调试时,成功和不成功的txn之间的唯一区别如下:
成功的日志:
2017-10-22 08:22:41,034 [3360] [sqlalchemy.engine.base.Engine:INFO] ROLLBACK
2017-10-22 08:22:41,034 [3360] [txn.140257118390016:DEBUG] commit
<zope.sqlalchemy.datamanager.SessionDataManager object at 0x7f9019dbf990>
2017-10-22 08:22:41,034 [3360] [txn.140257118390016:DEBUG] commit
UNSUCCESFUL LOGS:
2017-10-22 04:45:10,504 [3360] [txn.140257118390016:DEBUG] commit
2017-10-22 04:45:10,504 [3360] [sqlalchemy.engine.base.Engine:INFO] ROLLBACK
请注意,SessionDataManager对象丢失,语句序列相反。
应用程序设置:
相关代码段:
数据库初始化 的初始化 .db的
import sqlalchemy
import zope.sqlalchemy
import zope.sqlalchemy.datamanager
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
.
.
.
Session = sessionmaker()
engine = sqlalchemy.create_engine(settings['dburl'])
config.registry['sql_engine'] = engine
config.add_request_method(_session, name='db', reify=True)
_session方法如下
def _session(request):
engine = request.registry['sql_engine']
session = Session(bind=engine)
try:
tm = request.tm
except AttributeError:
pass
else:
zope.sqlalchemy.register(session, transaction_manager=tm)
return session
交易管理器挂钩如下:
config.add_settings({
"tm.attempts": 3,
"tm.manager_hook": lambda request: transaction.TransactionManager(),
})
config.include('pyramid_tm')
无任何异常返回的服务代码是:
def create_obj(request, data):
# Create an obj from passed data.
created = updated = datetime.utcnow()
obj = models.Obj(**data)
obj.created = created
obj.updated = updated
request.db.add(obj)
try:
request.db.flush() # to use the returned id
except Exception as e:
log.error('Createlog 1: Exception %s occurred while creating annotation %s', e, obj.id)
raise HTTPConflict()
return obj
我试图在视图中捕获异常,但是这并没有给我提供更多有关它的详细信息
@view_config(context=SqlAlchemyOperationalError)
def failed_sqlalchemy(exception, request):
log.warning("There was an error in SqlAlchemy!")
log.warning(request.exception)
pass
有人可以帮助我 1.如何获得有关该问题的更多信息? 2.我看到的日志模式的意义是什么? 3.由于某些竞争条件,我的会话是否会被破坏,我不知道?
很高兴提供所需的任何进一步细节。