How to add JDBC drivers and configure JDBC resources in Payara Micro?

What are my settings for configuring JDBC drivers and resources when using Java EE Payara Micro?

+8
java java-ee jdbc payara payara-micro
source share
6 answers

This method combines responses from Mike and Adam Bean through secrecy. It includes creating a new domain.xml , which is a Payara configuration file. No modification of the application is required if it works with full Payara. Example below for PostgreSQL JDBC.

  • Open payara-micro.jar with the archive manager and extract the /microdomain.xml file.
  • Open microdomain.xml in a text editor.
  • If your application has already been deployed to the full version of Payara, you can copy-paste the following changes from the full version of Payara domain.xml .
  • Add right above the line containing </resources> using your dbname, dbuser, dbpassword, hostname: port and pool name:

     <jdbc-connection-pool connection-validation-method="auto-commit" driver-classname="org.postgresql.Driver" res-type="java.sql.Driver" name="poolname" is-connection-validation-required="true" connection-creation-retry-attempts="3" validate-atmost-once-period-in-seconds="60"> <property name="URL" value="jdbc:postgresql://localhost:5432/dbname"></property> <property name="user" value="dbuser"></property> <property name="password" value="dbpassword"></property> </jdbc-connection-pool> <jdbc-resource pool-name="poolname" jndi-name="jdbc/poolname"></jdbc-resource> 
  • Add right above the line containing </server> :

     <resource-ref ref="jdbc/poolname"></resource-ref> 
  • Save and close the text editor.
  • Launch the Payara micro program from the command line using your paths and file names. Linux syntax:

     java -cp "/opt/jdbc/postgresql.jar:/opt/payara/micro.jar" fish.payara.micro.PayaraMicro --deploy webapp.war --domainConfig microdomain.xml 
+7
source share

Add the data source definition to your web.xml and then add the jar file for the JDBC jar to your WEB-INF / lib. Then deploy the war file, as usual, to Payara Micro.

 <data-source> <name>java:global/ExampleDataSource</name> <class-name>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</class-name> <server-name>localhost</server-name> <port-number>3306</port-number> <database-name>mysql</database-name> <user>root</user> <password>root</password> <!-- Example of how to use a Payara specific custom connection pool setting --> <property> <name>fish.payara.sql-trace-listeners</name> <value>com.sun.gjc.util.SQLTraceLogger</value> </property> </data-source> 

There is a complete example of how to do this in the GareHub repository for Payara samples. See an example data source in Payara GitHub

+5
source share

You can configure JDBC in a regular domain.xml and provide this with Payara. If you are not sure, you can always take the existing domain.xml file and use the JDBC configuration.

Payara Micro has several command line options , one of which allows you to specify an alternative domain.xml file:

 java -jar payara-micro.jar --deploy myApp.war --domainConfig mydomain.xml 

If you are downloading the Payara Micro programmatically, you should use:

 setAlternateDomainXML(File alternateDomainXML) 
+2
source share

Adam Bien answered this question in his 19th air show .

My take. When to use with user resources it is best to use as an embedded server, we mainly configure JDBC resources and with maven dependencies, we include the necessary drivers inside jar or war files.

0
source share

One option is glassfish-resources.xml

 <!-- db1 --> <jdbc-connection-pool datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" name="db1" res-type="javax.sql.DataSource" steady-pool-size="1" is-connection-validation-required="true" connection-validation-method="meta-data" max-pool-size="10"> <property name="password" value="icoder_pwd"/> <property name="user" value="icoder_user"/> <property name="databaseName" value="icoder_db"/> <property name="serverName" value="localhost"/> <property name="portNumber" value="3310"/> <property name="zeroDateTimeBehavior" value="convertToNull"/> </jdbc-connection-pool> <jdbc-resource pool-name="db1" jndi-name="jdbc/db1"/> <!-- db2 --> <jdbc-connection-pool datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" name="db2" res-type="javax.sql.DataSource" steady-pool-size="1" is-connection-validation-required="true" connection-validation-method="meta-data" max-pool-size="10"> <property name="password" value="icoder_pwd"/> <property name="user" value="icoder_user"/> <property name="databaseName" value="icoder_db"/> <property name="serverName" value="localhost"/> <property name="portNumber" value="3311"/> <property name="zeroDateTimeBehavior" value="convertToNull"/> </jdbc-connection-pool> <jdbc-resource pool-name="db2" jndi-name="jdbc/db2"/> </resources> 

In the completed example with the implementation of the entity manager, you can find: https://github.com/igorzg/payara-micro-jpa-multi-tenancy

0
source share

As the accepted answer did not help me in another, and a little easier. You still rely on custom domain.xml , but the start command can be simplified:

 java -jar /opt/payara/payara-micro.jar --deploy webapp.war --domainConfig domain.xml --addJars /opt/mysql-connector-java-5.1.40-bin.jar 

This call does not require you to know the main class.

0
source share

All Articles