PayaraMicro is not adding jdbc details in glassfis

2020-08-01 05:58发布

I have web app I'm trying to deploy in Payara Micro's docker container, but the glassfish-resources.xml details are not added to the server's microdomain.xml.

The JDBC definition is pretty basic, utilizing environment variables set from the docker command (this is location in MyApp.war/WEB-INF/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 pool-name="MyAppDB" jndi-name="jdbc/MyAppDB" enabled="true" ></jdbc-resource>


    <jdbc-connection-pool datasource-classname="com.microsoft.sqlserver.jdbc.SQLServerDataSource" 
                          name="MyAppDB" 
                          res-type="javax.sql.DataSource">
      <property name="User" value="${ENV=MY_APP_DATABASE_USER}"></property>
      <property name="Password" value="${ENV=MY_APP_DATABASE_PASS}"></property>
      <property name="DatabaseName" value="${ENV=MY_APP_DATABASE_NAME}"></property>
      <property name="ServerName" value="${ENV=MY_APP_DATABASE_SERVER}"></property>
      <property name="PortNumber" value="1433"></property>
    </jdbc-connection-pool>    

</resources>

Here is the payara.env file:

MY_APP_DATABASE_USER=someuser
MY_APP_DATABASE_PASS=somepass
MY_APP_DATABASE_NAME=MY_APP_DB
MY_APP_DATABASE_SERVER=192.168.10.110

As you can see from the log below, mysql-jdbc-7.0.0.jre8.jar does get added to the classpath.

Docker command:

docker run -p 8080:8080 \
           --mount type=bind,src=$(pwd)/deployments,dst=/opt/payara/deployments \
           --mount type=bind,src=$(pwd)/lib,dst=/opt/payara/lib \
           --env-file payara.env \
           --restart=always \
           payara/micro:5.191 \
           --addlibs /opt/payara/lib \
           --deploy /opt/payara/deployments/my-app.war \
           --contextroot ROOT

Server output (removed superfluous logging):

[2019-05-29T16:41:46.990+0000] [] [WARNING] [] [PayaraMicro] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1559148106990] [levelValue: 900] Payara Micro Runtime directory is located in a temporary file location which can be cleaned by system processes.

[2019-05-29T16:41:47.034+0000] [] [INFO] [] [PayaraMicro] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1559148107034] [levelValue: 800] Payara Micro Runtime directory is located at /tmp/payaramicro-rt5934725251203193927tmp

[2019-05-29T16:41:47.061+0000] [] [INFO] [] [fish.payara.micro.boot.runtime.PayaraMicroRuntimeBuilder] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1559148107061] [levelValue: 800] Built Payara Micro Runtime

[2019-05-29T16:41:48.197+0000] [] [INFO] [] [PayaraMicro] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1559148108197] [levelValue: 800] Added /opt/payara/lib/mssql-jdbc-7.0.0.jre8.jar to classpath

[2019-05-29T16:41:50.422+0000] [] [INFO] [NCLS-CORE-00101] [javax.enterprise.system.core] [tid: _ThreadID=19 _ThreadName=RunLevelControllerThread-1559148109281] [timeMillis: 1559148110422] [levelValue: 800] Network Listener http-listener started in: 24ms - bound to [/0.0.0.0:8080]

[2019-05-29T16:41:50.572+0000] [] [INFO] [NCLS-CORE-00017] [javax.enterprise.system.core] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1559148110572] [levelValue: 800] Payara Micro  5.191 #badassmicrofish (94) startup time : Embedded (2,200ms), startup services(1,306ms), total(3,506ms)

[2019-05-29T16:41:53.520+0000] [] [INFO] [AS-WEB-GLUE-00198] [javax.enterprise.web] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1559148113520] [levelValue: 800] Created HTTP listener http-listener on host/port 0.0.0.0:8080

[2019-05-29T16:41:53.587+0000] [] [INFO] [AS-WEB-GLUE-00200] [javax.enterprise.web] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1559148113587] [levelValue: 800] Created virtual server server

[2019-05-29T16:41:53.957+0000] [] [INFO] [AS-WEB-CORE-00306] [javax.enterprise.web.core] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1559148113957] [levelValue: 800] Setting JAAS app name glassfish-web

[2019-05-29T16:41:53.957+0000] [] [INFO] [AS-WEB-GLUE-00201] [javax.enterprise.web] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1559148113957] [levelValue: 800] Virtual server server loaded default web module 

[2019-05-29T16:41:57.129+0000] [] [SEVERE] [] [javax.enterprise.system.tools.deployment.dol] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1559148117129] [levelValue: 1000] AS-DEPLOYMENT-00026

[2019-05-29T16:41:57.135+0000] [] [SEVERE] [] [javax.enterprise.system.core] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1559148117135] [levelValue: 1000] Exception while deploying the app [my-app]

[2019-05-29T16:41:57.135+0000] [] [SEVERE] [NCLS-CORE-00026] [javax.enterprise.system.core] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1559148117135] [levelValue: 1000] [[
  Exception during lifecycle processing
org.glassfish.deployment.common.DeploymentException: JNDI lookup failed for the resource: Name: [MyAppPU], Lookup: [jdbc/MyAppDB], Type: [javax.sql.DataSource]
    at com.sun.enterprise.deployment.util.ResourceValidator.validateJNDIRefs(ResourceValidator.java:896)

Caused by: javax.naming.NamingException: Lookup failed for 'jdbc/MyAppDB' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.url.pkgs=com.sun.enterprise.naming, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl} [Root exception is javax.naming.NameNotFoundException: MyAppDB not found]
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:496)
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:442)
    at javax.naming.InitialContext.lookup(InitialContext.java:417)
    at javax.naming.InitialContext.lookup(InitialContext.java:417)
    at com.sun.enterprise.deployment.util.ResourceValidator.validateJNDIRefs(ResourceValidator.java:891)
    ... 32 more
Caused by: javax.naming.NameNotFoundException: MyAppDB not found
    at com.sun.enterprise.naming.impl.TransientContext.doLookup(TransientContext.java:237)
    at com.sun.enterprise.naming.impl.TransientContext.lookup(TransientContext.java:204)
    at com.sun.enterprise.naming.impl.TransientContext.lookup(TransientContext.java:208)
    at com.sun.enterprise.naming.impl.SerialContextProviderImpl.lookup(SerialContextProviderImpl.java:70)
    at com.sun.enterprise.naming.impl.LocalSerialContextProviderImpl.lookup(LocalSerialContextProviderImpl.java:114)
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:483)
    ... 36 more
]]

[2019-05-29T16:41:57.284+0000] [] [INFO] [] [PayaraMicro] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1559148117284] [levelValue: 800] Deployed 1 archive(s)

[2019-05-29T16:42:01.387+0000] [] [INFO] [] [PayaraMicro] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1559148121387] [levelValue: 800] [[

Payara Micro URLs:

]]

[2019-05-29T16:42:01.388+0000] [] [INFO] [] [PayaraMicro] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1559148121388] [levelValue: 800] Payara Micro  5.191 #badassmicrofish (build 94) ready in 14,400 (ms)

UPDATE:

I added a preboot script to set a system flag deployment.resource.validation as mentioned at payara/Payara/issues/3449

preboot.asadmin:

create-system-properties deployment.resource.validation=false

The start-up logging shows that the command executed successfully, but the resources are never added:

[2019-06-03T15:04:58.993+0000] [] [INFO] [] [fish.payara.boot.runtime.BootCommand] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1559574298993] [levelValue: 800] Boot Command create-system-properties returned with result SUCCESS : PlainTextActionReporterSUCCESSNo monitoring data to report.

I also changed to using payara-resources.xml with the following DOCTYPE:

<!DOCTYPE resources PUBLIC "-//Payara.fish//DTD Payara Server 4 Resource Definitions//EN" "https://raw.githubusercontent.com/payara/Payara-Server-Documentation/master/schemas/payara-resources_1_6.dtd">

1条回答
趁早两清
2楼-- · 2020-08-01 06:19

I finally got this working. First, the application I'm deploying is a WAR with packaged EJB modules, so the persistence.xml needs to be packaged in a jar within WAR/WEB-INF/lib. Here is the structure:

WAR/WEB-INF/lib/ejb1.jar
WAR/WEB-INF/lib/ejb2.jar
WAR/WEB-INF/lib/persistence.jar
WAR/WEB-INF/lib/persistence.jar/META-INF/persistence.xml
WAR/WEB-INF/payara-resources.xml

It's important to note that the resource definition in payara-resources.xml uses environment variables. It was suggested that I place my definitions in WAR/WEB-INF/web.xml, but I prefer the control I get from using resources.xml, and I'm not sure ENV variables swapping works.

WAR/WEB-INF/payara-resources.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//Payara.fish//DTD Payara Server 4 Resource Definitions//EN" "https://raw.githubusercontent.com/payara/Payara-Server-Documentation/master/schemas/payara-resources_1_6.dtd">
<resources>
    <jdbc-resource pool-name="MyAppDB" jndi-name="java:app/jdbc/MyApp" enabled="true" ></jdbc-resource>

    <jdbc-connection-pool datasource-classname="com.microsoft.sqlserver.jdbc.SQLServerDataSource" 
                          name="MyAppDB" 
                          res-type="javax.sql.DataSource">
      <property name="User" value="${ENV=MY_APP_DATABASE_USER}"></property>
      <property name="Password" value="${ENV=MY_APP_DATABASE_PASS}"></property>
      <property name="DatabaseName" value="${ENV=MY_APP_DATABASE_NAME}"></property>
      <property name="ServerName" value="${ENV=MY_APP_DATABASE_SERVER}"></property>
      <property name="PortNumber" value="1433"></property>
    </jdbc-connection-pool>  
</resources>

And here is the persistence:

WAR/WEB-INF/lib/persistence.jar/META-INF/persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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="MyAppPU" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>java:app/jdbc/MyApp</jta-data-source>
    <!-- other stuff //-->
  </persistence-unit>
</persistence>

One of the benefits of using a jar for persistence.xml is that the persistence definition can be shared across projects (services). Unfortunately, Micro requests the java:app/ prefix to the JNDI name, where as project deployed to a Full Server doesn't use that (I haven't tested using java:app/ in a Full Server deployment, yet). Anyway, the above works.

查看更多
登录 后发表回答