New guy here stumblin' and bumblin' through trying to create an H2 web
app that will run on glassfish and provide a user interface built on
JavaServer Faces. I'm kinda new to the whole enterprise enviroment
and I'm using this exercise as a learning experience. I have a couple
of questions if you all have the time to help educate a newbie.
1) The documentation says that I should use DbStart as a reference for
how to set up a listener to kick off the database. DbStart uses the
Server class to start servers, but can't I just create a connection
for any local data and call the TcpServer class directly?
2) The main method of the Server class would open a console window
would it not? I'm trying to avoid that if possible.
Also, on another note; is the information on registering H2 in the
glassfish app server still valid? I'm having trouble locating the
oracle toplink essentials stuff. I'm guessing that this doesn't
matter for embedding H2 in a .war file, but like I said, I'm trying to
learn a little about the java enterprise platform.
Thanks,
Mark
I use H2 in EE development, but not as part of my Glassfish instance.
I start H2 as a seperate database service (daemon) and then connect to
it via TCP/IP. It does make it a tad slower due to TCP/IP overhead,
but you really have to go way out to justify not doing this. In my
scenarion, I like having the database run merrily on it's own, with
the app server (Glassfish) now free to crash and burn as it likes.
This makes for less worries during installation and even less worries
during production. Now I have a database that my app server AND other
applications can talk to. Not only do desktop applications pull
reports from this database, but I also have a seperate application
that connects to the database from time to time to gather statistics.
Best regards
Ewald
<context-param>
<description>Enable org.h2.driver server at port 9092, but only for
localhost</description>
<param-name>db.tcpServer</param-name>
<param-value>-tcp</param-value>
</context-param>
<listener>
<listener-class>org.h2.server.web.DbStarter</listener-class>
</listener>
Apart from this, you only need to make the h2....jar available to the
WAR file.
Regards
Artejera
Thanks for your replies. You know, I might have bitten off more than
I can chew, but none the less, I'm going to keep trying. So far I
have databases all over my computer, but none where I want them to
be. I can't seem to get the hang of how I might structure the
creation url in order to get the h2 DBRMS to create files in the
folder of the web application that I have provided.
I'm using netbeans for starters and it's project view is very
different from the actual files view that is produced in the .war file
when compiled. When compiled I do have the h2*.jar file in the
WEB_INF/lib folder. I also have a folder in the application root like
this: App_Root/data/ that I want to be able to create my database
files in as the application is deployed to glassfish. I'm suspicious
that the h2 connection that I've provided to the IDE is creating my
discomfort and that I will have to programatically create a connection
from within a servlet that uses the file structure of my web app. I
think it all comes down to mapping the servlet properly in the
web.xml, but I'm not sure what takes over from there. I don't know if
glassfish is supposed to be handling my connections or if I handle
them internally in my web app. I'm still trying to figure that out.
Like I said earlier, I have new /data/ folders popping up all over my
computer, depending upon my latest big idea....and I have no idea how
to get them to go where I want them to go.
So I determine that I've obviously underestimated the complexity of
all this and I've begun trying to decipher and understand h2's jdbcx
folder and how I might structure the jndi name and make available
container managed connection pools and datasources with an h2 embedded
app, but judging by the looks of it, I'm sure I'll be stuck there for
a couple of weeks[months?] before I figure it out.
I'm not trying to waste anybody's time and I really appreciate any
pointers and examples that anyone might be willing to contribute.
Thanks,
Mark
What I do is to pass a global property to the webapp telling it where
the H2 databases are, and manage it outside the webapp directories.
Regards
Aritemo
> can't I just create a connection for any local data and call the TcpServer class directly?
Yes you can. See also
http://www.h2database.com/html/features.html#connection_modes
> The main method of the Server class would open a console window would it not?
It depends on the parameters you use. See
http://www.h2database.com/javadoc/org/h2/tools/Server.html#main_String...
> So far I have databases all over my computer
Try using an absolute path in the database URL, for example
jdbc:h2:/data/h2/test - see also
http://www.h2database.com/html/features.html#database_url and
http://www.h2database.com/html/cheatSheet.html
> trying to decipher and understand h2's jdbcx folder
> and how I might structure the jndi name and make available
> container managed connection pools and datasources with an h2 embedded app
All that may not be necessary. Personally, I avoid using JNDI, data
sources, and so on. It's complicated and doesn't help much. That's
just my personal view :-) There are some advantages (connection pools
may be a bit faster), but it's complex.
Regards,
Thomas
Thanks for your reply Thomas. I really appreciate the opportunity to
receive answers here on your forum from such knowledgeable java
programmers and I'm trying not to abuse it. Most forums don't seem to
have any patience with newbies.
I'm convinced that the problem is not h2, but rather my lack of
understanding about how my IDE and glassfish function, both separately
and in tandem. I'm using netbeans and trying to access a very simple
test database within the directory structure of my web app using an
older embedded version of h2, version 1.1.118 I think. It's one that
I had downloaded a while back. I've downloaded the new one now though
and I'm going to start messing with it. [Beware]
It's really probably more of a netbeans problem. I am having trouble
getting my application to use the version of the h2 library that I
have placed in my WEB_INF/lib folder and it insists on using the one
residing in my local file system, hence the little test databases all
over my computer. For the purposes of my web app I have not yet
figured out the secret handshake to get my code to run in the context
of my web app's directory structure, nor have I been able to get
netbeans to successfully deploy the app directly to glassfish from the
IDE as per the directions in netbeans help files. I can only assume
that I'm missing something somewhere and in time I will find it.
Though I haven't done it yet [too busy reading pdf's] I am going to
begin by manually deploying my app to glassfish to see if it works
there...which is the reason I'm reading up on datasources, resources
and connection pools. It appears that glassfish wants these things in
order to properly run a database.
I could always just use the the built in JavaDB [AKA Derby] because
it's already preconfigured in glassfish. But then, how much fun would
that be? ;-)
Mark
For embeded usage In Services tab -> Drivers node , you can add H2 jdbc
driver ;
and then create a new embedded connection with this driver.
Then you can use all database and persistence features of netbeans over
your embeded H2 database.
Remember that this connection open your H2 database. embeded in Netbeans !
So if you run you webapp from the ide or any other place , you must to
close this connection before (only one embedded connection at the same
time).
> It's really probably more of a netbeans problem. I am having trouble
> getting my application to use the version of the h2 library that I
> have placed in my WEB_INF/lib folder and it insists on using the one
> residing in my local file system, hence the little test databases all
> over my computer.
Define a new library for H2 driver in netbeans: Tools -> Libraries
then add this library to your project.
> For the purposes of my web app I have not yet
> figured out the secret handshake to get my code to run in the context
> of my web app's directory structure, nor have I been able to get
> netbeans to successfully deploy the app directly to glassfish from the
> IDE as per the directions in netbeans help files. I can only assume
> that I'm missing something somewhere and in time I will find it.
>
Don't create a WEB-INF/lib dir for your self this confuse netbeans
webapp deploy.
Or deploy your self outside the ide , but don't mix both methods.
To use JNDI Datasources in GlassFish you can need to open admin console:
1. From Common Task menu expand Resources menu by clicking on Resources
menu.
2. Expand JDBC under resources.
3. Click on ‘Connection Pools’ under JDBC menu. The Connection Pools
page will be displayed.
4. Click on New.
5. Enter a name for your JDBC Connection pool. Select java.sql.Driver as
your ‘Resource Type’ and type (not select) "H2 Database" as your
‘Database vendor’.
6. Next button , and then type "org.h2.Driver" in controller field
(leave other values as is)
7. Fill connection parameers (URL, User, Passw) with your own values
(url for embed is like ; jdbc:h2:~/grassfish-db/mydb )
9. Finish button
10. Then you can create your JNDI name to reference h2 connection pool
in "JDBC Resources" menu.
regards
Dario.
I just can't seem to get it to deploy to glassfish from the IDE. I
guess I'm just going to keep poking around in stuff until I figure it
out. I must have somehow misconfigured something or I haven't
configured something that I should have. I didn't install h2 using a
plugin, I just followed the directions provided in the help files and
everything "appeared" to work as expected except for being unable to
run it in glassfish. I can start and stop the server and even open
the Admin interface and poke around.
BTW, Dario, what happened to the visual database builder that was in
Netbeans once upon a time. I thought you used to be able to open up a
tab and build SQL files using a graphical table builder that looked
similar to UML diagrams, but I don't see anything like that in version
6.8. Maybe I'm mistaken.
Anyway, I'm going to hit the pdf's and do some more reading and try to
make a connection pool in glassfish for h2 and see if that helps my
troubles...or just makes them worse. Thanks for the directions, I'm
going to go give it a try.
Thanks,
Mark