Jboss AS 7.1.1 Access/Query 嵌入式 H2 数据库

Jboss AS 7.1.1 Access/Query Embedded H2 Database

我正在尝试访问我的嵌入式 h2 服务器并进行查询。我可以访问,但是我创建的表没有出现在 Jboss H2 工具上。

这是我的persistence.xml

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
    version="2.1">
    <persistence-unit name="primary">
        <jta-data-source>java:jboss/datasources/xxxDS</jta-data-source>
        <properties>
            <!-- For auto create tables on startup -->
            <property name="hibernate.hbm2ddl.auto" value="create-drop" />
            <property name="hibernate.show_sql" value="false" />
        </properties>
    </persistence-unit>
</persistence>

xxx-ds.xml

<?xml version="1.0" encoding="UTF-8"?>
<datasources xmlns="http://www.jboss.org/ironjacamar/schema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.jboss.org/ironjacamar/schema http://docs.jboss.org/ironjacamar/schema/datasources_1_0.xsd">

    <!--  start H2 DATABASE -->
    <datasource jndi-name="java:jboss/datasources/xxxDS"
        pool-name="xxx" enabled="true"
        use-java-context="true">
        <connection-url>jdbc:h2:mem:xxx;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1</connection-url>
        <driver>h2</driver>
        <security>
            <user-name>sa</user-name>
            <password>sa</password>
        </security>
    </datasource>
</datasources>

通过Jboss工具访问H2嵌入式数据库(如图测试成功): java -jar /opt/jboss/jboss-as-7.1.1.Final/modules/com/h2database/h2/main/h2-1.3.161.jar

我可以连接,但看不到我在应用程序中创建的表。我只看到 h2 系统表。

如何查看我的应用程序正在保留的表?

您正在使用内存数据库,因此无法从不同的 JVM 和 class 加载程序环境进行访问。

当您使用 Jboss 7 时,您可以使用 h2console 快速入门来查看数据库内容。 Here is the project,它构建了一个 war,您只需将其部署到 <jboss_home>/(domain|standalone)/deployments

另请注意,使用选项 create-drop for hibernate.hbm2ddl.auto 将删除任何数据库对象,当应用程序未部署。

如果您有机会更改 h2 数据库配置,您可以设置其 服务器模式 。请参阅 this short tutorial 了解如何快速完成:)