如何显示本地h2数据库(Web控制台)的内容?
最近我加入了一个新的团队,这里的人们使用h2进行存根服务。
我想知道我是否可以使用Web界面显示此数据库的内容。 在工作中,可以访问localhost:5080
我有一个项目,我使用h2数据库,但当我点击localhost:5080
时,我看不到h2 web控制台
我也试过localhost:8082
– 它也不起作用。
我的项目配置(成功运行):
classpath:hibernate-test.cfg.xml false UTF-8 true create-drop
我没有想法如何访问h2 web控制台。 请帮忙。
PS
我只在.m2
文件夹中看到了h2的提及
PS2
我注意到http://localhost:8082/
可以使用web控制台,如果在配置中替换url:
但是如果我已经启动了h2(在.m2
文件夹中找到h2
jar文件并点击双击)它就可以了
如果我在启动应用程序时未启动h2 – 我看到以下错误:
java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:94) ... Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dbInitializer': Invocation of init method failed; nested exception is org.hibernate.exception.GenericJDBCException: Could not open connection at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:136) ... Caused by: org.hibernate.exception.GenericJDBCException: Could not open connection at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54) ... Caused by: org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Соединение разорвано: "java.net.ConnectException: Connection refused: connect: localhost" Connection is broken: "java.net.ConnectException: Connection refused: connect: localhost" [90067-182]) at org.apache.commons.dbcp.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:1549) ... Caused by: org.h2.jdbc.JdbcSQLException: Соединение разорвано: "java.net.ConnectException: Connection refused: connect: localhost" Connection is broken: "java.net.ConnectException: Connection refused: connect: localhost" [90067-182] at org.h2.message.DbException.getJdbcSQLException(DbException.java:345) ... Caused by: java.net.ConnectException: Connection refused: connect at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ...
如果在我启动应用程序时没有启动,我想实现h2启动。
PS3
我试过写下面的代码:
Server server = null; try { server = Server.createTcpServer("-tcpAllowOthers").start(); Class.forName("org.h2.Driver"); Connection conn = DriverManager.getConnection("jdbc:h2:tcp://localhost/~/test;MODE=PostgreSQL", "sa", ""); } catch (Exception e) { LOG.error("Error while initialize", e); }
我执行它并在我尝试在浏览器中键入localhost:9092
之后。
此时下载文件。 在inside文件中有以下内容:
Version mismatch, driver version is “0” but server version is “15”
我的h2版本1.4.182
PS4
此代码有效:
public class H2Starter extends ContextLoaderListener { private static final Logger LOG = LoggerFactory.getLogger(H2Starter.class); @Override public void contextInitialized(ServletContextEvent event) { startH2(); super.contextInitialized(event); } private static void startH2() { try { Server.createTcpServer("-tcpAllowOthers").start(); Class.forName("org.h2.Driver"); DriverManager.getConnection("jdbc:h2:tcp://localhost/~/test;MODE=PostgreSQL;AUTO_SERVER=TRUE", "sa", ""); Server.createWebServer().start(); } catch (Exception e) { LOG.error("cannot start H2 [{}]", e); } } public static void main(String[] args) { startH2(); } }
但是我需要在混凝土弹簧轮廓激活时调用它(现在它始终有效)
我们将问题分成两部分。
根据您指定H2连接的方式,您将获得不同的操作模式。
模式包括:嵌入式,内存中,服务器。
jdbc:h2:~/test
在嵌入模式下为您提供H2实例。 嵌入模式有一个限制,只能通过相同的类加载器和相同的JVM访问( certificate )
jdbc:h2:mem:test
让你获得一个内存中的H2实例。 外部世界也无法获得这一点。
jdbc:h2:tcp://localhost/test
将启动H2服务器,它可以从JVM 服务器模式外部访问 ,但有一个限制 – 服务器需要在建立连接之前启动。
最后一个限制是导致您的Connection refused: connect: localhost"
exception。
总结一下:
- 在启动应用程序之前启动H2服务器
- 使用
jdbc:h2:tcp://localhost/test
作为连接字符串 - ….
- 快乐的编码:)
更新
刚刚注意到你想在启动应用程序的过程中启动服务器。
您可以通过多种方式执行此操作,具体取决于您如何启动应用程序:
- 如果你正在使用maven / gradle,你可以更容易地添加一些配置文件/任务,以便在应用程序实际启动之前执行它。
- 如果你必须在java中设置所有内容,我建议你看一下这个问题
更新2
如果仅为了开发/调试目的而需要连接到本地数据库,我将使用maven配置文件设置所有内容。 这个问题的答案将解决这个问题 。
如果您需要在生产中访问H2数据库(我很难想象有任何用例),最好在spring这样做。 主要是因为应用程序容器/环境设置可能在生产中有所不同(与开发环境相比)。
要解决有关是否在Spring上下文之外启动服务器的问题 – 这一切都取决于要求。 您应该注意的一件事是服务器应该在数据源启动之前启动(否则不会加载spring上下文)
更新3
不幸的是,我无法为您提供有效的解决方案,但根据JavaDocs,TCP服务器和Web服务器之间存在差异。 仔细看看H2 Server类的JavaDoc 。
我想你应该使用Server.createWebServer()
方法来创建服务器(TCP服务器和Web服务器之间的区别就是那个
你可以使用的另一个很棒的类org.h2.tools.Console
( 这里是JavaDoc )只需运行Console的主要方法,我想这应该可以解决所有问题。
您应该能够使用内存或基于文件的变体,然后在您的应用程序中单独启动H2 TCP服务器,例如使用Spring bean(请注意半伪代码和示例端口):
@Component class Bootstrap { @PostConstruct public void startH2TcpServer() { Server.createTcpServer("-tcpPort", "9123", "-tcpDaemon").start(); } }
请参见http://www.h2database.com/html/tutorial.html#using_server
如何在配置中更改jdbc url以包含
AUTO_SERVER=TRUE
自动启动h2。
请参阅自动混合模式
多个进程可以访问同一个数据库,而无需手动启动服务器。 为此,请将AUTO_SERVER = TRUE附加到数据库URL。 您可以使用相同的数据库URL,而不管数据库是否已打开。 此function不适用于内存数据库。
对与此数据库的所有连接使用相同的URL。 在内部,使用此模式时,第一次连接到数据库是在嵌入模式下进行的,另外一个服务器在内部启动(作为守护程序线程)。 如果数据库已在另一个进程中打开,则会自动使用服务器模式。