开发者

Maven Embedded Glassfish Plugin - Datasource and JDBC driver

开发者 https://www.devze.com 2023-03-10 02:53 出处:网络
I\'m trying to follow this approa开发者_Python百科ch to create a datasource for embedded glassfish. The only significant difference between the tutorial and my code is that I\'m running the server usi

I'm trying to follow this approa开发者_Python百科ch to create a datasource for embedded glassfish. The only significant difference between the tutorial and my code is that I'm running the server using the official maven plugin and the embedded-glassfish:run goal.

There is a dependency for the JDBC driver in the pom file:

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>1.3.155</version>
</dependency> 

And my glassfish-resources.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd">
<resources>
  <jdbc-resource enabled="true" jndi-name="jdbc/myDatasource" object-type="user" pool-name="connectionPool">
    <description/>
  </jdbc-resource>
  <jdbc-connection-pool datasource-classname="org.h2.jdbcx.JdbcDataSource"  name="connectionPool" res-type="javax.sql.DataSource">
    <property name="URL" value="jdbc:h2:~/Documents/Projetos/DBs/h2/glassfish/glassfish;AUTO_SERVER=TRUE;MVCC=TRUE"/>
    <property name="User" value="sa"/>
    <property name="Password" value=""/>
    <property name="driverClass" value="org.h2.Driver"/>
  </jdbc-connection-pool>
</resources> 

It runs fine using a local glassfish server. But no lucky with the embedded instance:

03/06/2011 19:26:31 com.sun.enterprise.v3.server.ApplicationLifecycle deploy GRAVE: Invalid resource : jdbc/myDatasource_pm java.lang.RuntimeException: Invalid resource : jdbc/myDatasource_pm at com.sun.enterprise.connectors.ConnectorRuntime.lookupDataSourceInDAS(ConnectorRuntime.java:539) at com.sun.enterprise.connectors.ConnectorRuntime.lookupPMResource(ConnectorRuntime.java:468) at org.glassfish.persistence.common.PersistenceHelper.lookupPMResource(PersistenceHelper.java:63) at org.glassfish.persistence.jpa.ProviderContainerContractInfoBase.lookupDataSource(ProviderContainerContractInfoBase.java:71) at

I'm clueless... Can anyone shed some light on it?


Figured it out. It had nothing to do with glassfish-resources.xml after all. The jdbc resource was properly created and bound to java:app/jdbc/myDatasource.

My persistence unit (generated by Netbeans) was looking for jdbc/myDatasource and not finding anything. I ended up changing my persistence unit to point to the application namespace:

<jta-data-source>java:app/jdbc/myDatasource</jta-data-source>

Another option would be using the embedded-glassfish:admin goal to deploy a global resource with non-standard name (so that I could use jdbc/myDatasource). But going standard seemed like the best approach.

This topic was very helpful.


I'm still not able to get glassfish-resources.xml working. But this gem here seems to do the trick:

@DataSourceDefinition(name = "java:app/jdbc/myDatasource", 
 className = "org.h2.jdbcx.JdbcDataSource", 
 url = "jdbc:h2:~/Documents/Projetos/DBs/h2/glassfish/glassfish;AUTO_SERVER=TRUE;MVCC=TRUE", 
 user = "glassfish",
 password="emb"
) 

I created another user because if password is empty some nasty exceptions are thrown. (Bug?)

0

精彩评论

暂无评论...
验证码 换一张
取 消