tainos
tainos

Reputation: 410

How do I add JDBC drivers and configure JDBC Resources in Payara Micro?

What are my options to set up JDBC drivers and resources when using Java EE Payara Micro?

Upvotes: 17

Views: 13814

Answers (6)

thomas.mc.work
thomas.mc.work

Reputation: 6464

As the accepted answer didn't work for me a figured a different and slightly easier way. You still rely on the custom domain.xml, but the startup 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 doesn't require you to know the Main class.

Upvotes: 3

igorzg
igorzg

Reputation: 1506

One of options 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>

Complete example with entity manager implementation you can find: https://github.com/igorzg/payara-micro-jpa-multi-tenancy

Upvotes: 1

Anton Stolbunov
Anton Stolbunov

Reputation: 356

This method combines answers from Mike and Adam Bien via tainos. It involves making a new domain.xml, which is a Payara config file. No application modification is needed if it worked with full Payara. The example below is for PostgreSQL JDBC.

  1. Open payara-micro.jar with an archive manager and extract the file /microdomain.xml.
  2. Open microdomain.xml in a text editor.
  3. If your application was already deployed to a full Payara, you can copy-paste the changes below from the full Payara's domain.xml.
  4. Add right above the line containing </resources>, using your dbname, dbuser, dbpassword, hostname:port and poolname:

    <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>
    
  5. Add right above the line containing </server>:

    <resource-ref ref="jdbc/poolname"></resource-ref>
    
  6. Save and close the text editor.
  7. Start Payara micro from command line, using your paths and filenames. Linux syntax:

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

Upvotes: 12

tainos
tainos

Reputation: 410

Adam Bien answered this question in his 19th Airhacks video.

My take, When used with custom resources is best used as an embedded server, in the main we configure the JDBC resources and with maven dependencies we include the needed drivers inside the jar or war files.

Upvotes: 2

S Millidge
S Millidge

Reputation: 101

Add the datasource definition to your web.xml and then add the jar file for the JDBC jar into 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 on the Payara Examples GitHub repository. See Datasource example on Payara GitHub

Upvotes: 10

Mike
Mike

Reputation: 4963

You can configure JDBC in a normal domain.xml and supply that to Payara. If you're unsure, you can always take an existing domain.xml and use the JDBC configuration from that.

Payara Micro has a few 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're bootstrapping Payara Micro programmatically, you would use:

setAlternateDomainXML(File alternateDomainXML)

Upvotes: 3

Related Questions