我有一个在 Azure 上运行的 Flask 服务器,由 Azure 应用服务提供,以 sqlite3 作为数据库。我无法更新 sqlite3,因为它显示数据库已锁定
2018-11-09T13:21:53.854367947Z [2018-11-09 13:21:53,835] ERROR in app: Exception on /borrow [POST]
2018-11-09T13:21:53.854407246Z Traceback (most recent call last):
2018-11-09T13:21:53.854413046Z File "/home/site/wwwroot/antenv/lib/python3.7/site-packages/flask/app.py", line 2292, in wsgi_app
2018-11-09T13:21:53.854417846Z response = self.full_dispatch_request()
2018-11-09T13:21:53.854422246Z File "/home/site/wwwroot/antenv/lib/python3.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
2018-11-09T13:21:53.854427146Z rv = self.handle_user_exception(e)
2018-11-09T13:21:53.854431646Z File "/home/site/wwwroot/antenv/lib/python3.7/site-packages/flask/app.py", line 1718, in handle_user_exception
2018-11-09T13:21:53.854436146Z reraise(exc_type, exc_value, tb)
2018-11-09T13:21:53.854440346Z File "/home/site/wwwroot/antenv/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise
2018-11-09T13:21:53.854444746Z raise value
2018-11-09T13:21:53.854448846Z File "/home/site/wwwroot/antenv/lib/python3.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
2018-11-09T13:21:53.854453246Z rv = self.dispatch_request()
2018-11-09T13:21:53.854457546Z File "/home/site/wwwroot/antenv/lib/python3.7/site-packages/flask/app.py", line 1799, in dispatch_request
2018-11-09T13:21:53.854461846Z return self.view_functions[rule.endpoint](**req.view_args)
2018-11-09T13:21:53.854466046Z File "/home/site/wwwroot/application.py", line 282, in borrow
2018-11-09T13:21:53.854480146Z cursor.execute("UPDATE books SET stock = stock - 1 WHERE bookid = ?",(bookid,))
2018-11-09T13:21:53.854963942Z sqlite3.OperationalError: database is locked
Run Code Online (Sandbox Code Playgroud)
这是路线——
@app.route('/borrow',methods=["POST"])
def borrow():
# import pdb; pdb.set_trace()
body = request.get_json()
user_id = body["userid"]
bookid = body["bookid"]
conn = sqlite3.connect("database.db")
cursor = conn.cursor()
date = datetime.now()
expiry_date = date + timedelta(days=30)
cursor.execute("UPDATE books SET stock = stock - 1 WHERE bookid = ?",(bookid,))
# conn.commit()
cursor.execute("INSERT INTO borrowed (issuedate,returndate,memberid,bookid) VALUES (?,?,?,?)",("xxx","xxx",user_id,bookid,))
conn.commit()
cursor.close()
conn.close()
return json.dumps({"status":200,"conn":"working with datess update"})
Run Code Online (Sandbox Code Playgroud)
我尝试使用编译指示检查数据库完整性。没有完整性损失。所以我不知道是什么导致了这个错误。任何帮助表示赞赏:)
小智 9
据推测,这个解决方案对于生产工作负载来说并不安全,但至少我通过执行以下命令让它工作:
sqlite3 <database-file> 'PRAGMA journal_mode=wal;'
Run Code Online (Sandbox Code Playgroud)
运行上述命令后,存储在 Azure 文件共享上的数据库在容器 Web 应用程序内运行。
我在 Linux 上的 Docker 上使用 Azure 应用服务,并且遇到了同样的问题。如果您在 Windows 上使用 Azure 应用服务,则问题与我的不同。
问题是 /home 被挂载为 CIFS 文件系统,无法处理 SQLite3 锁。
我的解决方法是将 db.sqlite3 文件复制到 /home 以外的某个目录,并正确设置 db.sqlite3 文件及其目录的权限和所有权。然后,让我的项目读/写它。但是,这种解决方法非常尴尬。我不推荐。
我通过使用以下配置设置 azure 安装选项来获得它:
dir_mode=0777,file_mode=0777,uid=0,gid=0,mfsymlinks,nobrl,cache=strict
Run Code Online (Sandbox Code Playgroud)
但真正的解决方案是添加标志nobrl(字节范围锁定)。
添加 kubernetes 的存储类示例:
---
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name: azureclass
provisioner: kubernetes.io/azure-file
mountOptions:
- dir_mode=0777
- file_mode=0777
- uid=0
- gid=0
- mfsymlinks
- nobrl
- cache=strict
parameters:
skuName: Standard_LRS
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2880 次 |
| 最近记录: |