The <connectionPolicy> element of the SQL adapter
The <connectionPolicy> element of the SQL adapter configures how the adapter connects to an SQL database.
The <connectionPolicy> element has two options for connecting:
- Use the <dataSourceDefinition> subelement
- Use the <dataSourceJNDIName> subelement
Connect using the <dataSourceDefinition> subelement
When we use this option, we specify the URL of the data source, the user, the password, and the driver class.
This method is primarily intended for development mode. In production mode, IBM recommends to use the <dataSourceJNDIName> subelement.
The following example shows the structure of the <connectionPolicy> element with the <dataSourceDefinition> subelement:
<connectionPolicy xsi:type="sql:SQLConnectionPolicy"> <dataSourceDefinition> <driverClass>com.mysql.jdbc.Driver</driverClass> <url>jdbc:mysql://localhost:3306/mysqldbname</url> <user>mysqluser</user> <password>mysqlpassword</password> </dataSourceDefinition> </connectionPolicy>
Attribute Description xsi:type Mandatory. The value of this attribute must be set to sql:SQLConnectionPolicy. The <connectionPolicy> element has the following subelement:
Subelement Description <dataSourceDefinition> Mandatory. Contains the parameters needed to connect to a data source. The parameters (url, user, password, and driverClass) can be externalized as custom MobileFirst properties, and can then be overridden by environment entries. See Configure a MobileFirst project in production using JNDI environment entries.
The following example illustrates this process:
adapter.xml: <connectionPolicy xsi:type="sql:SQLConnectionPolicy"> <dataSourceDefinition> <driverClass>com.mysql.jdbc.Driver</driverClass> <url>${my-mysql-url}</url> <user>${my-mysql-user}</user> <password>${my-mysql-password}</password> </dataSourceDefinition> </connectionPolicy> worklight.properties: my-mysql-url=jdbc:mysql://localhost:3306/mysqldbname my-mysql-user=worklight my-mysql-password=worklight
Connect using the <dataSourceJNDIName> subelement
We can also connect to the data source using the JNDI name of a data source provided by the application server. Application servers provide a way to configure data sources. See Create the databases manually.
When we configure a data source provided by the application server, the data source must have a JNDI name. This name can be used by applications that run inside the container, to get a reference to the data source, and to use it.
The following example shows the structure of the <connectionPolicy> element with the <dataSourceJNDIName> subelement:
adapter.xml: <connectionPolicy xsi:type="sql:SQLConnectionPolicy"> <dataSourceJNDIName>jdbc/myAdapterDS</dataSourceJNDIName> </connectionPolicy>In this example, a resource with the JNDI name: "jdbc/myAdapterDS" must be declared inside the container.
The <connectionPolicy> element has the following attribute:
Attribute Description xsi:type Mandatory. The value of this attribute must be set to sql:SQLConnectionPolicy. The <connectionPolicy> element has the following subelement:
Subelement Description dataSourceJNDIName Mandatory. The JNDI name of the data source. You also have the option to externalize the data source JNDI name and make it configurable from the server configuration:
adapter.xml: <connectionPolicy xsi:type="sql:SQLConnectionPolicy"> <dataSourceJNDIName>${my-adapter-ds}</dataSourceJNDIName> </connectionPolicy> worklight.properties: my-adapter-ds=jdbc/myAdapterDSSee Configure a MobileFirst project in production using JNDI environment entries.
Parent topic: The adapter XML File