Wednesday Apr 10, 2013

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> 

 

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
« July 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  
       
Today