You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by bu...@apache.org on 2016/07/29 14:19:49 UTC

svn commit: r994050 [3/4] - in /websites/production/camel/content: book-component-appendix.html book-in-one-page.html cache/main.pageCache jdbc.html sql-component.html

Modified: websites/production/camel/content/jdbc.html
==============================================================================
--- websites/production/camel/content/jdbc.html (original)
+++ websites/production/camel/content/jdbc.html Fri Jul 29 14:19:49 2016
@@ -97,7 +97,7 @@
 </div></div><div class="confluence-information-macro confluence-information-macro-warning"><span class="aui-icon aui-icon-small aui-iconfont-error confluence-information-macro-icon"></span><div class="confluence-information-macro-body"><p>This component can only be used to define producer endpoints, which means that you cannot use the JDBC component in a <code>from()</code> statement.</p></div></div><h3 id="JDBC-URIformat">URI format</h3><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
 <script class="brush: java; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[jdbc:dataSourceName[?options]
 ]]></script>
-</div></div><p>This component only supports producer endpoints.</p><p>You can append query options to the URI in the following format, <code>?option=value&amp;option=value&amp;...</code></p><h3 id="JDBC-Options">Options</h3><div class="confluenceTableSmall"><div class="table-wrap"><table class="confluenceTable"><tbody><tr><th colspan="1" rowspan="1" class="confluenceTh"><p>Name</p></th><th colspan="1" rowspan="1" class="confluenceTh"><p>Default Value</p></th><th colspan="1" rowspan="1" class="confluenceTh"><p>Description</p></th></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p><code>readSize</code></p></td><td colspan="1" rowspan="1" class="confluenceTd"><p><code>0</code></p></td><td colspan="1" rowspan="1" class="confluenceTd"><p>The default maximum number of rows that can be read by a polling query. The default value is 0.</p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p><code>statement.&lt;xxx&gt;</code></p></td><td colspan="1" rowspan="1" class="c
 onfluenceTd"><p><code>null</code></p></td><td colspan="1" rowspan="1" class="confluenceTd"><p><strong>Camel 2.1:</strong> Sets additional options on the <code>java.sql.Statement</code> that is used behind the scenes to execute the queries. For instance, <code>statement.maxRows=10</code>. For detailed documentation, see the <a shape="rect" class="external-link" href="http://java.sun.com/j2se/1.5.0/docs/api/java/sql/Statement.html" rel="nofollow"><code>java.sql.Statement</code> javadoc</a> documentation.</p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p><code>useJDBC4ColumnNameAndLabelSemantics</code></p></td><td colspan="1" rowspan="1" class="confluenceTd"><p><code>true</code></p></td><td colspan="1" rowspan="1" class="confluenceTd"><p><strong>Camel 2.2:</strong> Sets whether to use JDBC 4/3 column label/name semantics. You can use this option to turn it <code>false</code> in case you have issues with your JDBC driver to select data. This only applies when using <c
 ode>SQL SELECT</code> using aliases (e.g. <code>SQL SELECT id as identifier, name as given_name from persons</code>).</p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p><code>resetAutoCommit</code></p></td><td colspan="1" rowspan="1" class="confluenceTd"><p><code>true</code></p></td><td colspan="1" rowspan="1" class="confluenceTd"><p><strong>Camel 2.9:</strong> If true, Camel will set the autoCommit on the JDBC connection to be false, commit the change after executing the statement and reset the autoCommit flag of the connection at the end. If the JDBC connection does not support resetting the autoCommit flag, set this to false. <br clear="none">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.</p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p><code>allowNamedParameters</code></p></td><td colspan="1" rowspan="1" class="confluenceTd"><p><code>true</code></p></
 td><td colspan="1" rowspan="1" class="confluenceTd"><p><strong>Camel 2.12:</strong> Whether to allow using named parameters in the queries.</p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p><code>prepareStatementStrategy</code></p></td><td colspan="1" rowspan="1" class="confluenceTd"><p>&#160;</p></td><td colspan="1" rowspan="1" class="confluenceTd"><p><strong>Camel 2.12:</strong> Allows to plugin to use a custom <code>org.apache.camel.component.jdbc.JdbcPrepareStatementStrategy</code> to control preparation of the query and prepared statement.</p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p><code>useHeadersAsParameters</code></p></td><td colspan="1" rowspan="1" class="confluenceTd"><p><code>false</code></p></td><td colspan="1" rowspan="1" class="confluenceTd"><p><strong>Camel 2.12:</strong> Set this option to <code>true</code> to use the <code>prepareStatementStrategy</code> with named parameters. This allows to define queries with named plac
 eholders, and use headers with the dynamic values for the query placeholders.</p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p><code>outputType</code></p></td><td colspan="1" rowspan="1" class="confluenceTd"><p><code>SelectList</code></p></td><td colspan="1" rowspan="1" class="confluenceTd"><p><strong>Camel 2.12.1:</strong> Make the output of the producer to SelectList as List of Map, or SelectOne as single Java object in the following way:<br clear="none"> a) If the query has only single column, then that JDBC Column object is returned. (such as SELECT COUNT( * ) FROM PROJECT will return a Long object.<br clear="none"> b) If the query has more than one column, then it will return a Map of that result.<br clear="none"> c) If the outputClass is set, then it will convert the query result into an Java bean object by calling all the setters that match the column names. It will assume your class has a default constructor to create instance with. From <strong>Camel 2.1
 4</strong> onwards then SelectList is also supported. <br clear="none"> d) If the query resulted in more than one rows, it throws an non-unique result exception.<br clear="none"> <strong>Camel 2.14.0:</strong> New <code>StreamList</code> output type value that streams the result of the query using an <code>Iterator&lt;Map&lt;String, Object&gt;&gt;</code>, it can be used along with the <a shape="rect" href="splitter.html">Splitter</a> EIP.</p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p><code>outputClass</code></p></td><td colspan="1" rowspan="1" class="confluenceTd"><p><code>null</code></p></td><td colspan="1" rowspan="1" class="confluenceTd"><p><strong>Camel 2.12.1:</strong> Specify the full package and class name to use as conversion when outputType=SelectOne. From <strong>Camel 2.14</strong> onwards then SelectList is also supported.</p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p><code>beanRowMapper</code></p></td><td colspan="1" rowspan
 ="1" class="confluenceTd"><p>&#160;</p></td><td colspan="1" rowspan="1" class="confluenceTd"><p><strong>Camel 2.12.1:</strong> To use a custom <code>org.apache.camel.component.jdbc.BeanRowMapper</code> when using <code>outputClass</code>. The default implementation will lower case the row names and skip underscores, and dashes. For example <code>"CUST_ID"</code> is mapped as <code>"custId"</code>.</p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><code>useGetBytesForBlob</code></td><td colspan="1" rowspan="1" class="confluenceTd"><code>false</code></td><td colspan="1" rowspan="1" class="confluenceTd"><strong>Camel 2.16:</strong> 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.</td></tr></tbody></table></div></div><h3 id="JDBC-Result">Result</h3><p>By default the result is returned in the OUT body as an <code>ArrayList&lt;HashMap&lt;String, Object&gt;&gt;</code>. Th
 e <code>List</code> object contains the list of rows and the <code>Map</code> objects contain each row with the <code>String</code> key as the column name. You can use the option <code>outputType</code> to control the result.</p><p><strong>Note:</strong> This component fetches <code>ResultSetMetaData</code> to be able to return the column name as the key in the <code>Map</code>.</p><h4 id="JDBC-MessageHeaders">Message Headers</h4><div class="table-wrap"><table class="confluenceTable"><tbody><tr><th colspan="1" rowspan="1" class="confluenceTh"><p>Header</p></th><th colspan="1" rowspan="1" class="confluenceTh"><p>Description</p></th></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p><code>CamelJdbcRowCount</code></p></td><td colspan="1" rowspan="1" class="confluenceTd"><p>If the query is a <code>SELECT</code>, query the row count is returned in this OUT header.</p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p><code>CamelJdbcUpdateCount</code></p></td><td 
 colspan="1" rowspan="1" class="confluenceTd"><p>If the query is an <code>UPDATE</code>, query the update count is returned in this OUT header.</p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p><code>CamelGeneratedKeysRows</code></p></td><td colspan="1" rowspan="1" class="confluenceTd"><p><strong>Camel 2.10:</strong> Rows that contains the generated kets.</p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p><code>CamelGeneratedKeysRowCount</code></p></td><td colspan="1" rowspan="1" class="confluenceTd"><p><strong>Camel 2.10:</strong> The number of rows in the header that contains generated keys.</p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p><code>CamelJdbcColumnNames</code></p></td><td colspan="1" rowspan="1" class="confluenceTd"><p><strong>Camel 2.11.1:</strong> The column names from the ResultSet as a <code>java.util.Set</code> type.</p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p><code>CamelJdbcParamete
 s</code></p></td><td colspan="1" rowspan="1" class="confluenceTd"><p><strong>Camel 2.12:</strong> A <code>java.util.Map</code> which has the headers to be used if <code>useHeadersAsParameters</code> has been enabled.</p></td></tr></tbody></table></div><h3 id="JDBC-Generatedkeys">Generated keys</h3><p><strong>Available as of Camel 2.10</strong></p><p>If you insert data using SQL INSERT, then the RDBMS may support auto generated keys. You can instruct the <a shape="rect" href="jdbc.html">JDBC</a> producer to return the generated keys in headers.<br clear="none"> To do that set the header <code>CamelRetrieveGeneratedKeys=true</code>. Then the generated keys will be provided as headers with the keys listed in the table above.</p><p>You can see more details in this <a shape="rect" class="external-link" href="https://svn.apache.org/repos/asf/camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcGeneratedKeysTest.java">unit test</a>.</p><div class="confluence-
 information-macro confluence-information-macro-information"><span class="aui-icon aui-icon-small aui-iconfont-info confluence-information-macro-icon"></span><div class="confluence-information-macro-body"><p>Using generated keys does not work with together with named parameters.</p></div></div><h3 id="JDBC-Usingnamedparameters">Using named parameters</h3><p><strong>Available as of Camel 2.12</strong></p><p>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.<br clear="none"> Camel will then lookup these parameters from the message headers. Notice in the example above we set two headers with constant value<br clear="none"> for the named parameters:</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+</div></div><p>This component only supports producer endpoints.</p><p>You can append query options to the URI in the following format, <code>?option=value&amp;option=value&amp;...</code></p><h3 id="JDBC-Options">Options</h3><div class="confluenceTableSmall"><div class="table-wrap"><table class="confluenceTable"><tbody><tr><th colspan="1" rowspan="1" class="confluenceTh"><p>Name</p></th><th colspan="1" rowspan="1" class="confluenceTh"><p>Default Value</p></th><th colspan="1" rowspan="1" class="confluenceTh"><p>Description</p></th></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p><code>readSize</code></p></td><td colspan="1" rowspan="1" class="confluenceTd"><p><code>0</code></p></td><td colspan="1" rowspan="1" class="confluenceTd"><p>The default maximum number of rows that can be read by a polling query. The default value is 0.</p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p><code>statement.&lt;xxx&gt;</code></p></td><td colspan="1" rowspan="1" class="c
 onfluenceTd"><p><code>null</code></p></td><td colspan="1" rowspan="1" class="confluenceTd"><p><strong>Camel 2.1:</strong> Sets additional options on the <code>java.sql.Statement</code> that is used behind the scenes to execute the queries. For instance, <code>statement.maxRows=10</code>. For detailed documentation, see the <a shape="rect" class="external-link" href="http://java.sun.com/j2se/1.5.0/docs/api/java/sql/Statement.html" rel="nofollow"><code>java.sql.Statement</code> javadoc</a> documentation.</p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p><code>useJDBC4ColumnNameAndLabelSemantics</code></p></td><td colspan="1" rowspan="1" class="confluenceTd"><p><code>true</code></p></td><td colspan="1" rowspan="1" class="confluenceTd"><p><strong>Camel 2.2:</strong> Sets whether to use JDBC 4/3 column label/name semantics. You can use this option to turn it <code>false</code> in case you have issues with your JDBC driver to select data. This only applies when using <c
 ode>SQL SELECT</code> using aliases (e.g. <code>SQL SELECT id as identifier, name as given_name from persons</code>).</p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p><code>resetAutoCommit</code></p></td><td colspan="1" rowspan="1" class="confluenceTd"><p><code>true</code></p></td><td colspan="1" rowspan="1" class="confluenceTd"><p><strong>Camel 2.9:</strong> If true, Camel will set the autoCommit on the JDBC connection to be false, commit the change after executing the statement and reset the autoCommit flag of the connection at the end. If the JDBC connection does not support resetting the autoCommit flag, set this to false. <br clear="none">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.</p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p><code>allowNamedParameters</code></p></td><td colspan="1" rowspan="1" class="confluenceTd"><p><code>true</code></p></
 td><td colspan="1" rowspan="1" class="confluenceTd"><p><strong>Camel 2.12:</strong> Whether to allow using named parameters in the queries.</p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p><code>prepareStatementStrategy</code></p></td><td colspan="1" rowspan="1" class="confluenceTd"><p>&#160;</p></td><td colspan="1" rowspan="1" class="confluenceTd"><p><strong>Camel 2.12:</strong> Allows to plugin to use a custom <code>org.apache.camel.component.jdbc.JdbcPrepareStatementStrategy</code> to control preparation of the query and prepared statement.</p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p><code>useHeadersAsParameters</code></p></td><td colspan="1" rowspan="1" class="confluenceTd"><p><code>false</code></p></td><td colspan="1" rowspan="1" class="confluenceTd"><p><strong>Camel 2.12:</strong> Set this option to <code>true</code> to use the <code>prepareStatementStrategy</code> with named parameters. This allows to define queries with named plac
 eholders, and use headers with the dynamic values for the query placeholders.</p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p><code>outputType</code></p></td><td colspan="1" rowspan="1" class="confluenceTd"><p><code>SelectList</code></p></td><td colspan="1" rowspan="1" class="confluenceTd"><p><strong>Camel 2.12.1:</strong> <span>Make the output of consumer or producer. </span><code>SelectList</code><span> will output a List of Map. </span><code>SelectOne</code><span> will output single Java object in the following way</span>:<br clear="none"> a) If the query has only single column, then that JDBC Column object is returned. (such as SELECT COUNT( * ) FROM PROJECT will return a Long object.<br clear="none"> b) If the query has more than one column, then it will return a Map of that result.<br clear="none"> c) If the outputClass is set, then it will convert the query result into an Java bean object by calling all the setters that match the column names. It will ass
 ume your class has a default constructor to create instance with. From <strong>Camel 2.14</strong> onwards then SelectList is also supported. <br clear="none"> d) If the query resulted in more than one rows, it throws an non-unique result exception.<br clear="none"> <strong>Camel 2.14.0:</strong> New <code>StreamList</code> output type value that streams the result of the query using an <code>Iterator&lt;Map&lt;String, Object&gt;&gt;</code>, it can be used along with the <a shape="rect" href="splitter.html">Splitter</a> EIP.</p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p><code>outputClass</code></p></td><td colspan="1" rowspan="1" class="confluenceTd"><p><code>null</code></p></td><td colspan="1" rowspan="1" class="confluenceTd"><p><strong>Camel 2.12.1:</strong> Specify the full package and class name to use as conversion when outputType=SelectOne. From <strong>Camel 2.14</strong> onwards then SelectList is also supported.</p></td></tr><tr><td colspan="1" rowspa
 n="1" class="confluenceTd"><p><code>beanRowMapper</code></p></td><td colspan="1" rowspan="1" class="confluenceTd"><p>&#160;</p></td><td colspan="1" rowspan="1" class="confluenceTd"><p><strong>Camel 2.12.1:</strong> To use a custom <code>org.apache.camel.component.jdbc.BeanRowMapper</code> when using <code>outputClass</code>. The default implementation will lower case the row names and skip underscores, and dashes. For example <code>"CUST_ID"</code> is mapped as <code>"custId"</code>.</p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><code>useGetBytesForBlob</code></td><td colspan="1" rowspan="1" class="confluenceTd"><code>false</code></td><td colspan="1" rowspan="1" class="confluenceTd"><strong>Camel 2.16:</strong> 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.</td></tr></tbody></table></div></div><h3 id="JDBC-Result">Result</h3><p>By default the result is return
 ed in the OUT body as an <code>ArrayList&lt;HashMap&lt;String, Object&gt;&gt;</code>. The <code>List</code> object contains the list of rows and the <code>Map</code> objects contain each row with the <code>String</code> key as the column name. You can use the option <code>outputType</code> to control the result.</p><p><strong>Note:</strong> This component fetches <code>ResultSetMetaData</code> to be able to return the column name as the key in the <code>Map</code>.</p><h4 id="JDBC-MessageHeaders">Message Headers</h4><div class="table-wrap"><table class="confluenceTable"><tbody><tr><th colspan="1" rowspan="1" class="confluenceTh"><p>Header</p></th><th colspan="1" rowspan="1" class="confluenceTh"><p>Description</p></th></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p><code>CamelJdbcRowCount</code></p></td><td colspan="1" rowspan="1" class="confluenceTd"><p>If the query is a <code>SELECT</code>, query the row count is returned in this OUT header.</p></td></tr><tr><td colspa
 n="1" rowspan="1" class="confluenceTd"><p><code>CamelJdbcUpdateCount</code></p></td><td colspan="1" rowspan="1" class="confluenceTd"><p>If the query is an <code>UPDATE</code>, query the update count is returned in this OUT header.</p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p><code>CamelGeneratedKeysRows</code></p></td><td colspan="1" rowspan="1" class="confluenceTd"><p><strong>Camel 2.10:</strong> Rows that contains the generated kets.</p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p><code>CamelGeneratedKeysRowCount</code></p></td><td colspan="1" rowspan="1" class="confluenceTd"><p><strong>Camel 2.10:</strong> The number of rows in the header that contains generated keys.</p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p><code>CamelJdbcColumnNames</code></p></td><td colspan="1" rowspan="1" class="confluenceTd"><p><strong>Camel 2.11.1:</strong> The column names from the ResultSet as a <code>java.util.Set</code> type.</p><
 /td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p><code>CamelJdbcParametes</code></p></td><td colspan="1" rowspan="1" class="confluenceTd"><p><strong>Camel 2.12:</strong> A <code>java.util.Map</code> which has the headers to be used if <code>useHeadersAsParameters</code> has been enabled.</p></td></tr></tbody></table></div><h3 id="JDBC-Generatedkeys">Generated keys</h3><p><strong>Available as of Camel 2.10</strong></p><p>If you insert data using SQL INSERT, then the RDBMS may support auto generated keys. You can instruct the <a shape="rect" href="jdbc.html">JDBC</a> producer to return the generated keys in headers.<br clear="none"> To do that set the header <code>CamelRetrieveGeneratedKeys=true</code>. Then the generated keys will be provided as headers with the keys listed in the table above.</p><p>You can see more details in this <a shape="rect" class="external-link" href="https://svn.apache.org/repos/asf/camel/trunk/components/camel-jdbc/src/test/java/org/apache/ca
 mel/component/jdbc/JdbcGeneratedKeysTest.java">unit test</a>.</p><div class="confluence-information-macro confluence-information-macro-information"><span class="aui-icon aui-icon-small aui-iconfont-info confluence-information-macro-icon"></span><div class="confluence-information-macro-body"><p>Using generated keys does not work with together with named parameters.</p></div></div><h3 id="JDBC-Usingnamedparameters">Using named parameters</h3><p><strong>Available as of Camel 2.12</strong></p><p>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.<br clear="none"> Camel will then lookup these parameters from the message headers. Notice in the example above we set two headers with constant value<br clear="none"> for the named parameters:</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
 <script class="brush: java; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[  from(&quot;direct:projects&quot;)
      .setHeader(&quot;lic&quot;, constant(&quot;ASF&quot;))
      .setHeader(&quot;min&quot;, constant(123))