Configure DataSources for Maven Embedded GlassFish

 

In my previous post I showed to you how to configure Maven and the Embedded GlassFish Plugin to be run with GlassFish 4.0 b83. A comment on that post raised the following question: Is it now possible to setup datasources with GlassFish embedded for testing purposes? The answer is yes! Not only DataSource but any resource. For now, I only tested DataSources but it's working fine. Follow these steps:

  1. Create a file called glassfish-resources.xml inside your WEB-INF webapplication directory with the connection pool and datasource configured, by either:
    1. Adding the content of this Gist into it and edit it with your database information, or ...
    2. Create it using NetBeans as described in this blog post by Arun. Move the file to the src/webapps/WEB-INF folder.
  2. Make sure the JNDI name is similar to this: java:app/jdbc/YourDataSourceName and that it is correctly mapped in your persistence.xml

The most important thing here is to keep the JNDI name with java:app/ prefix. I tried using just jdbc/MyDS on both persistence.xml and glassfish-resources.xml, but it didn't work, shoulting errors in the log related to the __pm suffix issue. So remember the prefix, as this is an application-scoped resource.

UPDATE Also, don't forget to add the following dependency for your Database JDBC Driver inside the <plugin> of Embedded GlassFish. For MySQL, add this:

                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.24</version>
                    </dependency> 

 

Comments:

Excellent !!!
Does it works too with GF embedded 3.1 ?

Posted by usul_ on April 10, 2013 at 01:14 PM PDT #

Why not use the Java EE standard syntax in web.xml for this?

Posted by John on April 12, 2013 at 09:59 AM PDT #

@usul: Yes, it works for GF 3.1.2.2 at least. Just don't forget to use the correct prefix, as Bruno already mentioned.

Posted by Michel Graciano on April 15, 2013 at 05:31 AM PDT #

I tried "java:app/" prefix in jndi name, but it didn't work... raised a "__pm" sufix problem. I'm running GF3.1.2.2.

Posted by Jauzmera on December 16, 2014 at 09:52 AM PST #

This seems or seemed to be the answer to my post on http://www.coderanch.com/t/644331/EJB-JEE/java/EJB-error-withing-unit-test
but it doesn't work... Using Glassfish 4.1 and Derby...
When creating the entity classes and Restful services with the wizards and the unit tests too, there is a file sun.resources.xml instead of glassfish.resources.xml, I tried with both names, in different directories, but it doesn't matter...
But I must say I don't even know what the sentence " Also, don't forget to add the following dependency for your Database JDBC Driver inside the <plugin> of Embedded GlassFish. For MySQL, add this:

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.24</version>
</dependency> " is about...
I'm searching through the Glassfish admin papers to find where to add this entry to.

Posted by ahum on January 04, 2015 at 10:37 AM PST #

Post a Comment:
  • HTML Syntax: NOT allowed
About


Bruno has been having fun working with Java since 2000 and now helps Oracle on sharing the technology accross all Latin America. Also plays videogames, does trekking and loves beer.

Follow me on Twitter! @brunoborges

Search

Archives
« April 2015
SunMonTueWedThuFriSat
   
1
2
3
4
5
6
7
8
9
10
11
12
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  
       
Today