DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Kafka JDBC Source Connector for Large Data
  • Amazon Dynamo DB Connector Operations Walkthrough in Mule 4, Part 1
  • Integrating NoSQL Database With Mule 4 (OOTB Cassandra Connector)
  • The Aggregate Reference Problem

Trending

  • Run Gemma 4 on Your Laptop: A Hands-On Guide to Google's Latest Open Multimodal LLM
  • A Hands-On ABAP RESTful Programming Model Guide
  • How to Format Articles for DZone
  • A Deep Dive into Tracing Agentic Workflows (Part 1)
  1. DZone
  2. Data Engineering
  3. Databases
  4. Mule 4: Database Connector (Part 1)

Mule 4: Database Connector (Part 1)

There are tons of changes that you can witness in Mule 4 compared to Mule 3. Here's how to deal with some of the changes that can impact your database.

By 
Pryank Gupta user avatar
Pryank Gupta
·
May. 02, 18 · Tutorial
Likes (7)
Comment
Save
Tweet
Share
38.1K Views

Join the DZone community and get the full member experience.

Join For Free

Lots of people are wondering about the new features in Mule since Mule released its latest version, Mule 4. There are tons of changes that you can witness in Mule 4 compared to Mule 3. All of the Mule 3's connectors have been replaced by operation-based connectors in Mule 4.

Note: Mule 4 is the latest iteration of the Mule runtime engine. Mule 4 has a totally different approach to connect to the database and perform CRUD operation on a database.

Database Connector in Anypoint Studio 7

All of the connectors are no longer bundled with Mule runtime. We need to explicitly associate them with our application. Not every connector is available inside the palette by default. In Studio, you need to add the database connector explicitly to start working on it.

Add a Connector to Studio

  1. In Mule Palette, click Add Module > Add Extension.

  2. In Add Module to Project, select the module you want to add from the list of available modules.

  3. Click Finish and OK.

After adding the connector to Studio, the connector and its operations appear in the Mule Palette and we can start working on it.

Database Engines Supported Out-of-the-Box

The database connector in Mule 4 provides specialized support for the following databases:

  • Derby

  • MySQL

  • Oracle

  • Microsoft SQL Server

So, if we have to configure any other database apart from the above, then we can set it up using a generic JDBC configuration in Mule 4.

Connecting to a Generic Database

The generic database connector can connect to any database for which a JDBC driver is available. The generic connection type is useful in cases where:

  1. When we want to connect to a database for which MuleSoft does not provide the out-of-the-box support for the connector.

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

A generic connector configuration simply requires the driver class name and connection URL.

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

<db:generic-config name="Generic_Database_Configuration"
url="jdbc:hsqldb:hsql://localhost:9002" 
driverClassName="org.hsqldb.jdbcDriver"
doc:name="Generic Database Configuration" />

In this post, we will see a use case of the SELECT operation.

Querying a Database (SELECT Operation)

The SELECT operation is used to retrieve information from the relational database. Here, we will supply a SQL query and use DataWeave to supply the parameters to the query.

When we configure a DB operation using the database connector, there are many ways to add parameters values to the SQL statement that you execute against the database.

Approach #1

Using input parameters in the queries:

<flow name="selectParameterizedQuery">
  <db:select config-ref="Generic_Database_Configuration">
    <db:sql>SELECT * FROM EMPLOYEE WHERE name = :name</db:sql>
    <db:input-parameters>
      #[{'name' : payload}]
    </db:input-parameters>
  </db:select>
</flow>

As we see in the above example, input parameters are supplied as key-value pairs, which we can now create by embedding a DataWeave script. Those keys are used in conjunction with the semicolon character (:) to refer to a parameter value by name. This is the recommended approach for using parameters in your query.

Approach #2

The alternative is to directly write queries, but this is a very dangerous practice and can lead to many anomalies and is not recommended.

 <db:sql>SELECT * FROM EMPLOYEE WHERE name = #[payload] </db:sql>

The advantages of using input parameters to configure the WHERE clause in a SELECT statement this way are:

  1. The query becomes immune to SQL injection attacks.

  2. The connector can perform optimizations that are not possible otherwise, which improves the application’s overall performance.

In the Next Post…

With these basics in mind, in our next post, we will see the DB connector SELECT operation head-on.

Hope this helps! Keep learning!

Database Relational database Connector (mathematics)

Opinions expressed by DZone contributors are their own.

Related

  • Kafka JDBC Source Connector for Large Data
  • Amazon Dynamo DB Connector Operations Walkthrough in Mule 4, Part 1
  • Integrating NoSQL Database With Mule 4 (OOTB Cassandra Connector)
  • The Aggregate Reference Problem

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook