title: How to show content of local h2 database(web console)?
body: <p>Recently I joined a new team and here guys use h2 for stub service.</p>
  
  <p>I was wondering whether I can show the content of this database using web interface. At work it is available by going to <code>localhost:5080</code></p>
  
  <p>I have a project where I use h2 database, but I cannot see the h2 web console when I hit <code>localhost:5080</code></p>
  
  <p>I tried also <code>localhost:8082</code> - it also doesn't work.</p>
  
  <p>My project configuration (works successfully):</p>
  
  <pre><code>     &lt;bean id="wrappedDataSource" class="net.bull.javamelody.SpringDataSourceFactoryBean"&gt;
          &lt;property name="targetName" value="dataSource" /&gt;
       &lt;/bean&gt;
  
       &lt;bean id="wrappedDataSource" class="net.bull.javamelody.SpringDataSourceFactoryBean"&gt;
              &lt;property name="targetName" value="dataSource" /&gt;
          &lt;/bean&gt;
  
          &lt;bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource"&gt;
              &lt;property name="driverClassName" value="org.h2.Driver" /&gt;
              &lt;property name="url" value="jdbc:h2:~/test;MODE=PostgreSQL" /&gt;
              &lt;property name="username" value="sa" /&gt;
              &lt;property name="password" value="" /&gt;
          &lt;/bean&gt;
  
          &lt;bean id="sessionFactory"
                class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
              &lt;property name="dataSource" ref="wrappedDataSource"/&gt;
              &lt;property name="configLocation"&gt;
                  &lt;value&gt;classpath:hibernate-test.cfg.xml&lt;/value&gt;
              &lt;/property&gt;
              &lt;property name="hibernateProperties"&gt;
                  &lt;props&gt;
                      &lt;prop key="hibernate.show_sql"&gt;false&lt;/prop&gt;
                      &lt;prop key="hibernate.connection.charSet"&gt;UTF-8&lt;/prop&gt;
                      &lt;prop key="hibernate.format_sql"&gt;true&lt;/prop&gt;
                      &lt;prop key="hbm2ddl.auto"&gt;create-drop&lt;/prop&gt;
                  &lt;/props&gt;
              &lt;/property&gt;
          &lt;/bean&gt;
  
          &lt;context:property-placeholder location="classpath:jdbc.properties"/&gt;
  </code></pre>
  
  <p>I have not ideas how to access to h2 web console. please help.</p>
  
  <h2>P.S.</h2>
  
  <p>I see mentions of h2 only in <code>.m2</code> folder</p>
  
  <h2>P.S.2</h2>
  
  <p>I noticed that web console available by <code>http://localhost:8082/</code> if replace url in configuration with:</p>
  
  <pre><code>&lt;property name="url" value="jdbc:h2:tcp://localhost/~/test;MODE=PostgreSQL" /&gt;
  </code></pre>
  
  <p><strong>But</strong> it works if I already start h2(in <code>.m2</code> folder find <code>h2</code> jar file and hit double click)</p>
  
  <p>If h2 is not started when I start application - i see following error:</p>
  
  <pre><code>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)
      ...
  </code></pre>
  
  <p>I want to achieve that h2 starts if it not started when I start my application.</p>
  
  <h2>P.S.3</h2>
  
  <p>I have tried to wrote following code:</p>
  
  <pre><code>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);
   }
  </code></pre>
  
  <p>I execute it and after I am trying to type <code>localhost:9092</code> in my browser.</p>
  
  <p>At this moment downloads file. inside file the following content:</p>
  
  <pre><code>Version mismatch, driver version is “0” but server version is “15”
  </code></pre>
  
  <p>my h2 version 1.4.182</p>
  
  <h2>P.S.4</h2>
  
  <p>This code works:</p>
  
  <pre><code>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();
      }
  }
  </code></pre>
  
  <p>but I need to invoke it only when concrete spring profile active(now it works always)</p>