Chapter 55. JDBC Since Camel 1.2 Only producer is supported The JDBC component enables you to access databases through JDBC, where SQL queries (SELECT) and operations (INSERT, UPDATE, etc) are sent in the message body. This component uses the standard JDBC API. Note Prerequisites This component does not support transactions out of the box. For transactions, we recommend using the Spring JDBC Component instead. 55.1. Dependencies When using jdbc with Red Hat build of Camel Spring Boot make sure to use the following Maven dependency to have support for auto configuration: <dependency> <groupId>org.apache.camel.springboot</groupId> <artifactId>camel-jdbc-starter</artifactId> </dependency> 55.2. URI Format jdbc:dataSourceName[?options] 55.3. Configuring Options Camel components are configured on two separate levels: component level endpoint level 55.3.1. Configuring Component Options The component level is the highest level which holds general and common configurations that are inherited by the endpoints. For example, a component may have security settings, credentials for authentication, URLs for network connection. Components have pre-configured defaults that are commonly used. hence, you need to configure only a few options on a component or none at all. Configuring components can be done with the Component DSL , in a configuration file (application.properties|yaml), or directly with Java code. 55.3.2. Configuring Endpoint Options Endpoints have many options, which allows you to configure what you need the endpoint to do. The options are also categorized into whether the endpoint is used as a consumer (from) or as a producer (to) or used for both. Configuring endpoints is done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL and DataFormat DSL as a type safe way of configuring endpoints and data formats in Java. When configuring options is to use Property Placeholders , which allows to not hard-code urls, port numbers, sensitive information, and other settings. Placeholders allows to externalize the configuration from your code, and gives more flexibility and reuse. The following two sections lists all the options, firstly for the component followed by the endpoint. 55.4. Component Options The JDBC component supports 4 options, which are listed below. Name Description Default Type dataSource (producer) To use the DataSource instance instead of looking up the data source by name from the registry. DataSource lazyStartProducer (producer) Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel's routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. false boolean autowiredEnabled (adcanced) Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc. true boolean connectionStrategy (advanced) To use a custom strategy for working with connections. Do not use a custom strategy when using the spring-jdbc component because a special Spring ConnectionStrategy is used by default to support Spring Transactions. ConnectionStrategy 55.5. Endpoint Options The JDBC endpoint is configured using URI syntax: with the following path and query parameter: 55.5.1. Path Parameter (1 parameter) Name Description Default Type dataSourceName (producer) Required Name of DataSource to lookup in the Registry. If the name is dataSource or default, then Camel will attempt to lookup a default DataSource from the registry, meaning if there is a only one instance of DataSource found, then this DataSource will be used. String 55.5.2. Query Parameters (14 parameters) Name Description Default Type allowNamedParameters (producer) Whether to allow using named parameters in the queries. True Boolean outputClass (producer) Specify the full package and class name to use as conversion when outputType=SelectOne or SelectList. String outputType (producer) Determines the output the producer should use. Enum values: SelectOne SelectList StreamList SelectList JdbcOutputType parameters (producer) Optional parameters to the java.sql.Statement. For example to set maxRows, fetchSize etc. Map readSize (producer) The default maximum number of rows that can be read by a polling query. The default value is 0. int resetAutoCommit (producer) Camel will set the autoCommit on the JDBC connection to be false, commit the change after executed the statement and reset the autoCommit flag of the connection at the end, if the resetAutoCommit is true. If the JDBC connection doesn't support to reset the autoCommit flag, you can set the resetAutoCommit flag to be false, and Camel will not try to reset the autoCommit flag. When used with XA transactions you most likely need to set it to false so that the transaction manager is in charge of committing this tx. True Boolean transacted (producer) Whether transactions are in use. False Boolean useGetBytesForBlob (producer) To read BLOB columns as bytes instead of string data. This may be needed for certain databases such as Oracle where you must read BLOB columns as bytes. False Boolean useHeadersAsParameters (producer) Set this option to true to use the prepareStatementStrategy with named parameters. This allows to define queries with named placeholders, and use headers with the dynamic values for the query placeholders. False Boolean useJDBC4ColumnNameAndLabelSemantics (producer) Sets whether to use JDBC 4 or JDBC 3.0 or older semantic when retrieving column name. JDBC 4.0 uses columnLabel to get the column name where as JDBC 3.0 uses both columnName or columnLabel. Unfortunately JDBC drivers behave differently so you can use this option to work out issues around your JDBC driver if you get problem using this component This option is default true. True Boolean lazyStartProducer (producer (advanced)) Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel's routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. False Boolean beanRowMapper (advanced) To use a custom org.apache.camel.component.jdbc.BeanRowMapper when using outputClass. The default implementation will lower case the row names and skip underscores, and dashes. For example CUST_ID is mapped as custId. BeanRowMapper connectionStrategy (advanced) To use a custom strategy for working with connections. Do not use a custom strategy when using the spring-jdbc component because a special Spring ConnectionStrategy is used by default to support Spring Transactions. ConnectionStrategy prepareStatementStrategy (advanced) Allows the plugin to use a custom org.apache.camel.component.jdbc.JdbcPrepareStatementStrategy to control preparation of the query and prepared statement. JdbcPrepareStatementStrategy 55.6. Message Headers The JBBC component supports 8 message header(s), which is/are listed below: Name Description Default Type CamelJdbcUpdateCount (producer) Constant: JDBC_UPDATE_COUNT If the query is an UPDATE, query the update count is returned in this OUT header. int CamelJdbcRowCount (producer) Constant: JDBC_ROW_COUNT If the query is a SELECT, query the row count is returned in this OUT header. int CamelJdbcColumnNames (producer) Constant: JDBC_COLUMN_NAMES The column names from the ResultSet as a java.util.Set type. Set CamelJdbcParameters (producer) Constant: JDBC_PARAMETERS A java.util.Map which has the headers to be used if useHeadersAsParameters has been enabled. Map CamelRetrieveGeneratedKeys (producer) Constant: JDBC_RETRIEVE_GENERATED_KEYS Set its value to true to retrieve generated keys. False Boolean CamelGeneratedColumns (producer) Constant: JDBC_GENERATED_COLUMNS Set it to specify the expected generated columns. String[] or int[] CamelGeneratedKeysRowCount (producer) Constant: JDBC_GENERATED_KEYS_ROW_COUNT The number of rows in the header that contains generated keys. int CamelGeneratedKeysRows (producer) Constant: JDBC_GENERATED_KEYS_DATA Rows that contains the generated keys. List 55.7. Result By default the result is returned in the OUT body as an ArrayList<HashMap<String, Object>> . The List object contains the list of rows and the Map objects contain each row with the String key as the column name. You can use the option outputType to control the result. Note This component fetches ResultSetMetaData to be able to return the column name as the key in the Map . 55.8. Generated Keys If you insert data using SQL INSERT , then the RDBMS may support auto generated keys. You can instruct the JDBC producer to return the generated keys in headers. To do that set the header CamelRetrieveGeneratedKeys=true . Then the generated keys will be provided as headers with the keys listed in the table above. Note Using generated keys does not work together with named parameters. 55.9. Using named parameters In the given route below, we want to get all the projects from the projects table. Notice the SQL query has 2 named parameters, :?lic and :?min. Camel will then lookup these parameters from the message headers. Notice in the example above we set two headers with constant value for the named parameters: from("direct:projects") .setHeader(":?lic", constant("ASF")) .setHeader(":?min", constant(123)) .setBody(simple("select * from projects where license = :?lic and id > :?min order by id")) .to("jdbc:myDataSource?useHeadersAsParameters=true") You can also store the header values in a java.util.Map and store the map on the headers with the key CamelJdbcParameters . 55.10. Samples In the following example, we setup the DataSource that camel-jdbc requires. First we register our datasource in the Camel registry as testdb : EmbeddedDatabase db = new EmbeddedDatabaseBuilder() .setType(EmbeddedDatabaseType.DERBY).addScript("sql/init.sql").build(); CamelContext context = ... context.getRegistry().bind("testdb", db); Then we configure a route that routes to the JDBC component, so the SQL will be executed. Note how we refer to the testdb datasource that was bound in the step: from("direct:hello") .to("jdbc:testdb"); We create an endpoint, add the SQL query to the body of the IN message, and then send the exchange. The result of the query is returned in the OUT body: Endpoint endpoint = context.getEndpoint("direct:hello"); Exchange exchange = endpoint.createExchange(); // then we set the SQL on the in body exchange.getMessage().setBody("select * from customer order by ID"); // now we send the exchange to the endpoint, and receives the response from Camel Exchange out = template.send(endpoint, exchange); If you want to work on the rows one by one instead of the entire ResultSet at once you need to use the Splitter EIP such as: from("direct:hello") // here we split the data from the testdb into new messages one by one // so the mock endpoint will receive a message per row in the table // the StreamList option allows to stream the result of the query without creating a List of rows // and notice we also enable streaming mode on the splitter .to("jdbc:testdb?outputType=StreamList") .split(body()).streaming() .to("mock:result"); 55.11. Sample - Polling the database every minute If we want to poll a database using the JDBC component, we need to combine it with a polling scheduler such as the Timer or Quartz etc. In the following example, we retrieve data from the database every 60 seconds: from("timer://foo?period=60000") .setBody(constant("select * from customer")) .to("jdbc:testdb") .to("activemq:queue:customers"); 55.12. Sample - Move data between data sources A common use case is to query for data, process it and move it to another data source (ETL operations). In the following example, we retrieve new customer records from the source table every hour, filter/transform them and move them to a destination table: from("timer://MoveNewCustomersEveryHour?period=3600000") .setBody(constant("select * from customer where create_time > (sysdate-1/24)")) .to("jdbc:testdb") .split(body()) .process(new MyCustomerProcessor()) //filter/transform results as needed .setBody(simple("insert into processed_customer values('USD{body[ID]}','USD{body[NAME]}')")) .to("jdbc:testdb"); 55.13. Spring Boot Auto-Configuration The component supports 4 options, which are listed below. Name Description Default Type camel.component.jdbc.autowired-enabled Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatically configure JDBC data sources, JMS connection factories, AWS Clients, etc. True Boolean camel.component.jdbc.connection-strategy To use a custom strategy for working with connections. Do not use a custom strategy when using the spring-jdbc component because a special Spring ConnectionStrategy is used by default to support Spring Transactions. The option is a org.apache.camel.component.jdbc.ConnectionStrategy type. ConnectionStrategy camel.component.jdbc.enabled Whether to enable auto configuration of the jdbc component. This is enabled by default. Boolean camel.component.jdbc.lazy-start-producer Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel's routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. False Boolean

Commands:
<dependency> <groupId>org.apache.camel.springboot</groupId> <artifactId>camel-jdbc-starter</artifactId> </dependency>
jdbc:dataSourceName[?options]
jdbc:dataSourceName
from("direct:projects") .setHeader(":?lic", constant("ASF")) .setHeader(":?min", constant(123)) .setBody(simple("select * from projects where license = :?lic and id > :?min order by id")) .to("jdbc:myDataSource?useHeadersAsParameters=true")
EmbeddedDatabase db = new EmbeddedDatabaseBuilder() .setType(EmbeddedDatabaseType.DERBY).addScript("sql/init.sql").build(); CamelContext context = context.getRegistry().bind("testdb", db);
from("direct:hello") .to("jdbc:testdb");
Endpoint endpoint = context.getEndpoint("direct:hello"); Exchange exchange = endpoint.createExchange(); // then we set the SQL on the in body exchange.getMessage().setBody("select * from customer order by ID"); // now we send the exchange to the endpoint, and receives the response from Camel Exchange out = template.send(endpoint, exchange);
from("direct:hello") // here we split the data from the testdb into new messages one by one // so the mock endpoint will receive a message per row in the table // the StreamList option allows to stream the result of the query without creating a List of rows // and notice we also enable streaming mode on the splitter .to("jdbc:testdb?outputType=StreamList") .split(body()).streaming() .to("mock:result");
from("timer://foo?period=60000") .setBody(constant("select * from customer")) .to("jdbc:testdb") .to("activemq:queue:customers");
from("timer://MoveNewCustomersEveryHour?period=3600000") .setBody(constant("select * from customer where create_time > (sysdate-1/24)")) .to("jdbc:testdb") .split(body()) .process(new MyCustomerProcessor()) //filter/transform results as needed .setBody(simple("insert into processed_customer values('USD{body[ID]}','USD{body[NAME]}')")) .to("jdbc:testdb");