Non Automatic Mixed mode

90 views
Skip to first unread message

Luigi R. Viggiano

unread,
May 7, 2012, 6:13:19 PM5/7/12
to H2 Database
Hello.

I discovered h2 today, and I'm very excited about it.

I am trying to embed h2 into a web app, so following the documentation
(pdf, page 25) I did this in my web.xml

<web-app>
...
<context-param>
<param-name>db.url</param-name>
<param-value>jdbc:h2:~/.myapp/data</param-value>
</context-param>
<context-param>
<param-name>db.user</param-name>
<param-value>sa</param-value>
</context-param>
<context-param>
<param-name>db.password</param-name>
<param-value>sa</param-value>
</context-param>
<context-param>
<param-name>db.tcpServer</param-name>
<param-value>-tcpAllowOthers -trace</param-value>
</context-param>

<listener>
<listener-class>org.h2.server.web.DbStarter</listener-class>
</listener>
...
</web-app>

Now, I'm trying to connect using an external client, using the url
jdbc:h2:tcp://localhost/~/.myapp/data but I am getting the error
"Database may be already in use: "Locked by another process". Possible
solutions: close all other connection(s); use the server mode
[90020-166] 90020/90020"

I understood that h2 can work in "Automatic Mixed Mode" which I
managed to make it work.

If I specify the url jdbc:h2:tcp://localhost/~/.myapp/data instead of
jdbc:h2:~/.myapp/data in the web.xml, then it works. But the
documentation makes an example which consistent with mine (using a
local url in the web.xml), so I thought it should work. But it
doesn't: I need to connect always using tcp even from the web app?

Is it possible to have the web application access the database in
embedded mode while the external applications accessing it through
server mode, without using the *Automatic* Mixed Mode?

What am I doing wrong?

Thanks
Luigi

Ryan How

unread,
May 8, 2012, 2:16:19 AM5/8/12
to h2-da...@googlegroups.com
Yes it most definitely is. You need to access the database from the web
application using the embedded mode. And then from the web application,
start up a tcpserver. Then you can connect to the database using that
TCP server. I'm not familiar with how you do it from the web.xml, I just
usually do it in the application startup code. I'm sure someone will
point you in the right direction soon!.

Ryan How

unread,
May 8, 2012, 2:20:57 AM5/8/12
to h2-da...@googlegroups.com
On second look, you don't by any chance have the database open in
another process, eg the web console?

Luigi R. Viggiano

unread,
May 8, 2012, 2:43:10 AM5/8/12
to H2 Database
Yes Ryan, I'm sure it was not used by any second process (I deleted
the files and restarted many times)

The solution that worked for me, at the end, was to specify the port:

<context-param>
<param-name>db.tcpServer</param-name>
<param-value>-tcpPort 9081</param-value>
</context-param>

With this, and connecting with url jdbc:h2:tcp://localhost:9081/
~/.myapp/data things are now woking as expected. I think there may be
a bug which pops up when the DbStarter is used without the -tcpPort
option.

Thanks,

Luigi

Peter Yuill

unread,
May 8, 2012, 5:51:32 AM5/8/12
to h2-da...@googlegroups.com
Hi Luigi,

> <context-param>
> <param-name>db.url</param-name>
> <param-value>jdbc:h2:~/.myapp/data</param-value>
> </context-param>
...
> Now, I'm trying to connect using an external client, using the url
> jdbc:h2:tcp://localhost/~/.myapp/data but I am getting the error
> "Database may be already in use: "Locked by another process". Possible
> solutions: close all other connection(s); use the server mode
> [90020-166] 90020/90020"
>
> I understood that h2 can work in "Automatic Mixed Mode" which I
> managed to make it work.
>
> If I specify the url jdbc:h2:tcp://localhost/~/.myapp/data instead of
> jdbc:h2:~/.myapp/data in the web.xml, then it works. But the
> documentation makes an example which consistent with mine (using a
> local url in the web.xml), so I thought it should work. But it
> doesn't: I need to connect always using tcp even from the web app?
>
> Is it possible to have the web application access the database in
> embedded mode while the external applications accessing it through
> server mode, without using the *Automatic* Mixed Mode?
>
> What am I doing wrong?

I think you misunderstood automatic mixed mode. The critical part is the
extra AUTO_SERVER parameter on the end of the URL eg

jdbc:h2:~/.myapp/data;AUTO_SERVER=TRUE

Both the web app and the external client use exactly the same URL (the
automatic part). The first connection (should be the web app) connects
in embedded mode while the second connection automatically connects via
tcp. It is explained here:

http://www.h2database.com/html/features.html#auto_mixed_mode

Regards,
Peter

Luigi R. Viggiano

unread,
May 8, 2012, 10:04:51 AM5/8/12
to H2 Database
Thanks Everyone for the answers.

Peter, I know about the AUTO_SERVER feature, it's just a different
thing from what I want to have.
AUTO_SERVER means that the first process accessing the database does
it as embedded while keeping a tpc listener for subsequent
connections.

Now imagine this scenario.

Process1: access the database and becomes the holder of the lock.
Process2: access the database and it finds it loked, so connects to
Process1 with tcp connection.
Process1: ends its task.
Process2: has a broken connection now.

What I want is to make sure that the Process2, in my case a web
application that will be always up, is always the one who holds the
lock.
And this is how it is specified by this page: "Using a Servlet
Listener to Start and Stop a Database" http://www.h2database.com/html/tutorial.html#web_applications

And I think there may be a but in the configuration exposed in the
documentation.

I just discovered H2, and I think it's the best. I am migrating my app
from a "nosql" store to H2. H2 it's just awesome; I'm looking at the
source code, and it is quite incredible, sort of alien technology! :-)

Thanks for the help.

Luigi.

Igal

unread,
May 8, 2012, 11:02:33 AM5/8/12
to h2-da...@googlegroups.com
I'm an H2 newbie -- but given your scenario -- why not use it only in Server Mode?

Luigi R. Viggiano

unread,
May 8, 2012, 4:32:58 PM5/8/12
to h2-da...@googlegroups.com
> I'm an H2 newbie -- but given your scenario -- why not use it only in Server
> Mode?

Good question; in effect, using the server mode it was working.

The reason why I preferred that the web application access the files
in embedded mode is just because it's *the correct way*.
Wouldn't you call me weirdo if I phone call the colleague who is
sitting next to me? :-)

Also, performances are much better accessing a local database rather
than doing it using the network (even though it's localhost). But I'm
not concerned about performance, I just don't like to waste resources.

Cheers,
--Luigi

Luigi R. Viggiano

Mobile:
   Italy: +39 366 521 5000
   Germany: +49 176 3200 1283
Web/Blog: http://en.newinstance.it
Skype: luigi.viggiano
Twitter: https://twitter.com/lviggiano
Professional Profile: http://www.linkedin.com/in/viggiano
> --
> You received this message because you are subscribed to the Google Groups
> "H2 Database" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/h2-database/-/tBUFhQiBVLYJ.
>
> To post to this group, send email to h2-da...@googlegroups.com.
> To unsubscribe from this group, send email to
> h2-database...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/h2-database?hl=en.
Reply all
Reply to author
Forward
0 new messages