javajakarta-eepayarapayara-micro

Switch between JNDI names for databases in PROD and TEST


I'm working on a Maven EJB module.

My EJBs are all using a persistencecontext like this:

@PersistenceContext(unitName = "dk.mycontext")
private EntityManager em;

It works really well. Problem is when i want to change from testing to actually deploy on my app server.

I'm using JUnit for testing and running all tests on Payara Embedded. Payara embedded uses this format for JNDI look up (in my persistence.xml file):

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

When testing the data source is found

When i deploy the compiled module to my full blown Payara server it complains that it can't find the data source:

javax.naming.NameNotFoundException: No object bound to name java:app/jdbc/druid

since it expects the JNDI to be like this:

 jdbc/druid

which is the format in which i have defined the JNDI name directly on the server. Problem is - as far as i can see - that the payara server will not accept a naming convention with

java:app/jdbc/druid

at least the Admin interface will not validate it.

How do I control this crazy behavior? I cant be the first one to experience this problem but I have been unable to find a viable solution for this problem.

Can I control the naming using Maven for instance?


Solution

  • Use maven profiles:

    persistence.xml

    ...
    <jta-data-source>${datasource}</jta-data-source>
    ...
    

    pom.xml

    <properties>
        <datasource>java:app/jdbc/druid</datasource>
    </properties>
    
    <profiles>
    <profile>
       <id>release</id>
       <properties>
          <datasource>jdbc/druid</datasource>
       </properties>
    </profile>
    </profiles>