H2:如何设置默认架构和数据库?

时间:2014-05-26 21:29:59

标签: database spring h2 liquibase

我正在使用Liquibase进行数据库更新,并针对H2对其进行测试。

我正在使用Spring来配置属性。我用

dataSource.setUrl("jdbc:h2:mem:test_common");

连接到test_common数据库,但没有成功。

我在H2 database != Schema中意识到,所以我尝试将默认架构设为test_common

dataSource.setUrl("jdbc:h2:mem:test_common;INIT=CREATE SCHEMA test_common\\; SET SCHEMA test_common");

但这没有成功,我将日志视为

INFO 5/26/14 2:24 PM:liquibase: Dropping Database Objects in schema: TEST_COMMON.PUBLIC
INFO 5/26/14 2:24 PM:liquibase: Creating database history table with name: PUBLIC.DATABASECHANGELOG
INFO 5/26/14 2:24 PM:liquibase: Creating database history table with name: PUBLIC.DATABASECHANGELOG
INFO 5/26/14 2:24 PM:liquibase: Successfully released change log lock
INFO 5/26/14 2:24 PM:liquibase: Successfully acquired change log lock
INFO 5/26/14 2:24 PM:liquibase: Reading from PUBLIC.DATABASECHANGELOG
INFO 5/26/14 2:24 PM:liquibase: Reading from PUBLIC.DATABASECHANGELOG
INFO 5/26/14 2:24 PM:liquibase: Reading from PUBLIC.DATABASECHANGELOG
INFO 5/26/14 2:24 PM:liquibase: liquibase/changelog.xml: liquibase/2014/1-1.xml::05192014.1525::h2: Reading from PUBLIC.DATABASECHANGELOG
INFO 5/26/14 2:24 PM:liquibase: liquibase/changelog.xml: liquibase/2014/1-1.xml::05192014.1525::h2: Table network created
INFO 5/26/14 2:24 PM:liquibase: liquibase/changelog.xml: liquibase/2014/1-1.xml::05192014.1525::h2: ChangeSet liquibase/2014/1-1.xml::05192014.1525::h2 ran successfully in 5ms
INFO 5/26/14 2:24 PM:liquibase: liquibase/changelog.xml: liquibase/2014/1-1.xml::05192014.1525::h2: Reading from PUBLIC.DATABASECHANGELOG
INFO 5/26/14 2:24 PM:liquibase: liquibase/changelog.xml: liquibase/2014/1-2.xml::05192014.1525::h2: Reading from PUBLIC.DATABASECHANGELOG
INFO 5/26/14 2:24 PM:liquibase: liquibase/changelog.xml: liquibase/2014/1-2.xml::05192014.1525::h2: New row inserted into network
INFO 5/26/14 2:24 PM:liquibase: liquibase/changelog.xml: liquibase/2014/1-2.xml::05192014.1525::h2: New row inserted into network
INFO 5/26/14 2:24 PM:liquibase: liquibase/changelog.xml: liquibase/2014/1-2.xml::05192014.1525::h2: New row inserted into network
INFO 5/26/14 2:24 PM:liquibase: liquibase/changelog.xml: liquibase/2014/1-2.xml::05192014.1525::h2: New row inserted into network
INFO 5/26/14 2:24 PM:liquibase: liquibase/changelog.xml: liquibase/2014/1-2.xml::05192014.1525::h2: New row inserted into network
INFO 5/26/14 2:24 PM:liquibase: liquibase/changelog.xml: liquibase/2014/1-2.xml::05192014.1525::h2: New row inserted into network
INFO 5/26/14 2:24 PM:liquibase: liquibase/changelog.xml: liquibase/2014/1-2.xml::05192014.1525::h2: ChangeSet liquibase/2014/1-2.xml::05192014.1525::h2 ran successfully in 5ms
INFO 5/26/14 2:24 PM:liquibase: liquibase/changelog.xml: liquibase/2014/1-2.xml::05192014.1525::h2: Reading from PUBLIC.DATABASECHANGELOG

如何在H2中设置默认架构和数据库名称?

2 个答案:

答案 0 :(得分:5)

默认架构为PUBLIC

对于记录,SQL Grammar命令的H2数据库站点的SET SCHEMA页面显示:

  

新连接的默认架构为PUBLIC

该文档还指出您可以在连接时指定默认架构:

  

此设置可以附加到数据库URL:jdbc:h2:test; SCHEMA = ABC

只有一个数据库

对于访问各种数据库,H2不支持CLUSTERCATALOG的SQL标准概念。您作为JDBC URL的一部分连接到一个特定的数据库(目录)。与该数据库的连接仅限于该单个数据库。请参阅问题Can you create multiple catalogs in H2?,并附上Thomas Mueller的答案。

您可以打开另一个与另一个数据库的连接,但它完全是分开的。

因此,谈论“默认数据库”与H2数据库没有任何意义。

答案 1 :(得分:1)

看起来您正在通过Java API与Liquibase进行交互。数据库对象上有一个setDefaultSchemaName()方法,可用于设置不同的默认架构。