ENV var for className in DataSourceDefinition doesn't work

145 views
Skip to first unread message

Hans Pikkemaat

unread,
Jan 13, 2021, 1:03:17 PM1/13/21
to Payara Forum
Hi,

I've got the following DataSourceDefinition

@DataSourceDefinition(
name = "java:global/dbDocumentMetadata",
className = "${ENV=DB_DRIVER}",
url = "${ENV=DB_JDBC_URL}",
user = "${ENV=DB_USER}",
password = "${ENV=DB_PASSWORD}"
)
And set and export these env vars in my shell.
During startup of payara micro I get
   RAR5099 : Wrong class name or classpath for Datasource Object
   java.lang.ClassNotFoundException: ${ENV=DB_DRIVER}
If I replace ${ENV=DB_DRIVER} with the actual driver it works ok.
If I remove the DataSourceDefinition and use a web.xml:
<data-source>
<name>java:global/dbDocumentMetadata</name>
<class-name>${ENV=DB_DRIVER}</class-name>
<server-name>${ENV=DB_SERVER}</server-name>
<port-number>5432</port-number>
<database-name>${ENV=DB_DATABASE}</database-name>
<user>${ENV=DB_USER}</user>
<password>${ENV=DB_PASSWORD}</password>
</data-source>
Then it does work ok (port must be numeric so cannot use ENV)
So my question is why the ENV=DB_DRIVER doesn't work in the DataSourceDefinition
gr. Hans

Eduard Drenth

unread,
Jan 15, 2021, 4:00:20 AM1/15/21
to Hans Pikkemaat, Payara Forum
I guess you'd best use jta and move your datasource definition to payara, example:

persistence.xml snippet:

<persistence-unit name="oat_unit" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>jdbc/oatresource</jta-data-source>

dockerfile snippet (BUILDKIT):

RUN --mount=type=secret,id=pwdprd,required,mode=0444 \
--mount=type=secret,id=ldap,required,mode=0444 \
--mount=type=secret,id=payarainit,required,mode=0444 \
--mount=type=secret,id=ldapinit,required,mode=0444 \
${PAYARA_DIR}/bin/asadmin -u admin -W /run/secrets/pwdprd start-domain &&\
${PAYARA_DIR}/bin/asadmin -u admin -W /run/secrets/pwdprd multimode --file /run/secrets/payarainit &&\
${PAYARA_DIR}/bin/asadmin -u admin -W /run/secrets/ldap multimode --file /run/secrets/ldapinit &&\
${PAYARA_DIR}/bin/asadmin -u admin -W /run/secrets/pwdprd multimode --file deploy.txt &&\
rm *

payarainit snippet:

create-password-alias oatdbpw

create-jdbc-connection-pool --datasourceclassname org.postgresql.ds.PGConnectionPoolDataSource --isolationlevel=read-committed --restype javax.sql.ConnectionPoolDataSource --property user=oatweb:password=${ALIAS=oatdbpw}:serverName=pgdboat:databaseName=oat:portNumber=5432:driverClass=org.postgresql.Driver oatpool
create-jdbc-resource --connectionpoolid oatpool jdbc/oatresource

docker-compose snippet:

networks:
oat:
aliases:
- pgdb${APPNAME}


Hope this helps, Eduard

--
You received this message because you are subscribed to the Google Groups "Payara Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to payara-forum...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/payara-forum/CA%2Bk6B5pOagQXY%3DWQw0MmxGKr0%3DEk2FCZK-x_%2Bu235krzxdSatg%40mail.gmail.com.
signature.asc

Hans Pikkemaat

unread,
Jan 16, 2021, 11:40:23 AM1/16/21
to Payara Forum
Hi Eduard,

Yes in my payara-full setup I also configure things using asadmin commands. 
What I wanted to achieve though is using environment variables for the configured values in the @DatasourceDefinition.
I wanted to use this in a payara-micro setup.
So I was wondering why only the className was not picked up. Probably a bug. 
In the meantime I also posted it as a payara issue


Thanks for responding though :)

gr. Hans

Eduard Drenth

unread,
Jan 16, 2021, 2:59:07 PM1/16/21
to Hans Pikkemaat, Payara Forum
I see, is the construction with ${...} in annotations glassfish/payara specific or part of some EE spec?

Op za 16 jan. 2021 17:40 schreef Hans Pikkemaat <the...@gmail.com>:

Hans Pikkemaat

unread,
Jan 26, 2021, 4:07:05 PM1/26/21
to Eduard Drenth, Payara Forum

rbe...@gmail.com

unread,
Jan 26, 2021, 4:32:10 PM1/26/21
to Payara Forum
Hi, 
Try this (works for me)

/WEB-INF/web.xml
----------------------------------------------
         version="4.0">
 
    <data-source>
        <name>jdbc/database</name>
        <class-name>${ENV=DATABASE_DRIVER}</class-name>        
        <url>jdbc:${ENV=DATABASE_TYPE}:${ENV=DATABASE_URL}</url>
        <user>${ENV=DATABASE_USER}</user>
        <password>${ENV=DATABASE_PASSWORD}</password>
    </data-source>
:::
</web-app>

Java code
-------------------------

@ApplicationScoped
public class RdbmsDao implements Dao
{
    @Resource(name = "jdbc/database")
    private DataSource dataSource;
    : : :
}

pom.xml
---------------------
    <dependencies>

        <!-- dependency por Oracle 11gXE -->
        <dependency>
            <groupId>com.oracle.database.jdbc</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.4</version>
        </dependency>
       : : :
  </dependencies>


deploy Payara micro
----------------------------
export DATABASE_DRIVER="oracle.jdbc.pool.OracleDataSource" 
export DATABASE_TYPE="oracle" 
export DATABASE_URL="thin:@localhost:1521:xe"
export DATABASE_USER="your username"
export DATABASE_PASSWORD="your password" 
export MP_OPENAPI_SERVERS="http://localhost:8080"
java -jar /opt/payara/micro/payara-micro-5.2020.4.jar --deploy  app-1.0.war --contextroot /

deploy Docker compose
-----------------------------------
app:
    build:
        context: ./backend
        dockerfile: Dockerfile
    networks:
        : : :
    ports:
        - 80:8080
    environment:
        - DATABASE_DRIVER=oracle.jdbc.pool.OracleDataSource
        - DATABASE_TYPE=oracle
        - DATABASE_URL=thin:@oracledb:1521:xe
        - DATABASE_USER=yourUsername
        - DATABASE_PASSWORD=yourPassword
    depends_on:
        - : : : :

Hans Pikkemaat

unread,
Jan 26, 2021, 4:58:51 PM1/26/21
to Payara Forum
Hi,

Yes I know that it can be used in a web.xml. See here the file in my test project.
The only possible issue could be that the port cannot be set separately as it must be numeric.

I simply wanted to know if this is a bug or I’m missing something.

gr. Hans


rbe...@gmail.com

unread,
Jan 26, 2021, 5:16:52 PM1/26/21
to Payara Forum
Sorry, It`s not a bug

You can't define port as an environment variable.
The solution I have found is to define the url with that specific format

Hans Pikkemaat

unread,
Jan 26, 2021, 5:19:04 PM1/26/21
to Payara Forum
sorry I was’nt clear. With the bug I meant the initial problem where I could not use ${ENV=…} in the  @DataSourceDefinition
for the className property.

rbe...@gmail.com

unread,
Jan 26, 2021, 5:35:24 PM1/26/21
to Payara Forum
It may be a bug, I don’t know.   @DatasourceDefinition works on payara server but not on payara micro. <data-source>  works on both

Hans Pikkemaat

unread,
Jan 26, 2021, 7:28:46 PM1/26/21
to Payara Forum
@DatasourceDefinition works fine on payara-micro, only the class-name cannot be configured using an ${ENV=var} construction


Reply all
Reply to author
Forward
0 new messages