最近我加入了一个新团队,在这里,人们使用h2进行存根服务。
我想知道我是否可以使用网络界面显示此数据库的内容。在工作中,可以转到localhost:5080
我有一个使用h2数据库的项目,但是当我点击localhost:5080
我也试过localhost:8082
- 它也没有用。
我的项目配置(成功运作):
<bean id="wrappedDataSource" class="net.bull.javamelody.SpringDataSourceFactoryBean">
<property name="targetName" value="dataSource" />
</bean>
<bean id="wrappedDataSource" class="net.bull.javamelody.SpringDataSourceFactoryBean">
<property name="targetName" value="dataSource" />
</bean>
<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
<property name="driverClassName" value="org.h2.Driver" />
<property name="url" value="jdbc:h2:~/test;MODE=PostgreSQL" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="wrappedDataSource"/>
<property name="configLocation">
<value>classpath:hibernate-test.cfg.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.connection.charSet">UTF-8</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hbm2ddl.auto">create-drop</prop>
</props>
</property>
</bean>
<context:property-placeholder location="classpath:jdbc.properties"/>
我没有想法如何访问h2 web控制台。请帮忙。
我只在.m2
文件夹
我注意到http://localhost:8082/
提供的Web控制台,如果将配置中的url替换为:
<property name="url" value="jdbc:h2:tcp://localhost/~/test;MODE=PostgreSQL" />
但是如果我已经启动了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启动。
我试过编写以下代码:
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
后执行它。
此时下载文件。在文件里面有以下内容:
Version mismatch, driver version is “0” but server version is “15”
我的h2版本1.4.182
此代码有效:
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();
}
}
但是我需要在混凝土弹簧轮廓激活(现在它始终有效)时调用它
答案 0 :(得分:20)
让我们将问题分成两部分。
根据您指定与H2的连接方式,您将获得不同的操作模式。
模式包括:嵌入式,内存中,服务器。
jdbc:h2:~/test
在embedded mode中为您提供H2实例。
嵌入模式的局限性是只能通过相同的类加载器和相同的JVM(proof)
jdbc:h2:mem:test
为您提供内存中的H2实例。这也是不可以从外部世界访问。
jdbc:h2:tcp://localhost/test
将启动H2服务器,它将从JVM server mode外部可访问,但有一个限制 - 服务器需要在建立连接之前启动。
最后一个限制是导致Connection refused: connect: localhost"
例外。
总结一切:
jdbc:h2:tcp://localhost/test
作为连接字符串<强>更新强>
注意到你想在启动应用程序的过程中启动服务器。
您可以通过多种方式实现这一目标,具体取决于您如何启动应用程序:
更新2
如果仅为了开发/调试目的需要连接到本地数据库,我将使用maven配置文件设置所有内容。来自this question的答案将解决这个问题。
如果您需要在生产中访问H2数据库(我很难想象有任何用例),那么在春季这样做会更好。主要是因为应用程序容器/环境设置可能在生产方面有所不同(与开发环境相比)。
要解决有关是否在Spring上下文之外启动服务器的问题 - 这一切都取决于要求。 您应该注意的一件事是服务器应该在启动数据源之前启动(否则弹出上下文不会加载)
更新3
不幸的是,我无法为您提供有效的解决方案,但根据JavaDocs,TCP服务器和Web服务器之间存在差异。 仔细查看JavaDoc of H2 Server class。
我想您应该使用Server.createWebServer()
方法来创建服务器(TCP服务器和Web服务器之间的区别是
您可以使用的另一个优秀课程org.h2.tools.Console
(JavaDoc here)
只需运行Console的主要方法,我想这应该可以解决所有问题。
答案 1 :(得分:1)
您应该能够使用内存或基于文件的变体,然后在您的应用程序中单独启动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
答案 2 :(得分:1)
如何在配置中更改jdbc url以包含
AUTO_SERVER=TRUE
自动启动h2。
多个进程可以访问同一个数据库而无需访问 手动启动服务器。为此,请追加; AUTO_SERVER = TRUE 数据库URL。您可以使用相同的数据库URL,而不管是否 数据库是否已打开。此功能不起作用 内存数据库。
对此数据库的所有连接使用相同的URL。在内部,当使用这种模式时, 第一次连接到数据库是在嵌入模式下进行的 另外,服务器在内部启动(作为守护程序线程)。如果 数据库已在另一个进程中打开,服务器模式是 自动使用。