gpt4 book ai didi

How do I allow multiple users to connect to my H2 database simultaneously?(如何允许多个用户同时连接到我的H2数据库?)

转载 作者:bug小助手 更新时间:2023-10-25 14:15:17 30 4
gpt4 key购买 nike



I am using H2 for database management, and this is what I would like to do:

我正在使用H2进行数据库管理,这是我想做的:



I would like to allow multiple users to access a database at the same time. I've read a bit about "MULTI_THREADED=TRUE", "LOCK_FILE=NO", and "AUTO_SERVER=TRUE". I've also read that "LOCK_FILE=NO" can be dangerous, because it can corrupt the database. I definitely would not want this, so I'm assuming that is a bad way to go. I've also tried to close the connection immediately after a record is accessed, whether it is being read from or written to. So far, nothing seems to work. The application is not allowing me to read from or write to the database if the database has been connected to in a separate instance of the application (ex: on another computer). Once I completely close the application on one computer, I am able to access the database records.

我希望允许多个用户同时访问一个数据库。我读过一些关于“MULTI_THREADED=TRUE”、“LOCK_FILE=NO”和“AUTO_SERVER=TRUE”的内容。我还了解到,“lock_file=no”可能是危险的,因为它会损坏数据库。我绝对不会想要这样的,所以我想这是一种糟糕的方式。我还尝试在访问记录后立即关闭连接,无论是读取还是写入记录。到目前为止,一切似乎都没有奏效。如果数据库已连接到应用程序的单独实例中(例如,在另一台计算机上),则应用程序不允许我读取或写入数据库。一旦我在一台计算机上完全关闭了应用程序,我就能够访问数据库记录了。



How do I allow multiple users to connect to the H2 database at the same time without compromising the safety of the database?

如何允许多个用户同时连接到H2数据库,而不会影响数据库的安全性?


更多回答

Funny how the first answer you get is exactly what I've been saying...

有趣的是,你得到的第一个答案正是我一直在说的.

Not exactly. None of your responses were very direct or helpful.

不完全同意。你的回复没有一个是非常直接或有帮助的。

So "use the server mode" wasn't helpful...shame

所以“使用服务器模式”无济于事…真可惜

Okay, I don't think using "AUTO_SERVER=TRUE" alone fixed the problem. Should that alone have fixed the problem? Right now, I have it set up to immediately commit and close the connection after a query is made, so the lock file is created and then deleted. Is this an efficient way to allow multiple users to access the database?

好吧,我认为单独使用“AUTO_SERVER=TRUE”并不能解决问题。单凭这一点就应该解决这个问题吗?现在,我将其设置为在进行查询后立即提交并关闭连接,这样就创建了锁文件,然后将其删除。这是允许多个用户访问数据库的有效方式吗?

优秀答案推荐

It looks like you are using H2 in embedded mode, which only allows one database connection at a time. See connection modes in the documentation for details.

看起来您正在嵌入式模式下使用h2,这一次只允许一个数据库连接。有关详细信息,请参阅文档中的连接模式。



If you need support for multiple connections, including from multiple application instances, then you need to start H2 in server mode instead and use the appropriate connection URLs for this mode.

如果您需要支持多个连接,包括来自多个应用程序实例的连接,那么您需要改为在服务器模式下启动h2,并为该模式使用适当的连接URL。



AUTO_SERVER=TRUE allows multiple connections mode.

AUTO_SERVER=TRUE允许多连接模式。



The accepted answer is wrong. It is possible to have multiple connections without starting a separate H2 server manually by using Automatic Mixed Mode:

公认的答案是错误的。可以使用自动混合模式拥有多个连接,而无需手动启动单独的H2服务器:



Multiple processes can access the same database without having to start the server manually. To do that, append ;AUTO_SERVER=TRUE to the database URL. You can use the same database URL independent of whether the database is already open or not. This feature doesn't work with in-memory databases.



So, to allow multiple users to connect to an H2 database simultaneously, append ;AUTO_SERVER=true data source URL property.

因此,要允许多个用户同时连接到一个H2数据库,需要添加;AUTO_SERVER=TRUE数据源URL属性。


The only caveat is that you can't use this with the in-memory database. The next best thing (based on simplicity) is file-based database.

唯一需要注意的是,您不能将其用于内存数据库。下一个最好的东西(基于简单性)是基于文件的数据库。




Example for a spring app

Spring应用程序的示例


application.properties to use a file-based database:

要使用基于文件的数据库,请执行以下操作:


spring.datasource.url=jdbc:h2:file:./data/testdb;AUTO_SERVER=TRUE
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.generate-ddl=true

When launching the app this will create a new database file as ./data/testdb.mv.db or use it if it already exists.

在启动应用程序时,这将创建一个新的数据库文件./data/testdb.mv.db,如果它已经存在,也可以使用它。


Now you can connect to this database from another source using the same url h2:file:./data/testdb;AUTO_SERVER=TRUE. It's important not to forget to append ;AUTO_SERVER=true to the url when connecting from another source, otherwise you will get The file is locked exception.

现在,您可以使用相同的url h2:file:./data/testdb;AUTO_SERVER=TRUE从另一个源连接到该数据库。从其他来源连接时,不要忘记将;AUTO_SERVER=TRUE附加到url,否则会出现文件被锁定异常。


更多回答

So, is there a way to use "AUTO_SERVER=TRUE" to do this? Would that make it mixed?

那么,有没有办法使用“AUTO_SERVER=TRUE”来实现这一点呢?这会让它变得复杂吗?

And, if I make the Auto Server true, would that allow multiple connections?

如果我将Auto Server设为真,是否会允许多个连接?

The automatic mixed mode (i.e. AUTO_SERVER=TRUE) allows multiple concurrent connections to the database. However, I wouldn't recommend it, due to the constraints that the first connection starts the server and when that connection is closed, so is the server and all remote connections are therefore closed, and pending transactions will be rolled back.

自动混合模式(即AUTO_SERVER=TRUE)允许多个并发连接到数据库。但是,我不建议这样做,因为第一个连接必须启动服务器,并且当该连接关闭时,服务器也会启动,因此所有远程连接都会关闭,挂起的事务将回滚。

The documentation states that in embedded mode there is no limit on the number of open connections.

文档指出,在嵌入式模式下,对打开的连接数量没有限制。

@StefanNeuhaus but not by multiple users

@Stefan Neuhaus,但不是由多个用户

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com