How to create a datasource

When working with Java applications, a datasource has two components, both equally important:

  1. JDBC Driver

  2. Datasource Definition

The JDBC driver is responsible to knowing how to communicate with the database, while providing a constant API to application developers. An application must bring their own JDBC driver, since the range of available databases and versions of each driver is rather large. Usually the application will not directly interact with the JDBC driver. Instead, the underlying runtime will manage creating a datasource which provides an efficient way to share and manage a discrete connection (or pool of connections) to a particular database using the driver.

JDBC Drivers

Auto-detection

WildFly Swarm has the capability to detect, install and register a variety of JDBC drivers based on their inclusion as a dependency to your application.

The currently detectable drivers include:

  • H2

  • MySQL

  • PostgreSQL

  • EnterpriseDB

  • SQLServer

  • Oracle

  • Sybase

  • DB2

Simply add the appropriate dependency (with the default <scope>compile</scope> scope) to your application:

<dependency>
  <groupId>com.h2database</groupId>
  <artifactId>h2</artifactId>
  <version>${version.h2}</version>
</dependency>
<dependency>
  <groupId>org.postgresql</groupId>
  <artifactId>postgresql</artifactId>
  <version>${version.postgresql}</version>
</dependency>
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>${version.mysql}</version>
</dependency>

Explicit JDBC Driver Configuration

In the event you need to provide customized driver configuration instead of relying on the auto-detection, you can use project-defaults.yml to define the driver information. This presumes you’ve followed the normal WildFly instructions for creating an appropriate module.xml to expose the driver through JBoss Modules. This is completely unnecessary if you rely upon the auto-detection mentioned above.

Underneath the tree of swarmdatasourcesjdbc-drivers, you can define the driver by key, which will be used later for connecting it to a datasource.

swarm:
  datasources:
    jdbc-drivers:
      myh2:
        driver-class-name: org.h2.Driver
        xa-datasource-name: org.h2.jdbcx.JdbcDataSource
        driver-module-name: com.h2database.h2

Datasources

Once a driver has been configured, through either auto-detection or explicit configuration, you can use project-defaults.yml to configure one or more datasources using that driver, under swarmdatasourcesdata-sources:

swarm:
  datasources:
    data-sources:
      MyDS:
        driver-name: myh2
        connection-url: jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
        user-name: sa
        password: sa

Full project-defaults.yml

swarm:
  datasources:
    data-sources:
      MyDS:
        driver-name: myh2
        connection-url: jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
        user-name: sa
        password: sa
    jdbc-drivers:
      myh2:
        driver-class-name: org.h2.Driver
        xa-datasource-name: org.h2.jdbcx.JdbcDataSource
        driver-module-name: com.h2database.h2

Sources

results matching ""

    No results matching ""