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

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

问题描述

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

  dataSource {
pooled = true
jmxExport = true
driverClassName =org.h2.Driver
username =sa
password =
dbCreate =create -drop//创建,创建,删除,更新,验证,
url =jdbc:h2:mem:devDb; MVCC = TRUE; LOCK_TIMEOUT = 10000; DB_CLOSE_ON_EXIT = FALSE

我试图为此数据库设置连接IntelliJ IDEA的数据库客户端工具。我开始建立这样的连接







然后选择Schemas&表选项卡。





测试连接按钮表示成功,但您可以从红色圆圈看到,没有找到表格。看来我已经正确设置了连接到h2服务器,但不是架构本身。



顺便说一句,我尝试在应用程序运行后设置此连接,所以我确信模式/表确实存在。

解决方案

您的配置适用于 h2:mem 数据库。内存数据库在连接到它们时没有表格,并且任何&所有连接关闭时,所有表都会丢失。而且,内存数据库中的一个(named)内容对于打开它的是唯一的。从 H2文档


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

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



如果您想将应用程序和IntelliJ IDEA(或任何其他数据库工具)连接到H2数据库,您需要在您的应用程序中使用嵌入式数据库(写入文件)并使用


  1. 混合模式允许IntelliJ IDEA(和/或其他数据库工具)连接到它

  2. 使用服务器模式数据库
  3. >

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

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

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

Then in the following dialog, I enter the JDBC URL

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

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.

解决方案

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:

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)

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.

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. 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

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

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

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