In a previous post I have explained how to setup Oracle XE and GlassFish. This time we will install and configure GlassFish 3.1 and MySQL 5.1 on an Ubuntu Server 11.10.
For this example I have used VirtualBox 4.1 as virtualization tool. I can recommend you VirtualBox when you want to try out some examples and you do not want to screw up your setup.
GlassFish 3.1 Installation
First we have to install Java. Ubuntu does not longer have the sun-jdk in their repository. (see https://lists.ubuntu.com/archives/ubuntu-security-announce/2011-December/001528.html)
So for this example we are going to use OpenJDK 7 which is available in Ubuntu and is also the Java 7 reference implementation.
sudo apt-get install openjdk-7-jdk
After a new installation of the Ubuntu server unzip is not yet installed, so you have to apt-get it. Later we need unzip to unpack some archives.
sudo apt-get install unzip
There ist no Ubuntu package for GlassFish, but the installation is quite simple. I recommand you the to download the GlassFish Server Open Source Edition 3.1.1 (Zip archive) and unzip it to /opt/glassfish.
wget http://download.java.net/glassfish/3.1.1/release/glassfish-3.1.1.zip sudo unzip glassfish-3.1.1.zip -d /opt
For an automatically start up of GlassFish we need to create a traditional sysvinit script.
sudo vim /etc/init.d/glassfish
The content of the start script should look as follows.
#!/bin/bash
GLASSFISH_HOME=/opt/glassfish3
case "$1" in
start)
${GLASSFISH_HOME}/bin/asadmin start-domain domain1
;;
stop)
${GLASSFISH_HOME}/bin/asadmin stop-domain domain1
;;
restart)
${GLASSFISH_HOME}/bin/asadmin stop-domain domain1
${GLASSFISH_HOME}/bin/asadmin start-domain domain1
;;
*)
echo "usage: $0 {start|stop|restart}"
;;
esac
exit 0
The last step is to make the script executable and to install our script as System-V style script with update-rc.d.
sudo chmod 755 /etc/init.d/glassfish sudo update-rc.d /etc/init.d/glassfish defaults
Now we can start glassfish with our start script.
sudo /etc/init.d/glassfish start
After GlassFish is started you can access the admin console on http://localhost:4848.
MySQL 5.1 Installation
We use the MySQL version which is already packged with the Ubuntu server.
sudo apt-get install mysql-server-5.1
Out of the box MySQL allowes only connections from localhost. To change that we have to edit /etc/mysql/my.cnf. Open the MySQL configuration file and comment out the following line.
#bind-address = 127.0.0.1
After the restart of MySQL we cann now connect from our network to the MySQL server.
sudo /etc/init.d/mysql restart
It’s time to create a MySQL database and user which we can use from glassfish as DataSource. To create a MySQL database and user you have to login to the mysql server as root.
mysql -u root -p
The following SQL commands create the database glassfish_db and the mysql user glassfish which is allowed to access the database form the external network.
create database glassfish_db; grant usage on *.* to 'glassfish'@'%' identified by 'glassfish'; grant all privileges on glassfish_db.* to 'glassfish'@'%';
You should now be able to connect as user glassfish to the MySQL database glassfish_db. When you don’t want to work with the console I would suggest you the MySQL Workbench.
mysql -u glassfish -p
GlassFish DataSource configuartion for MySql
To use MySQL with GlassFish we need the MySQL Connector/J which is the official JDBC driver for MySQL. Copy the JDBC driver to the GlassFish lib directory.
unzip mysql-connector-java-5.1.18.zip sudo cp mysql-connector-java-5.1.18/mysql-connector-java-5.1.18-bin.jar \ /opt/glassfish3/glassfish/lib/
The next thing we have to do is to restart GlassFish and configure our DataSource.
sudo /etc/init.d/glassfish restart
Login to the GlassFish admin console and create two JDBC Connection Pools one of them as XA DataSource.
- MySqlDataSource
- MySqlDataSourceXA
The two DataSources should have the following settings:
DataSource
- Pool Name: MySqlDataSource
- Resource Type: javax.sql.DataSource
- Database Driver Vendor: MySQL
- Datasource Classname: com.mysql.jdbc.jdbc2.optional.MysqlDataSource
XA DataSource
- Pool Name: MySqlDataSourceXA
- Resource Type: javax.sql.XADataSource
- Database Driver Vendor: MySQL
- Datasource Classname: com.mysql.jdbc.jdbc2.optional.MysqlXADataSource
Both DataSources need at least the following additional properties. Which are required to connect to the MySQL Database.
Additional Properties:
- ServerName: localhost
- Port: 3306
- User: glassfish
- Password: glassfish
- DatabaseName: glassfish_db
In the GlassFish admin console you can check if both DataSources are properly configured. When you click on the ping button you should receive a ping succeeded.
JDBC Resources:
For both DataSources we create now a JDBC resource with a JNDI name.
- JNDI Name: jdbc/MySqlDataSource
- Pool Name: MySqlDataSource
- JNDI Name: jdbc/MySqlDataSourceXA
- Pool Name: MySqlDataSourceXA
Finally
After that we can now use the two DataSources in our Java EE application.

