Skip to content

Latest commit

 

History

History
83 lines (65 loc) · 3.67 KB

File metadata and controls

83 lines (65 loc) · 3.67 KB

Binding MySQL instances to Liberty applications

If you bind a MySQL service instance to an application, the Liberty buildpack also generates the following configuration elements in the server.xml file. The elements are required to access the database.

  • A dataSource element with the default properties subelement.
  • A jdbcDriver element.
  • A library element with an embedded fileset subelement. The library element is created for the Java Database Connectivity (JDBC) driver.
  • A featureManager element. The Liberty buildpack also adds the jdbc-4.0 feature to the featureManager element.

In addition, the Liberty buildpack provides the JDBC driver that is required.

In the dataSource element, the Liberty buildpack generates a JNDI name that is used by your application to access the data source. Because the Liberty buildpack does not have access to the JNDI name that is used by the application, the Liberty buildpack generates a JNDI name with a convention of jdbc/service_name, where service_name is the name of the bound service. For example, if you bind a MySQL service that is named mydb to the application, the Liberty buildpack generates a JNDI name of jdbc/mydb. When you develop the application and create the MySQL service instance, ensure that the JNDI name that is used by the application to access the data source is the same as the JNDI name that is generated by the Liberty buildpack.

The following example shows the configuration that is generated when an application is pushed to the cloud and is bound to a MySQL service instance that is named mydb:

<dataSource id='mysql-mydb' jdbcDriverRef='mysql-driver' jndiName='jdbc/mydb'
            transactional='true' type='javax.sql.ConnectionPoolDataSource'>
        <properties id='mysql-mydb-props' 
          databaseName='${cloud.services.mydb.connection.name}'
          user='${cloud.services.mydb.connection.user}'
          password='${cloud.services.mydb.connection.password}' 
          portNumber='${cloud.services.mydb.connection.port}'
          serverName='${cloud.services.mydb.connection.host}'/> 
</dataSource>    

<jdbcDriver id='mysql-driver' 
            javax.sql.XADataSource='org.mariadb.jdbc.MySQLDataSource'
            javax.sql.ConnectionPoolDataSource='org.mariadb.jdbc.MySQLDataSource'  
            libraryRef='mysql-library'/>  

<library id='mysql-library'>       
	    <fileset id='mysql-fileset'  dir='${server.config.dir}/lib'/>   
</library>

With this configuration, the datasource should be accessible using Java code similar to the following:

import javax.annotation.Resource
import javax.sql.DataSource

...

@Resource(name = "jdbc/mydb")
private DataSource mydb;

The configuration elements in the server.xml file use the following IDs and ID formats:

  • The dataSource element uses a configuration ID of mysql-service_name.
  • The properties element uses a configuration ID of mysql-service_name-props.
  • The jdbcDriver element uses a configuration ID of mysql-driver.
  • The library element uses a configuration ID of mysql-library.
  • The fileset element uses a configuration ID of mysql-fileset.

If the Liberty buildpack finds an existing data source configuration, it updates only the following attributes:

  • databaseName
  • user
  • password
  • serverName
  • portNumber

The Liberty buildpack does not update the jndiName attribute.

You can provide your own client driver JAR files. The client driver JAR files must be placed in the usr/servers/<servername>/lib directory. If you do not provide client driver JAR files, the Liberty buildpack provides the files for you. The client driver JAR files that you provide must use the standard names that are established by the providing vendor. You cannot rename client JAR files.