简体   繁体   English

SQLite3 数据库被锁定在 Azure

[英]SQLite3 database is Locked in Azure

I have a Flask server Running on Azure provided by Azure App services with sqlite3 as a database.我有一个 Flask 服务器运行在 Azure 应用程序服务提供的 Azure 上,以 sqlite3 作为数据库。 I am unable to update sqlite3 as it is showing that database is locked我无法更新 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

Here is the route -这是路线——

@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"})

I tried checking the database integrity using pragma.我尝试使用 pragma 检查数据库完整性。 There was no integrity loss.没有完整性损失。 So I don't know what might be causing that error.所以我不知道是什么导致了这个错误。 Any help is Appreciated:)感谢任何帮助:)

I use Azure app service on Docker on Linux, and have the same issue.我在 Linux 上的 Docker 上使用 Azure 应用服务,并且遇到了同样的问题。 If you are using Azure app service on Windows, the problem is different from mine.如果您在 Windows 上使用 Azure 应用服务,则问题与我的不同。

The problem is that /home is mounted as CIFS filesystem which can not deal with SQLite3 lock.问题是 /home 被挂载为 CIFS 文件系统,无法处理 SQLite3 锁。

My workaround is to copy db.sqlite3 file to some directory other than /home, and properly set permissions and ownerships of the db.sqlite3 file and its directory as well.我的解决方法是将 db.sqlite3 文件复制到 /home 以外的某个目录,并正确设置 db.sqlite3 文件及其目录的权限和所有权。 Then, let my project read/write it.然后,让我的项目读/写它。 However, this workaround is pretty awkward.但是,这种解决方法非常尴尬。 I don't recommned.我不推荐。

Presumably this solution is not safe for production workloads but at least I got it working by executing the following command:据推测,此解决方案对于生产工作负载并不安全,但至少我通过执行以下命令使其工作:

sqlite3 <database-file> 'PRAGMA journal_mode=wal;'

After running the above command, my database stored on an Azure File share works inside a container Web App.运行上述命令后,我存储在 Azure 文件共享上的数据库在容器 Web 应用程序中运行。

I got it by setting up the azure mount options with the following configuration:我通过使用以下配置设置 azure 安装选项来获得它:

dir_mode=0777,file_mode=0777,uid=0,gid=0,mfsymlinks,nobrl,cache=strict

But the real solution is to add the flag nobrl .但真正的解决方案是添加标志nobrl

Add storageclass example for kubernetes:为 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

This answer appears toward the top of a typical Google search for this issue so I thought I'd add a couple of additional tips:这个答案出现在针对这个问题的典型谷歌搜索的顶部,所以我想我会添加一些额外的提示:

For those running JavaScript and using Sequelize as the interface to your SQLite DB, running对于那些运行 JavaScript 并使用 Sequelize 作为 SQLite 数据库接口的用户,运行

await sequelize.query('PRAGMA journal_mode=WAL;')

prior to creating your database will allow you to read/write the DB file in an Azure web app running under a Linux service plan.在创建数据库之前将允许您在 Linux 服务计划下运行的 Azure web 应用程序中读取/写入数据库文件。 I have a separate script that creates one via a call to sequelize.sync() .我有一个单独的脚本,它通过调用sequelize.sync()创建一个脚本。 I'm storing the DB file in a separate directory under /home within the file system for the Linux container.我将 DB 文件存储在 Linux 容器文件系统内 /home 下的单独目录中。 It seems to run fine and my workload is expected to be very light.它似乎运行良好,我的工作量预计会很轻。 Note that you don't need to set the journal mode again when your app starts and you try to connect to the database, that mode will be set in the file itself (this wasn't obvious from the SQLite docs).请注意,当您的应用程序启动并尝试连接到数据库时,您不需要再次设置日志模式,该模式将在文件本身中设置(这在 SQLite 文档中并不明显)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM