【问题标题】:Bottle Cork creates a user, but it doesn't save it to the databaseBottle Cork 创建用户,但不会将其保存到数据库中
【发布时间】:2014-09-03 11:36:41
【问题描述】:

我正在使用带有 SQLite 后端的 Bottle Cork 进行身份验证,并且正在尝试创建用户:

@bottle.post('/ajax/users/create/')
def create_user():
    response.content_type = 'application/json'
    password = bottle.request.forms.get('password')
    username = bottle.request.forms.get('username')
    role = bottle.request.forms.get('role')
    ret = create_user_by_admin(username, password, role)
    response.content_type = 'application/json'
    return dumps({"status": ret.get('ok')})

def create_user_by_admin(username, password, role):
    try:
        auth.create_user(username, role, password)
        return dict(ok=True, msg='')
    except Exception, e:
        return dict(ok=False, msg=e.message)

瓶应用日志:

127.0.0.1 - - [13/Jul/2014 13:56:38] "POST /ajax/users/create/ HTTP/1.1" 200 16

jQuery 发布响应:{"status": true}

如果我是第一次创建用户,则响应为 True(用户成功创建),但数据库中没有新用户。下一个响应将返回 False 和错误消息:

127.0.0.1 - - [13/Jul/2014 13:56:38] "POST /ajax/users/create/ HTTP/1.1" 200 16
/home/art/projects/leggera/app/views.py:33: DeprecationWarning: BaseException.message        has been deprecated as of Python 2.6
return dict(ok=False, msg=e.message)

也就是说,创建了一个用户,但是...数据库中仍然没有新用户(我正在使用 SQLiteMan 查看数据库的内容)!
数据库未锁定,chmod +rwx... 请问这个问题怎么解决?

【问题讨论】:

  • 看看backend source code 我会说这些更改永远不会提交。任何地方都没有自动提交配置,也没有显式提交。这是一个已经存在一年多的软件包中的一个令人惊讶的错误。

标签: python sqlite bottle


【解决方案1】:

后端实现永远不会提交事务。

Cork 问题跟踪器中有两个问题显示人们是如何解决这个问题的:

  • Issue 66 是更新示例以添加 backend.connection.commit() 调用的拉取请求:

    auth.create_user(username, role, password)
    auth._store.connection.commit()
    
  • Issue 63 是关于 SQLite 后端的线程问题;但包含的代码使用后端的子类来设置隔离级别以强制自动提交。

请注意,issue 63 表明后端完全不适合多线程环境;我会改用 SQLAlchemy 后端,因为它可以正确处理 SQLite 和线程。

快速粗略地浏览一下该后端也会使它看起来也无法处理提交。

【讨论】:

  • 是的,auth._store.connection.commit() 有效!非常感谢!
【解决方案2】:

我认为该解决方案是在 sqlite_backend.py 中重新定义函数(save_users、save_roles、save_pending_registrations):

def save_users(self): self.connection.commit() def save_roles(self): self.connection.commit() def save_pending_registrations(self): self.connection.commit()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-19
    • 2020-10-10
    • 2020-02-29
    相关资源
    最近更新 更多