Configure DataSources for Maven Embedded GlassFish
By Bruno.Borges-Oracle on Apr 10, 2013
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:
- Create a file called glassfish-resources.xml inside your WEB-INF webapplication directory with the connection pool and datasource configured, by either:
- Adding the content of this Gist into it and edit it with your database information, or ...
- Create it using NetBeans as described in this blog post by Arun. Move the file to the src/webapps/WEB-INF folder.
- 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: