Contact Free trial Login

Configure a Database Connection - Mule 4

The Database connector can connect to any database for which a JDBC driver is available. To make it easier to connect to the most popular databases, the connector includes a series of different connection types.

Some of the following examples show configurations for common connection types such as MySQL, Oracle, and Microsoft SQL Server. Others of the following illustrate more advanced use cases, such as connecting to a global data source, configuring a JDBC driver, and configuring connection pooling.

Connect to MySQL

<db:config name="dbConfig">

Connect to Oracle

<db:config name="dbConfig">
  port="1521" instance="xe"
  password="oracle" />
</db :config >

Connect to Microsoft SQL Server

<db:config name="dbConfig">
    databaseName="microsoftDB" />

Connect to a Generic Database

The generic connection type is useful in these cases:

  • When you want to connect to a database for which MuleSoft does not provide a specific connection type.

  • When you want to connect to one of the supported databases, but you are using custom options that not included in the connection types.

A generic connection simply requires:

  • Driver class name

  • Connection URL

The following example shows how to connect to an H2 database:

<db:config name="dbConfig">
    <db:generic-connection driverClassName="org.h2.Driver"

Connect to a Global DataSource

When you set up a generic connection, the connector will use the information you provide to generate a JDBC DataSource. In some cases, you might want to create your own DataSource. To do this, the database configuration supports a data-source-connection element that allows you to reference a DataSource that is defined through the Spring module.

To connect to a global DataSource:

  1. Create a Spring configuration file containing a Bean such as this one:

    <bean id="jdbcDataSource" class="org.enhydra.jdbc.standard.StandardDataSource"
       <property name="driverName" value="org.apache.derby.jdbc.EmbeddedDriver"/>
       <property name="url" value="jdbc:derby:muleEmbeddedDB;create=true"/>

    The example above creates a custom enhydra DataSource that connects to a Derby database.

  2. Reference your Spring configuration file using the Spring module, and use the DataSource in a connection:

    <spring:config name="datasource" files="datasource-config.xml" />
    <db:config name="dbConfig">
       <db:data-source-connection dataSourceRef="jdbcDataSource" />

Set the JDBC Driver

Now that you know how to configure the connections, you can supply the JDBC driver. The example below shows how to supply a driver for the MySQL database.

Note that the steps are the same for any database. The only difference is the need for the driver dependency.

  1. Add the driver as a dependency in your project’s pom.xml file:

  2. Through your pom.xml, make Mule Runtime expose the driver dependency to the Database connector, for example:

            <!-- Only used to declare the shared libraries-->

Configure Connection Pools

Establishing connections is costly in terms of connector and RDBMS network and processing overhead. The solution is to build a connection pool that provides a number of available connections. You use the <db:pooling-profile> element, rather than the standard Mule <pooling-profile> element, for this purpose.

This example shows how to use the db:pooling-profile element:

<db:config name="dbConfig">
    <db:my-sql-connection database="mozart_test" host="${host}" password="${password}" port="${port}" user="${user}">
        <db:pooling-profile acquireIncrement="1" maxPoolSize="5" maxWait="0" maxWaitUnit="SECONDS" minPoolSize="0" preparedStatementCacheSize="5"/>

All connection configuration elements, except the global data source reference, accept the pooling profile. In JDBC, pooling occurs at the data source level. To do pooling, you have to configure the global data source. The software cannot add it on the fly.

Notice that unlike other connectors such as FTP or SFTP, the <db:pooling-profile> element is used instead of the standard <pooling-profile>. This is because databases have special pooling attributes, such as preparedStatementCacheSize, which do not make sense on the generic element.

For more details on parameters and capabilities of the connection types, see the Database Connector Technical Reference.

Create an Oracle Database Connection with TNS Names

To create an Oracle connection that relies on the tnsnames.ora file, you can use the Database connector and provide configuration details in the connector configuration. For this to work you have to add the ojdbc6.jar as a dependency in your project build path.

Note: If a firewall is between your Mule instance and the database itself, use the ENABLE=BROKEN parameter to enable a keep-alive at the TNS connection. If not set, the connection may drop traffic due to expiring inactivity timers.

For example, you can configure a Database URL in the Database connector’s General tab in Anypoint Studio, and supply this URL:


Was this article helpful?

💙 Thanks for your feedback!

Edit on GitHub