SQL Stored Procedure Component

Available as of Camel version 2.17

The sql-stored: component allows you to work with databases using JDBC Stored Procedure queries. This component is an extension to the SQL Component but specialized for calling stored procedures.

This component uses spring-jdbc behind the scenes for the actual SQL handling.

Maven users will need to add the following dependency to their pom.xml for this component:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-sql</artifactId>
    <version>x.x.x</version>
    <!-- use the same version as your Camel core version -->
</dependency>

URI format

The SQL component uses the following endpoint URI notation:

sql-stored:template[?options]

Where template is the stored procedure template, where you declare the name of the stored procedure and the IN and OUT arguments. 

You can also refer to the template in a external file on the file system or classpath such as:

sql-stored:classpath:sql/myprocedure.sql[?options]

Where sql/myprocedure.sql is a plain text file in the classpath with the template, as show:

SUBNUMBERS(
  INTEGER ${headers.num1},
  INTEGER ${headers.num2},
  OUT INTEGER resultofsub
)

You can append query options to the URI in the following format, ?option=value&option=value&…​

Options

The SQL Stored Procedure component supports 2 options which are listed below.

Name Description Default Type

dataSource (producer)

Sets the DataSource to use to communicate with the database.

DataSource

resolveProperty Placeholders (advanced)

Whether the component should resolve property placeholders on itself when starting. Only properties which are of String type can use property placeholders.

true

boolean

The SQL Stored Procedure endpoint is configured using URI syntax:

sql-stored:template

with the following path and query parameters:

Path Parameters (1 parameters):

Name Description Default Type

template

Required Sets the StoredProcedure template to perform

String

Query Parameters (7 parameters):

Name Description Default Type

batch (producer)

Enables or disables batch mode

false

boolean

dataSource (producer)

Sets the DataSource to use to communicate with the database.

DataSource

function (producer)

Whether this call is for a function.

false

boolean

noop (producer)

If set will ignore the results of the template and use the existing IN message as the OUT message for the continuation of processing

false

boolean

outputHeader (producer)

Store the template result in a header instead of the message body. By default outputHeader == null and the template result is stored in the message body any existing content in the message body is discarded. If outputHeader is set the value is used as the name of the header to store the template result and the original message body is preserved.

String

useMessageBodyForTemplate (producer)

Whether to use the message body as the template and then headers for parameters. If this option is enabled then the template in the uri is not used.

false

boolean

synchronous (advanced)

Sets whether synchronous processing should be strictly used or Camel is allowed to use asynchronous processing (if supported).

false

boolean

Declaring the stored procedure template

The template is declared using a syntax that would be similar to a Java method signature. The name of the stored procedure, and then the arguments enclosed in parenthesis. An example explains this well:

<to uri="sql-stored:SUBNUMBERS(INTEGER ${headers.num1},INTEGER ${headers.num2},OUT INTEGER resultofsub)"/>

The arguments is declared by a type and then the mapping to the Camel message using simple expression. So in this example the first two parameters are IN values that are INTEGER type, that maps to the message headers. The last parameter is the OUT value, also an INTEGER type.

In SQL term the stored procedure could be declared as:

CREATE PROCEDURE SUBNUMBERS(VALUE1 INTEGER, VALUE2 INTEGER,OUT RESULT INTEGER)

Camel Sql Starter

A starter module is available to spring-boot users. When using the starter, the DataSource can be directly configured using spring-boot properties.

# Example for a mysql datasource
spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

To use this feature, add the following dependencies to your spring boot pom.xml file:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-sql-starter</artifactId>
    <version>${camel.version}</version> <!-- use the same version as your Camel core version -->
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
    <version>${spring-boot-version}</version>
</dependency>

You should also include the specific database driver, if needed.