Since writing this, I’ve realised JavaDB is pretty crap. Do yourself a favour and try the decent PostgreSQL installer I found.
Glassfish v3 ships with JavaDB (aka Apache Derby aka Cloudscape).
I’ll be using this for trying out Rails and JRuby, but it’s also handy
for things like authentication via JDBC Realms.
A JavaDB database is essentially a directory that only one process can access at a time (a bit like sqlite). This can be Glassfish itself (an embedded database) or a standalone database process (that serves SQL clients over TCP/IP).
Both have pros and cons. I’ll take you through creating both.
option 1: standalone database server
You sure you want JavaDB? There are better options (sorry, can’t help it).
The main benefit to running a network server process is that it’s the only way for multiple clients to access the database simultaneously
(it’s also the only option that makes sense if you were clustering Glassfish).
If you need to create a schema before you deploy a webapp (with NetBeans or ‘rake migrate’)
you’ll have to stop Glassfish first unless you go down this route.
hypnotoad:databases $ asadmin start-database --dbhost 127.0.0.1
Database started in Network Server mode on host 127.0.0.1 and port 1527.
Could not connect to Derby Network Server on host 127.0.0.1 port 1527.
Starting database in the background.
Log redirected to /Users/dick/Applications/glassfishv3-tp2/glassfish/databases/derby.log.
Command start-database executed successfully.
( –dbhost defaults to ‘0.0.0.0’ but this causes problems if you change IP . Stick to 127.0.0.1).
Next, create the connection pool (and associated database - see later).
It’s simplest to do this on the command line (partly due to bug 4889 ):
hypnotoad:databases $ asadmin create-jdbc-connection-pool \
--datasourceclassname=org.apache.derby.jdbc.ClientConnectionPoolDataSource\
--isconnectvalidatereq=true --validationmethod=meta-data \
--property user=GFv3:password=GFv3:databaseName=railsdb:\
connectionAttributes=\;create\\=true \
railspool
Command create-jdbc-connection-pool executed successfully.
- The ’;create=true’ option tells JavaDB to create the ‘railsdb’ database on demand
- host:port defaults to localhost:1527
- username and password can be anything, but are required
We now ‘ping’ the pool. This checks our network connection is good, and has the side-effect
of creating the ‘railsdb’ database:
hypnotoad:databases $ asadmin ping-connection-pool railspool
Command ping-connection-pool executed successfully.
hypnotoad:databases $ ls
derby.log railsdb
option 2. embed Derby in Glassfish
This is my preferred option for several reasons:
- it saves having to run 2 JVMs
- in development, I don’t mind stopping Glassfish
- for production, I want webapps to create their own schema anyway
- connection validation and authentication is no longer an issue
- sorry to bang on about it, but if you want a standalone database there are much better options
One side effect is that Glassfish is going to use more memory (especially if you have several connection pools configured). You might want to tweak your JVM .
There’s no need to ‘start-database’ in this case – just go ahead and make the pool:
hypnotoad:glassfishv3-tp2 $ asadmin create-jdbc-connection-pool \
--datasourceclassname org.apache.derby.jdbc.EmbeddedDataSource \
--property databaseName=\$\{com.sun.aas.instanceRoot\}/databases/railsdb:\
connectionAttributes=\;create\\=true \
railspool
Command create-jdbc-connection-pool executed successfully.
- the different DataSource class is what makes it embedded
- provide a full path in the databaseName attribute to avoid current working directory hell
- since Glassfish is the database server, we can skip username,password and connection validation options
If we ping the pool, we can see Glassfish creates derby.log and the database dir
hypnotoad:glassfishv3-tp2 $ asadmin ping-connection-pool railspool
Command ping-connection-pool executed successfully.
hypnotoad:glassfishv3-tp2 $ tail derby.log
2008-05-15 11:12:48.770 GMT:
Booting Derby version The Apache Software Foundation – Apache Derby – 10.2.2.1 – (538595): instance c013800d-0119-ec48-424c-000001a39158
on database directory /Users/dick/Applications/glassfishv3-tp2/glassfish/domains/domain1/databases/railsdb
Database Class Loader started – derby.database.classpath=’‘
hypnotoad:glassfishv3-tp2 $
Butler Lampsons mamma didn’t raise no fools
Whichever option you choose, the command to give the pool a JNDI name is the same:
hypnotoad:glassfishv3-tp2 $ asadmin create-jdbc-resource --connectionpoolid=railspool jdbc/railspool
Command create-jdbc-resource executed successfully.
And we’re done. Now simply write a webapp to use the damn thing.