Server in Mixed Mode + embedding H2 Console, single file DB in webapp

638 views
Skip to first unread message

mhaller

unread,
Oct 29, 2008, 7:37:25 AM10/29/08
to H2 Database
Hi all,

my scenario: web application using H2 for demo purposes.

We'd like to embed the H2 Console into the existing web application,
so it's reachable on the same hostname/port, instead of listening on
its own HTTP port. Is that possible?

Also, i'd like to embed the web console in our web application, so we
can frame it with the styles of our web design.

Now, my questions are:
- How to embed the H2 Console (e.g. which Servlet to use, which file
mappings), how to pre-configure the JDBC URI (only allow connections
to the single database)
- How to start up H2 Server within a Spring Application Context, or
should we use DbStarter in web.xml?
- How to configure the pooled DataSource (Spring managed) to access
the H2 Server, which was started by
<listener>
<listener-class>org.h2.server.web.DbStarter</listener-class>
</listener>

We need a filebased database - one per web application installation.
We use embedded Jetty.

any ideas?
regards
Mike

Thomas Mueller

unread,
Oct 31, 2008, 12:24:29 PM10/31/08
to h2-da...@googlegroups.com
Hi,

> my scenario: web application using H2 for demo purposes.

I'm not sure, did you already read the documentation at
http://www.h2database.com/html/tutorial.html#web_applications

> We'd like to embed the H2 Console into the existing web application,
> so it's reachable on the same hostname/port, instead of listening on
> its own HTTP port. Is that possible?

Yes, using WebServlet. It is not documented at the moment, I will do
that in the next release.

== Using the H2 Console Servlet ==

The H2 Console is a standalone application and includes its own web
server, but it can be used as a servlet as well. To do that, include
the the h2 jar file in your application, and add the following
configuration to your web.xml:

<servlet>
<servlet-name>H2Console</servlet-name>
<servlet-class>org.h2.server.web.WebServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>H2Console</servlet-name>
<url-pattern>/console/*</url-pattern>
</servlet-mapping>

For details, see also src/tools/WEB-INF/web.xml.

To create a web application that creates just the H2 Console, run the
following command:

build warConsole

> Also, i'd like to embed the web console in our web application, so we
> can frame it with the styles of our web design.

You will probably have to overwrite the stylesheet.

> how to pre-configure the JDBC URI (only allow connections to the single database)

Hm, that doesn't work well I'm afraid. I'm not sure how to do that
exactly. You would need to have a look at the code. What you can do is
start the standalone H2 Console using a connection:
http://www.h2database.com/javadoc/org/h2/tools/Server.html#r15
(Console.startWebServer(Connection conn)).

> - How to start up H2 Server within a Spring Application Context, or should we use DbStarter in web.xml?

I'm sure you can start the H2 Server from Spring, but I don't know
Spring well enough...

> - How to configure the pooled DataSource (Spring managed) to access the H2 Server

I'm not sure if you need to access the Server... What about just
opening a JDBC connection using the server protocol
(jdbc:h2:tcp://...)?

Regards,
Thomas

mhaller

unread,
Nov 10, 2008, 8:46:55 AM11/10/08
to H2 Database
Hi Thomas,

> Yes, using WebServlet. It is not documented at the moment, I will do
> that in the next release.

Thanks! Works great.

> > Also, i'd like to embed the web console in our web application, so we
> > can frame it with the styles of our web design.
> You will probably have to overwrite the stylesheet.

Could not embed the H2 Console, as there's JavaScript code which
checks for top window and redirects. So I could not even embed it
using <iframe>. Now, i'm using an external popup window.

> > - How to start up H2 Server within a Spring Application Context, or should we use DbStarter in web.xml?
> I'm sure you can start the H2 Server from Spring, but I don't know
> Spring well enough...

Works fine with the following:

<bean id="org.h2.tools.Server"
class="org.h2.tools.Server"
factory-method="createTcpServer"
init-method="start"
destroy-method="stop">
<constructor-arg value="-tcp,-tcpAllowOthers,true,-tcpPort,
8043" />
</bean>

"destroy-method" is important to prevent Exceptions when hot-
redeployment or restarting server.


> Regards,
> Thomas

regards
Mike

Thomas Mueller

unread,
Nov 14, 2008, 10:54:23 AM11/14/08
to h2-da...@googlegroups.com
Hi,

> Works fine with the following:
>
> <bean id="org.h2.tools.Server"
> class="org.h2.tools.Server"
> factory-method="createTcpServer"
> init-method="start"
> destroy-method="stop">
> <constructor-arg value="-tcp,-tcpAllowOthers,true,-tcpPort,
> 8043" />
> </bean>
>
> "destroy-method" is important to prevent Exceptions when hot-
> redeployment or restarting server.

Thanks a lot! I will add this information to the documentation.

Regards,
Thomas

Reply all
Reply to author
Forward
0 new messages