2

I'm trying to develop a website using java EE, which will be deployed to a remote server, i am trying to implement JPA into the application.
For testing purposes i'd like to create a variable persistence unit, so that on the local deployment, the application will use my local mySQL server, while on the remote deployment it will use the server's provided mySQL server.

the problem however is that i'm running on glassfish locally, and jboss remote, so i can't make the resource JNDI names for the datasources the same (since jboss requires "java:/" or "java:jboss/" as a prefix, while glassfish doesn't allow :'s in JNDI names)

another problem is that i'm not simply allowed to create 2 persistence units with the same name,
i've tried making 2 different persistence units, but then the deployment fails because one of the persistence units fails to resolve.

below is my persistence.xml at this time:

<persistence-unit name="LocalPU">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>jdbc/website</jta-data-source>
    <properties>
        <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
        <property name="eclipselink.ddl-generation.output-mode" value="both"/>
    </properties>
        </persistence-unit>

<persistence-unit name="RemotePU">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>java:/website</jta-data-source>
    <properties>
        <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
        <property name="eclipselink.ddl-generation.output-mode" value="both"/>
    </properties>
</persistence-unit>

so my question is, is it possible to have an entitymanager resolve to EITHER of these persistence units, but not require both persistence units to be available

EDIT:

about 5 minutes after posting this question, i found an article that suggests using environment variables

however this does not seem to work within glassfish,

this persistence.xml:

<persistence-unit name="LocalPU">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <jta-data-source>${myds}</jta-data-source>
        <properties>
            <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
            <property name="eclipselink.ddl-generation.output-mode" value="both"/>
        </properties>
</persistence-unit>

and the JVM-option -Dmyds=jndi/website results in the following error:

Exception while preparing the app : Invalid resource : ${myds}__pm
com.sun.appserv.connectors.internal.api.ConnectorRuntimeException: Invalid resource : ${myds}__pm

which leads me to believe environment variables can't be parsed within glassfish (???)

sirius black
  • 97
  • 1
  • 6
  • I'm not sure why you would need to use java:/website in JBoss, as this is something that is fairly consistent between servers. What would be done if the underlying naming scheme is different is use a constant name in your persistence.xml, jdbc/website for instance, and then correct it with resource-ref links in server specific deployment descriptors. See http://docs.jboss.org/jbossas/jboss4guide/r1/html/ch3.chapter.html#ch3.ejbref.ex and http://docs.oracle.com/cd/E18930_01/html/821-2418/beaoa.html – Chris May 08 '14 at 14:54
  • the second link you provided gave me a handy insight into the issue apparently within glassfish, the "java:" prefix is implicit, so where i'm using "jdbc/website" it's actually parsed as "java:jdbc/website" therefore using "jboss/datasources/MySQLDS" and renaming the reference to that within glassfish should (in theory) work – sirius black May 09 '14 at 22:52

1 Answers1

1

after following the tips provided in the first comment above, i have concluded that the question is irrelevant, and that my issues were caused by my misunderstanding of how JNDI names are displayed/parsed differently between glassfish, jboss, and JPA (in my case this has switched to hibernate, since openshift's Jboss servers turned out not to support eclipselink after all)

Glassfish, names the JNDI resource as jdbc/website, yet parses it as java:jdbc/website

Jboss on the other hand, requires the "java:" prefix to be defined explicitly, so in order to adress the same data source, it should be named java:jdbc/website

i'm not entirely sure how JPA/Java EE parse the JNDI names internally, but it seems as though either version works, so both java:jdbc/website AND jdbc/website would succesfully connect to the datasource defined in both the glassfish and the jboss environment. at least i've been able to succesfully build to both jboss and glassfish using the java:jdbc/website datasource name, and i've had the same result with jdbc/website

sirius black
  • 97
  • 1
  • 6