wildfly-extras / wildfly-datasources-galleon-pack

WildFly Feature Pack for DataSources
Apache License 2.0
21 stars 18 forks source link

Configuration of multiple datasources (same DB type) #35

Open wisteso opened 3 years ago

wisteso commented 3 years ago

I've read the documentation which talks about using environment variables to set the DB user/pass/schema/etc. However, multiple datasources will not work this way unless there's something I'm missing?

Not using OpenShift - Looking for something that will work on bare metal or cloud.

jfdenise commented 3 years ago

@wisteso , yes, for multiple datasources you will need to use WildFly CLI scripts. If you are using Bootable JAR, then CLI scipt execution is integrated in the Maven plugin that produces WildFly Bootable JAR.

d-schmidt commented 2 years ago

The solution looks like this:

<plugin>
  <groupId>org.wildfly.plugins</groupId>
  <artifactId>wildfly-jar-maven-plugin</artifactId>
  <version>${wildfly.bootable.jar.plugin.version}</version>
  <configuration>
    <dump-original-artifacts>true</dump-original-artifacts>
    <log-time>true</log-time>
    <contextRoot>false</contextRoot> <!-- disable root deployment -->
    <feature-packs>
      <feature-pack>
        <location>wildfly@maven(org.jboss.universe:community-universe)#${wildfly.version}</location>
      </feature-pack>
      <feature-pack>
        <groupId>org.wildfly</groupId>
        <artifactId>wildfly-datasources-galleon-pack</artifactId>
        <version>${wildfly.ds.galleon.version}</version>
      </feature-pack>
    </feature-packs>
    <layers>
      <layer>jaxrs-server</layer>
      <layer>mail</layer>
      <layer>postgresql-driver</layer>
      <layer>ejb-lite</layer>
    </layers>
    <excluded-layers>
      <layer>deployment-scanner</layer>
    </excluded-layers>
    <cli-sessions>
      <cli-session>
        <script-files>
          <script>../wildfly-jar-scripts/server-cfg.cli</script>
        </script-files>
        <properties-file>wildfly.properties</properties-file>
      </cli-session>
      <cli-session>
        <script-files>
          <script>../wildfly-jar-scripts/db.cli</script>
        </script-files>
        <!-- We want the env variables to be resolved during server execution -->
        <resolve-expressions>false</resolve-expressions>
      </cli-session>
    </cli-sessions>
    <extra-server-content-dirs>
      <extra-content>../manual/wildfly-jar-extra-content</extra-content>
    </extra-server-content-dirs>
    <plugin-options>
      <jboss-fork-embedded>false</jboss-fork-embedded>
    </plugin-options>
    <cloud>
      <type>kubernetes</type>
    </cloud>
  </configuration>
  <executions>
    <execution>
      <goals>
        <goal>package</goal>
      </goals>
    </execution>
  </executions>
</plugin>

and db.cli with multiple lines like:

data-source add --name=read_write --jndi-name=java:jboss/datasource/ReadWriteDS --jta=true --driver-name=postgresql --user-name=${env.POSTGRESQL_USER} --password=${env.POSTGRESQL_PASSWORD} --connection-url=jdbc:postgresql://${env.POSTGRESQL_HOST}:${env.POSTGRESQL_PORT}/${env.POSTGRESQL_DB} --exception-sorter-class-name=org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLExceptionSorter --validate-on-match=false --background-validation=true --background-validation-millis=10000 --check-valid-connection-sql="SELECT 1"