Wildfly-Swarm – Database

Tried this with Wildfly Swarm version 2017.8.1.

Adding Oracle driver.

In the project pom.xml, add the driver as a dependency:

<dependency>
   <groupId>com.oracle</groupId>
   <artifactId>ojdbc7</artifactId>
   <version>12.1.0.2</version>
</dependency>

We have to tell Wildfly-Swarm, that there is a Oracle driver to use. Therefor we add module.xml to /src/main/resources/modules/com/oracle/ojdbc7/main/

<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.3" name="com.oracle.ojdbc7">
   <resources>
      <artifact name="com.oracle:ojdbc7:12.1.0.2"/>
   </resources>
   <dependencies>
      <module name="javax.api"/>
      <module name="javax.transaction.api"/>
      <module name="javax.servlet.api" optional="true"/>
   </dependencies>
</module>

Add the datasource fraction as dependency:

<dependency>
   <groupId>org.wildfly.swarm</groupId>
   <artifactId>datasources</artifactId>
   <version>2017.8.1</version>
</dependency>

Define datasources

The files, which defines the datasources, are placed in /src/main/webapp/WEB-INF/ and have a naming convention *-ds.xml

example1-ds.xml:

<?xml version="1.0" encoding="UTF-8"?>
<datasources  
   xmlns="http://www.jboss.org/ironjacamar/schema"    
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
   xsi:schemaLocation="http://www.jboss.org/ironjacamar/schema http://docs.jboss.org/ironjacamar/schema/datasources_1_0.xsd">
   <!-- The datasource is bound into JNDI at this location. We reference
        this in META-INF/persistence.xml -->
   <datasource
      jndi-name="java:jboss/datasources/example1"
      pool-name="example1-pool"
      enabled="true"
      use-java-context="true">
      <connection-url>jdbc:oracle:thin:@serverUrl1:1521:SID</connection-url>
      <driver>oracle</driver>
      <security>
         <user-name>username1</user-name>
         <password>password1</password>
      </security>
   </datasource>
</datasources>

example2-ds.xml:

<?xml version="1.0" encoding="UTF-8"?>
<datasources  
 xmlns="http://www.jboss.org/ironjacamar/schema"    
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
 xsi:schemaLocation="http://www.jboss.org/ironjacamar/schema http://docs.jboss.org/ironjacamar/schema/datasources_1_0.xsd">
   <!-- The datasource is bound into JNDI at this location. We reference
        this in META-INF/persistence.xml -->
   <datasource
     jndi-name="java:jboss/datasources/example2"
     pool-name="example2-pool"
     enabled="true"
     use-java-context="true">
     <connection-url>jdbc:oracle:thin:@serverUrl2:1521:SID</connection-url>
      <driver>oracle</driver>
      <security>
         <user-name>username2</user-name>
         <password>password2</password>
      </security>
 </datasource>
</datasources>

Define the persistence unit

Place a persistence.xml into /src/main/resources/META-INF/. Here is an example with two datasources:

<?xml version="1.0" encoding="UTF-8"?>
<persistence
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    version="2.1"
    xmlns="http://xmlns.jcp.org/xml/ns/persistence"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">  
   <persistence-unit   
      name="example1PU"   
      transaction-type="JTA">
      <jta-data-source>java:jboss/datasources/example1</jta-data-source>
      <exclude-unlisted-classes>false</exclude-unlisted-classes>
      <properties>
         <property name="javax.persistence.schema-generation.database.action" value=""/>
         <property name="javax.persistence.schema-generation.create-source" value="metadata"/>
         <property name="javax.persistence.schema-generation.drop-source" value="metadata"/>
      </properties>
   </persistence-unit>
   <persistence-unit
      name="example2PU"
      transaction-type="JTA">
      <jta-data-source>java:jboss/datasources/example2</jta-data-source>
      <exclude-unlisted-classes>false</exclude-unlisted-classes>
      <properties>
         <property name="javax.persistence.schema-generation.database.action" value=""/>
         <property name="javax.persistence.schema-generation.create-source" value="metadata"/>
         <property name="javax.persistence.schema-generation.drop-source" value="metadata"/>
      </properties>
   </persistence-unit>
</persistence>

Reference the persistence unit

In your EJBs you can reference the persistence units:

@PersistenceContext(unitName = "example1PU" )
EntityManager em1;

@PersistenceContext(unitName = "example2PU" )
EntityManager em2;

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.