WAS v8.5 > Reference > Developer detailed usage information

Liberty profile: Application-defined data sources

We can define a data source within your application, through annotations or in the deployment descriptor, as defined by the Java™ EE specification.

This capability is limited to names in java:comp. Other name spaces such as java:module, java:app, and java:global are not available.

When defining a data source in an application, the JDBC driver must be made available to the application. This is accomplished by configuring a shared library in the server.xml for your application.

For example:

 <application id="myApp" name="myApp" location="myApp.war" type="war">       <classloader commonLibraryRef="DB2Lib"/>     </application>  
    <library id="DB2Lib">       <fileset dir="C:/DB2/java" includes="db2jcc4.jar db2jcc_license_cisuz.jar"/>     </library>

Then we can define a data source in your application either through annotations or in the deployment descriptor.

In general, properties that can be defined on dataSource or connectionManager in server.xmls can also be specified on application defined data sources. However, we cannot specify properties that refer to other elements, such as connectionManagerRef and jdbcDriverRef, because the application defined data source implicitly defines the connection manager and JDBC driver. When using application-defined data sources for two-phase commit, we can specify the recoveryAuthDataRef property to select the authentication data used for transaction recovery. However, it is important to be aware that recovery of transactions is only possible while the application is running. We can use variables, encoded passwords, and duration syntax in application defined data sources.

The duration syntax does not apply to properties that are explicitly defined in the annotation, such as loginTimeout or maxIdleTime.

Here is an example of two data sources using connection manager properties, variables, encoded passwords, and duration syntax.

@DataSourceDefinitions(value = {
    @DataSourceDefinition(
        name         = "java:comp/env/jdbc/derby",
        className    = "org.apache.derby.jdbc.EmbeddedDataSource40",
        databaseName = "${shared.resource.dir}/data/SAMPLEDB",
        minPoolSize  = 1,
        maxPoolSize  = 10,
        maxIdleTime = 1.0,         properties = { "agedTimeout=10m", "connectionTimeout=30s", "createDatabase=create" }
        ),
    @DataSourceDefinition(
        name         = "java:comp/env/jdbc/oracle",
        className    = "oracle.jdbc.pool.OracleDataSource",
        url          = "jdbc:oracle:thin:@//localhost:1521/SAMPLEDB",
        user         = "user1",
        password     = "{xor}Oz0vKDtt"
        )})


Parent topic: Configuring database connectivity in the Liberty profile


Reference topic |