MySQL provides a good step-by-step guide to install MySQL on GlassFish. However, it is manual so I have taken their information and converted it to set of asadmin
commands to set up MySQL on Glassfish and also set it as the default JPA datasource.
Creating the database
For this post, a database with the following criteria will be created:
- database name =
glassfish
- user name =
gfuser
- password =
password
There are the commands to run from mysql -u root
CREATE DATABASE glassfish DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci; CREATE USER 'gfuser'@'%' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON glassfish.* TO 'gfuser'@'%';
Installing the driver
There are two ways you can install the MySQL Java Connector. The simplest one for those that have full access to the server is to copy the mysql-connector-java-5.x.xx.jar
file to glassfish\domains\domain1\lib\ext
. If the domain is started, then a restart is required for it to load it into the classpath.
The second which is better for a managed installation is to deploy it as an OSGi bundle using and it won’t require a domain restart.
deploy --type osgi mysql-connector-java-5.x.xx.jar
Creating the datasource and JPA default
Finally the following commands are used to create the JDBC pools and resources.
# create the database connection pools # it performs validation by checking the DUAL table # this is for the non-XA data source create-jdbc-connection-pool --restype javax.sql.DataSource --datasourceclassname com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource --validationmethod table --validationtable DUAL --property User=gfuser:Password=password:ServerName=localhost:DatabaseName=glassfish --ping true MySQLPool # this is for an XA data source create-jdbc-connection-pool --restype javax.sql.XADataSource --datasourceclassname com.mysql.jdbc.jdbc2.optional.MysqlXADataSource --validationmethod table --validationtable DUAL --property User=gfuser:Password=password:ServerName=localhost:DatabaseName=glassfish --ping true MySQLXAPool set resources.jdbc-resource.jdbc/__default.pool-name=MySQLXAPool # this creates named JDBC resources create-jdbc-resource --connectionpoolid MySQLPool jdbc/MySQL create-jdbc-resource --connectionpoolid MySQLXAPool jdbc/MySQLXA # this set the JPA default data source set resources.jdbc-resource.jdbc/__default.pool-name=MySQLXAPool