使用 IntelliJ 数据库客户端连接到 H2 数据库 [英] Connect to H2 database using IntelliJ database client

查看:30
本文介绍了使用 IntelliJ 数据库客户端连接到 H2 数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Grails 应用程序在开发模式下使用 h2 数据库(Grails 应用程序的默认行为).DataSource.groovy 中的 DB 连接设置是

My Grails app uses a h2 database in dev mode (the default behaviour for Grails apps). The DB connection settings in DataSource.groovy are

dataSource {
    pooled = true
    jmxExport = true
    driverClassName = "org.h2.Driver"
    username = "sa"
    password = ""
    dbCreate = "create-drop" // one of 'create', 'create-drop', 'update', 'validate', ''
    url = "jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE"
}

我正在尝试使用 IntelliJ IDEA 的数据库客户端工具为此数据库设置连接.我开始像这样创建连接

I'm trying to setup a connection for this database using IntelliJ IDEA's database client tools. I start off creating the connection like so

然后在下面的对话框中,我输入 JDBC URL

Then in the following dialog, I enter the JDBC URL

并在架构和表"选项卡上选择所有可用的数据库.

And choose all available databases on the "Schemas & Tables" tab.

测试连接"按钮表示成功,但正如您从红色圆圈中看到的那样,没有找到任何表.似乎我已经正确设置了到 h2 服务器的连接,但没有设置架构本身.

The "Test Connection" button indicates success, but as you can see from the red circle, no tables are found. It seems like I've correctly setup a connection to the h2 server, but not the schema itself.

顺便说一句,我尝试在应用程序运行后设置此连接,因此我确定架构/表确实存在.

BTW, I try to setup this connection once the app is running, so I'm sure that the schema/tables do actually exist.

推荐答案

您的配置适用于 h2:mem 数据库.内存数据库在连接到它们时没有表,并且任何 &当所有连接都关闭时,所有表都将丢失.此外,内存数据库中的(命名的)对于打开它的 JVM 进程来说是唯一的.来自 H2 文档:

Your configuration is for an h2:mem database. Memory Databases have no tables upon connecting to them, and any & all tables are lost when all the connections are closed. Furthermore, a (named) in memory database is unique to the JVM process that opens it. From the H2 documentation:

有时需要多个连接到同一个内存数据库.在这种情况下,数据库 URL 必须包含名称.示例:jdbc:h2:mem:db1.使用这个 URL 访问同一个数据库只能在同一个虚拟机和类加载器环境中工作.(强调)

Sometimes multiple connections to the same in-memory database are required. In this case, the database URL must include a name. Example: jdbc:h2:mem:db1. Accessing the same database using this URL only works within the same virtual machine and class loader environment. (Emphasis added)

这意味着 IDEA 将在其 JVM(和类加载器)空间中创建一个唯一的 devDb,而您的应用程序将在其 JVM(和类加载器)空间中创建一个唯一的 devDb.您无法从外部 JVM 进程连接到内存数据库.

This means IDEA will create a unique devDb in its JVM (and classloader) space and your application will create a unique devDb in its JVM (and classloader) space. You can not connect to an in memory database from an external JVM process.

如果要将应用程序和 IntelliJ IDEA(或任何其他数据库工具)同时连接到 H2 数据库,则需要

If you want to connect both your application and IntelliJ IDEA (or any other DB tool) to an H2 database at the same time, you will need to either

  1. 在您的应用程序中使用嵌入式数据库(写入文件)并使用 混合模式 以允许 IntelliJ IDEA(和/或其他数据库工具)连接到它
  2. 使用服务器模式数据库
  1. use an embedded database (that writes to a file) in your application and use Mixed Mode to allow IntelliJ IDEA (and/or other database tools) to connect to it
  2. use a server mode database

参见 http://www.h2database.com/html/features.html#connection_modes 了解更多信息.

See http://www.h2database.com/html/features.html#connection_modes for more information.

这篇关于使用 IntelliJ 数据库客户端连接到 H2 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆