Skip to main content

Embedded GlassFish with installation-less MySQL

Posted by bhavanishankar on April 4, 2011 at 11:46 PM PDT


http-equiv="content-type">
Accessing MySQL from Embedded GlassFish


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






src="http://www.java.net/sites/default/files/EmbeddedGF_with_MySQL.png">

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





src="http://www.java.net/sites/default/files/EmbeddedGF_with_EmbeddedMySQL.png">



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.



 @Test 
public void class="s1">test() throws class="s1">Exception {
GlassFishProperties glassFishProperties = class="s2">new GlassFishProperties();
name="l64"> glassFishProperties.setPort("http-listener"class="s1">, 8080);
name="l65">
GlassFish glassFish = GlassFishRuntime.bootstrap().
name="l67"> newGlassFish(glassFishProperties);
name="l68">
glassFish.start();
name="l70">
Deployer deployer = glassFish.getDeployer();
name="l72"> String appName = deployer.deploy(new class="s1">File("target/mysqltest.war"class="s1">));

class="s0">// Access the servlet
name="l76"> get("http://localhost:8080/mysqltest/mysqlTestServlet"class="s1">);
}


For scenario1, in my app I have mysqlTestServlet which accesses MySQL
DB like this:

@DataSourceDefinition(name = "java:app/mysql/MySQLDataSource"class="s1">, 
className = class="s4">"com.mysql.jdbc.jdbc2.optional.MysqlDataSource"class="s1">,
portNumber = class="s2">3306,
serverName = class="s4">"localhost",
name="l65"> databaseName = "testDB"class="s1">,
user = class="s4">"root",
password = class="s4">"abc123",
name="l68"> properties = {"createDatabaseIfNotExist=true"class="s1">}
)
@WebServlet(name = class="s4">"mySqlTestServlet", urlPatterns = class="s4">"/mysqlTestServlet")
name="l71">public class MySqlTestServlet class="s2">extends HttpServlet {
name="l72">
@Resource(mappedName = class="s4">"java:app/mysql/MySQLDataSource")
name="l74"> DataSource myDB;
.....

}



For scenario2, in my app I have mysqlTestServlet which accesses MySQL
DB like this (DataSourceDefinition differs):


@DataSourceDefinition(name = "java:app/mysql/MySQLEmbeddedDataSource"class="s1">, 
className = class="s4">"com.mysql.jdbc.Driver",
name="l64"> url = "jdbc:mysql:mxj://localhost:3336/testDB"class="s1">,
user = class="s4">"root",
password = class="s4">"abc123",
name="l67"> properties = {"createDatabaseIfNotExist=true"class="s1">,
class="s4">"server.basedir=/tmp/testDB", class="s4">"server.initialize-user=true"}
name="l69">)
@WebServlet(name = "mySqlTestServlet"class="s1">, urlPatterns = "/mysqlTestServlet"class="s1">)
public class class="s1">MySqlTestServlet extends class="s1">HttpServlet {

name="l73"> @Resource(mappedName = "java:app/mysql/MySQLEmbeddedDataSource"class="s1">)
DataSource myEmbeddedDB;
name="l74"> .....

}


Now let us see how to run these two scenarios.



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:


 <dependencyclass="s2">> 
class="s2"><groupId>class="s4">org.glassfish.extrasclass="s6">groupId>
name="l93"> <artifactIdclass="s2">>glassfish-embedded-allclass="s2">artifactIdclass="s2">>
class="s2"><version>class="s4">3.1versionclass="s2">>
class="s2">dependencyclass="s2">>
class="s2"><dependencyclass="s2">>
class="s2"><groupId>class="s4">mysqlgroupIdclass="s2">>
class="s2"><artifactIdclass="s2">>mysql-connector-javaclass="s2">artifactIdclass="s2">>
class="s2"><version>class="s4">5.1.15versionclass="s2">>
class="s2">dependencyclass="s2">>



For scenario2, you DON'T need MySQL server preinstalled/running, but
you need href="http://dev.mysql.com/doc/refman/5.5/en/connector-mxj.html"
target="_blank">MySQL Connector/MXJ (Java library for
deploying/managing MySQL
DB). For the lack of any maven repository for Connector/MXJ, please
download
it target="_blank">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):

 <dependencyclass="s2">> 
class="s2"><groupId>class="s4">com.mysql.connectorclass="s6">groupId>
name="l103"> <artifactIdclass="s2">>mxjclass="s6">artifactId>class="s4">
<class="s6">version>${project.version}class="s2">version>class="s4">
<class="s6">scope>systemclass="s2">scope>class="s4">
<class="s6">systemPath>class="s4">/tmp/mysql-connector-mxj-gpl-5-0-11/mysql-connector-mxj-gpl-5-0-11.jarclass="s2">systemPathclass="s2">>
class="s2">dependencyclass="s2">>
class="s2"><dependencyclass="s2">>
class="s2"><groupId>class="s4">com.mysql.connectorclass="s6">groupId>
name="l110"> <artifactIdclass="s2">>mxj-db-filesclass="s2">artifactIdclass="s2">>
class="s2"><version>class="s4">${project.version}class="s6">version>
name="l112"> <scopeclass="s2">>systemclass="s6">scope>
name="l113"> <systemPathclass="s2">>/tmp/mysql-connector-mxj-gpl-5-0-11/mysql-connector-mxj-gpl-5-0-11-db-files.jarclass="s2">systemPathclass="s2">>
class="s2">dependencyclass="s2">>





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 class="s1">version="2.0" class="s1">xmlns="http://java.sun.com/xml/ns/persistence"class="s2"> 
xmlns:xsi=class="s3">"http://www.w3.org/2001/XMLSchema-instance"class="s2">
xsi:schemaLocation=class="s3">"http://java.sun.com/xml/ns/persistence class="s2">>class="s2">>http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"... class="s4">

name="l47"> <persistence-unit class="s1">name="mysql_testdb" class="s1">transaction-type="JTA"class="s2">>
class="s2"><jta-data-sourceclass="s2">>java:app/mysql/MySQLEmbeddedDataSourceclass="s2">jta-data-sourceclass="s2">>
class="s2"><propertiesclass="s2">>
class="s2"><property name=class="s3">"eclipselink.ddl-generation" value=class="s3">"drop-and-create-tables"/>class="s4">
<class="s6">property name=class="s3">"eclipselink.logging.level" value=class="s3">"FINE"/>
name="l52"> propertiesclass="s2">>
class="s2">persistence-unitclass="s2">>
class="s2">persistenceclass="s2">>

I have checked in the test corresponding to this blog @ href="http://java.net/projects/glassfish/sources/svn/show/trunk/v3/tests/embedded/mysql"
target="_blank">v3/tests/embedded/mysql. The test
uses JPA also.











AttachmentSize
EmbeddedGF_with_MySQL.png46.54 KB
EmbeddedGF_with_EmbeddedMySQL.png51.02 KB