【问题标题】:View content of embedded H2 database started by Spring查看Spring启动的嵌入式H2数据库内容
【发布时间】:2013-07-22 03:22:50
【问题描述】:

由于以下配置,我想在网络浏览器中查看由 Spring 启动的 H2 数据库的内容:

<jdbc:embedded-database id="dataSource" type="H2" />

<jdbc:initialize-database data-source="dataSource">
    <jdbc:script location="classpath:db/populateDB.sql"/>
</jdbc:initialize-database>

我在日志中搜索了 JDBC URL:

DEBUG o.s.j.d.SimpleDriverDataSource - Creating new JDBC Driver Connection to [jdbc:h2:mem:dataSource;DB_CLOSE_DELAY=-1]

这样我就可以如下填写连接表:

但不幸的是,数据库仍然是空的,而它不应该是由于 populateDB.sql 脚本造成的。

有什么想法吗?

谢谢!

【问题讨论】:

    标签: spring h2


    【解决方案1】:

    View content of H2 or HSQLDB in-memory database 几乎相同的问题。

    只需将以下内容添加到您的配置中。

    <bean id="h2Server" class="org.h2.tools.Server" factory-method="createTcpServer" init-method="start" destroy-method="stop" depends-on="h2WebServer">
        <constructor-arg value="-tcp,-tcpAllowOthers,-tcpPort,9092"/>
    </bean>
    <bean id="h2WebServer" class="org.h2.tools.Server" factory-method="createWebServer" init-method="start" destroy-method="stop">
        <constructor-arg value="-web,-webAllowOthers,-webPort,8082"/>
    </bean>
    

    这将在与嵌入式数据库相同的 JVM 中启动 H2 Web 控制台和 TCP 服务器,以便您可以使用 Web 浏览器访问端口 8082(输入 jdbc:h2:mem:dataSource 作为 URL),或使用访问端口 9092外部 SQL 客户端(例如 SQuirreLSQL)并查看相同的数据。

    【讨论】:

    • 知道 tcp 的 url 是什么吗?我尝试使用 jdbc:h2:tcp://localhost:9092/dataSource 但它给了我一个名为 DATASOURCE 的空数据库。 Web 控制台使用 jdbc:h2:mem:dataSource 在 8082 上显示我的表
    • 解决了.. url 需要有“mem:” 字.. 新 url: jdbc:h2:tcp://localhost:9092/mem:dataSource
    • 知道如何在代码中进行等效配置,如果您已经配置了 DataSource using EmbeddedDatabaseBuilder along these lines
    • 我已经按照你说的做了配置。但是当我尝试使用 url “jdbc:h2:tcp://localhost:9092/mem:dataSource”从松鼠 SQL 客户端连接时,它显示连接失败。
    【解决方案2】:

    使用 spring boot,您可以通过 application.properties 文件中的几个配置来做到这一点。

    spring.h2.console.enabled=true
    spring.h2.console.path=/console/
    

    然后您可以在http://localhost:8080/console/ 中访问h2 web 控制台。默认登录配置应该可以工作,除非您更改它们。

    见弹簧靴documentation

    【讨论】:

    • 选择“通用 H2(嵌入式)”并点击“连接”。
    • 我不明白为什么我不能让它工作。我的 pom.xml 中有:&lt;dependency&gt; &lt;groupId&gt;com.h2database&lt;/groupId&gt; &lt;artifactId&gt;h2&lt;/artifactId&gt; &lt;version&gt;${h2.db.version}&lt;/version&gt; &lt;scope&gt;test&lt;/scope&gt; &lt;/dependency&gt; 和我正在使用的application-test.properties 中的上述属性。我的 e2e 测试适用于 h2 db,但无法访问 localhost:8080 返回站点。我错过了什么?
    • Donc 忘记根据你的 portcontextPath 进行调整:Tomcat started on port(s): 8082 (http) with context path '/foo' 意味着你必须继续 http://localhost:8082/foo/console/
    【解决方案3】:

    数据库 URL jdbc:h2:mem:dataSource 表示您正在使用内存数据库。现在,如果您启动第二个 Java 进程并连接到该数据库,您最终将拥有两个内存数据库(每个进程一个)。

    如果你想连接到现有的数据库,你有多种选择:

    • 从同一进程中连接到数据库。不要启动第二个进程。

    • 使用具有硬编码绝对路径的持久化数据库,例如:`jdbc:h2:/data/db/dataSource'。

    • 更复杂/不推荐:如果启动第二个进程,理论上可以使用服务器模式连接到内存数据库。但这意味着您需要启动运行测试的服务器。

    【讨论】:

      【解决方案4】:

      使用 Spring Boot 时,您可以按如下方式注册 H2 Console Servlet:

      @Bean
      public ServletRegistrationBean h2servletRegistration() {
          ServletRegistrationBean registration = new ServletRegistrationBean(new WebServlet());
          registration.addUrlMappings("/console/*");
          registration.addInitParameter("webAllowOthers", "true");
          return registration;
      }
      

      如果您希望控制台远程可用,重要的行是addInitParameter"webAllowOthers" 设置为"true"

      【讨论】:

      • 我收到语法错误“WebServlet is abstract cannot be instatiated”
      • @P-RAD 你导入了错误的 WebServlet 你必须import org.h2.server.web.WebServlet;
      • 可以在此处找到有关此方法的更多详细信息:springframework.guru/…
      【解决方案5】:

      当您使用带有 xml jdbc 配置的 embeddeb 时,数据库的默认名称是“testdb”

      尝试在您的 url 连接中使用:

      jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1
      

      【讨论】:

        【解决方案6】:

        对于那些想要 Java Config 设置的人来说,在实现 ServletContextInitializer 和链接控制台服务器时初始化 TCP 服务器也相当容易......

        @Configuration
        public class WebConfig implements ServletContextInitializer{
        ...
        
        @Override
        public void onStartup( ServletContext servletContext )
        //do stuff onStartUp...
        initH2TCPServer( servletContext );
        ....    
        
        @Bean(initMethod="start", destroyMethod="stop")
        public Server initH2TCPServer(ServletContext servletContext) {
            log.debug( "Initializing H2 TCP Server" );
            try {
                server = Server.createTcpServer( "-tcp", "-tcpAllowOthers", "-tcpPort", "9092" );
            } catch( SQLException e ) {
                e.printStackTrace();
            } finally {
                //Always return the H2Console...
                initH2Console( servletContext );
            }
            return server;
        }
        
        public void initH2Console( ServletContext servletContext ) {
            log.debug( "Initializing H2 console" );
            ServletRegistration.Dynamic h2ConsoleServlet = servletContext.addServlet(
            "H2Console", new org.h2.server.web.WebServlet() );
            h2ConsoleServlet.addMapping( "/console/*" );
         );
        }
        

        【讨论】:

        • 很高兴为您提供帮助:)
        【解决方案7】:

        我遇到了类似的问题。但修复真的非常小。详情请参考页面:https://springframework.guru/using-the-h2-database-console-in-spring-boot-with-spring-security/

        就我而言,我已将 H2 依赖范围添加为“运行时”。我删除了它,它解决了我的问题。我无法在 H2 控制台中看到表格。

        我的 pom 中以前的依赖项是:

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        

        以及解决了我的问题的新依赖项:

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>
        

        【讨论】:

          猜你喜欢
          • 2012-10-22
          • 2018-06-13
          • 1970-01-01
          • 2023-03-24
          • 2017-02-10
          • 2019-12-07
          • 1970-01-01
          • 2017-03-05
          • 2013-02-03
          相关资源
          最近更新 更多