Turns out the datasource just was not initialized successfully
(although no errors apperared in the log).
I found out this by adding debug that showed all jdbc resources:
glassfish.start();
String command = "list-jdbc-resources";
CommandRunner commandRunner = glassfish.getCommandRunner();
CommandResult commandResult = commandRunner.run(command);
log.info("jdbc resources=" + commandResult.getOutput());
The root cause was that domain.xml had the connection pool and
datasource in the wrong place.
On Mon, Apr 11, 2011 at 2:39 PM, janne postilista
<jannepostilistat_at_gmail.com> wrote:
> I'm trying to test my application in Embedded GlassFish 3.1.
>
> I have an ear, inside are some ejb jars, the code uses JPA and
> persistence.xml reference to datasource foo-ds:
>
> <persistence version="2.0"
> xmlns="http://java.sun.com/xml/ns/persistence"
> 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="foo-pu" transaction-type="JTA">
> <jta-data-source>jdbc/foo-ds</jta-data-source>
> ...
>
> I try to test this:
>
> @Test
> public void test() throws Exception {
> File configFile = new File("target/classes/blank-domain.xml");
> File tempDir = new File("target/embedded-glassfish-instance");
> String configUri = configFile.toURI().toString();
> GlassFishProperties p = new GlassFishProperties();
> p.setConfigFileReadOnly(true);
> p.setConfigFileURI(configUri);
> p.setProperty("glassfish.embedded.tmpdir", tempDir.getAbsolutePath());
> p.setProperty("keep-temporary-files", "true");
>
> GlassFish glassfish = GlassFishRuntime.bootstrap().newGlassFish(p);
> glassfish.start();
>
> File ear = new File("target/services-ear.ear");
> System.out.println(ear.getAbsolutePath());
> Deployer deployer = glassfish.getDeployer();
> deployer.deploy(ear);
>
> And I get error:
>
> PlainTextActionReporterFAILUREDescription: deploy AdminCommandError
> occurred during deployment: Exception while preparing the app :
> Invalid resource : jdbc/foo-ds__pm. Please see server.log for more
> details.
> [name=services-ear
>
> Exception while invoking class
> org.glassfish.persistence.jpa.JPADeployer prepare method :
> java.lang.RuntimeException: Invalid resource : jdbc/foo-ds__pmdeployed
> ear
>
> The resource is defined in blank-domain.xml, which seems to be read as
> adding errors to it generates "Failed to create a habitat".
> blank-domain.xml:
>
> <jdbc-resource pool-name="foo-pool" jndi-name="jdbc/foo-ds" />
> <jdbc-connection-pool
> datasource-classname="org.h2.jdbcx.JdbcDataSource" name="foo-pool">
> <property name="User" value="sa" />
> <property name="URL" value="jdbc:h2:tcp://localhost/~/test" />
> <property name="driverClass" value="org.h2.Driver" />
> <property name="Password" value="sa" />
> </jdbc-connection-pool>
>
> Nothing useful in the logs. Where does the "__pm" come from? I see
> some bugs like http://java.net/jira/browse/GLASSFISH-15430 that claim
> to be fixed.
>