Embedded GlassFish with installation-less MySQL
Posted by bhavanishankar on April 5, 2011 at 1:46 AM CDT
Using MySQL database with GlassFish is nothing new. However I just wanted to share a small note on how I used MySQL DB from GlassFish which is embedded in my test.
I actually had 2 scenarios which I listed below:Scenario 1. MySQL server is pre-installed and running in my system

Scenario 2. There is no MySQL server pre-installed in my system

Obvioulsy, in both scenarios there is no preinstalled GlassFish.
In my test, I embed GlassFish and deploy an application using the following piece of code. This code remains the same in both the scenarios.
@TestFor scenario1, in my app I have mysqlTestServlet which accesses MySQL DB like this:
public void test() throws Exception {
GlassFishProperties glassFishProperties = new GlassFishProperties();
glassFishProperties.setPort("http-listener", 8080);
GlassFish glassFish = GlassFishRuntime.bootstrap().
newGlassFish(glassFishProperties);
glassFish.start();
Deployer deployer = glassFish.getDeployer();
String appName = deployer.deploy(new File("target/mysqltest.war"));
// Access the servlet
get("http://localhost:8080/mysqltest/mysqlTestServlet");
}
@DataSourceDefinition(name = "java:app/mysql/MySQLDataSource",}
className = "com.mysql.jdbc.jdbc2.optional.MysqlDataSource",
portNumber = 3306,
serverName = "localhost",
databaseName = "testDB",
user = "root",
password = "abc123",
properties = {"createDatabaseIfNotExist=true"}
)
@WebServlet(name = "mySqlTestServlet", urlPatterns = "/mysqlTestServlet")
public class MySqlTestServlet extends HttpServlet {
@Resource(mappedName = "java:app/mysql/MySQLDataSource")
DataSource myDB;
.....
For scenario2, in my app I have mysqlTestServlet which accesses MySQL DB like this (DataSourceDefinition differs):
@DataSourceDefinition(name = "java:app/mysql/MySQLEmbeddedDataSource",Now let us see how to run these two scenarios.
className = "com.mysql.jdbc.Driver",
url = "jdbc:mysql:mxj://localhost:3336/testDB",
user = "root",
password = "abc123",
properties = {"createDatabaseIfNotExist=true",
"server.basedir=/tmp/testDB", "server.initialize-user=true"}
)
@WebServlet(name = "mySqlTestServlet", urlPatterns = "/mysqlTestServlet")
public class MySqlTestServlet extends HttpServlet {
@Resource(mappedName = "java:app/mysql/MySQLEmbeddedDataSource")
DataSource myEmbeddedDB;
.....
}
For scenario1, all you need is the MySQL driver jar file in addition to glassfish-embedded-[web|all|.jar in your classpath. Make sure MySQL server is running at port 3306. If you are using the maven based project, then your dependencies would look like this:
<dependency>
<groupId>org.glassfish.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.15</version>
</dependency>
For scenario2, you DON'T need MySQL server preinstalled/running, but you need MySQL Connector/MXJ (Java library for deploying/managing MySQL DB). For the lack of any maven repository for Connector/MXJ, please download it manually from here and extract it under /tmp. Once you extract it, you need the extracted JAR files in the classpath. So, all in all you will have glassfish-embedded-[web|all].jar and the connector/MXJ jars in your classpath. If you are using maven based project, your dependencies will look like this (plus the above mentioned dependencies):
<dependency>
<groupId>com.mysql.connector</groupId>
<artifactId>mxj</artifactId>
<version>${project.version}</version>
<scope>system</scope>
<systemPath>/tmp/mysql-connector-mxj-gpl-5-0-11/mysql-connector-mxj-gpl-5-0-11.jar</systemPath>
</dependency>
<dependency>
<groupId>com.mysql.connector</groupId>
<artifactId>mxj-db-files</artifactId>
<version>${project.version}</version>
<scope>system</scope>
<systemPath>/tmp/mysql-connector-mxj-gpl-5-0-11/mysql-connector-mxj-gpl-5-0-11-db-files.jar</systemPath>
</dependency>
That's pretty much it.
Additionally, if you were to use JPA, then you need META-INF/persistence.xml which should look like this:
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"I have checked in the test corresponding to this blog @ v3/tests/embedded/mysql. The test uses JPA also.
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="mysql_testdb" transaction-type="JTA">
<jta-data-source>java:app/mysql/MySQLEmbeddedDataSource</jta-data-source>
<properties>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
<property name="eclipselink.logging.level" value="FINE"/>
</properties>
</persistence-unit>
</persistence>
| Attachment | Size |
|---|---|
| EmbeddedGF_with_MySQL.png | 46.54 KB |
| EmbeddedGF_with_EmbeddedMySQL.png | 51.02 KB |
Related Topics >>
Blog Links >>
- Printer-friendly version
- bhavanishankar's blog
- 1768 reads





