This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 6a5eee1e67cd CAMEL-27886: camel-spring-jdbc - leave the connection of
a Spring transaction to the transaction manager (#31327)
6a5eee1e67cd is described below
commit 6a5eee1e67cd92e9ed2851546359c5a3731504ea
Author: Chakravarthy Batna
<[email protected]>
AuthorDate: Fri Dec 4 00:52:35 2026 +0530
CAMEL-27886: camel-spring-jdbc - leave the connection of a Spring
transaction to the transaction manager (#31327)
* CAMEL-27886: camel-spring-jdbc - leave the connection of a Spring
transaction to the transaction manager
* CAMEL-27886: camel-jdbc - release the connection when
isConnectionTransactional fails
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
.../org/apache/camel/catalog/components/jdbc.json | 6 +-
.../camel/catalog/components/spring-jdbc.json | 6 +-
.../camel/catalog/docs/spring-jdbc-component.adoc | 5 +-
.../org/apache/camel/component/jdbc/jdbc.json | 6 +-
.../camel/component/jdbc/ConnectionStrategy.java | 8 +++
.../apache/camel/component/jdbc/JdbcComponent.java | 5 +-
.../apache/camel/component/jdbc/JdbcEndpoint.java | 8 ++-
.../apache/camel/component/jdbc/JdbcProducer.java | 43 +++++++-------
.../jdbc/JdbcConnectionStrategyFailureTest.java | 65 ++++++++++++++++++++++
.../camel/component/spring/jdbc/spring-jdbc.json | 6 +-
.../src/main/docs/spring-jdbc-component.adoc | 5 +-
.../spring/jdbc/SpringConnectionStrategy.java | 5 ++
.../spring/jdbc/JdbcSpringTransactionTest.java | 61 ++++++++++++++++++++
.../ROOT/pages/camel-4x-upgrade-guide-4_25.adoc | 12 ++++
.../component/dsl/JdbcComponentBuilderFactory.java | 9 +--
.../dsl/SpringJdbcComponentBuilderFactory.java | 9 +--
.../endpoint/dsl/JdbcEndpointBuilderFactory.java | 34 ++++++-----
.../dsl/SpringJdbcEndpointBuilderFactory.java | 34 ++++++-----
18 files changed, 251 insertions(+), 76 deletions(-)
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/jdbc.json
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/jdbc.json
index ee73682ba25f..735bf8baf70d 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/jdbc.json
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/jdbc.json
@@ -27,7 +27,7 @@
"dataSource": { "index": 0, "kind": "property", "displayName": "Data
Source", "group": "producer", "label": "", "required": false, "type": "object",
"javaType": "javax.sql.DataSource", "deprecated": false, "autowired": false,
"secret": false, "description": "The javax.sql.DataSource to use for all the
endpoints of this component, instead of looking up the dataSourceName of the
endpoint in the registry." },
"lazyStartProducer": { "index": 1, "kind": "property", "displayName":
"Lazy Start Producer", "group": "producer", "label": "producer", "required":
false, "type": "boolean", "javaType": "boolean", "deprecated": false,
"autowired": false, "secret": false, "defaultValue": false, "description":
"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 [...]
"autowiredEnabled": { "index": 2, "kind": "property", "displayName":
"Autowired Enabled", "group": "advanced", "label": "advanced", "required":
false, "type": "boolean", "javaType": "boolean", "deprecated": false,
"autowired": false, "secret": false, "defaultValue": true, "description":
"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 t [...]
- "connectionStrategy": { "index": 3, "kind": "property", "displayName":
"Connection Strategy", "group": "advanced", "label": "advanced", "required":
false, "type": "object", "javaType":
"org.apache.camel.component.jdbc.ConnectionStrategy", "deprecated": false,
"autowired": false, "secret": false, "description": "A custom
ConnectionStrategy for all the endpoints of this component, that gets the
java.sql.Connection from the DataSource (the default calls
DataSource.getConnection()) and t [...]
+ "connectionStrategy": { "index": 3, "kind": "property", "displayName":
"Connection Strategy", "group": "advanced", "label": "advanced", "required":
false, "type": "object", "javaType":
"org.apache.camel.component.jdbc.ConnectionStrategy", "deprecated": false,
"autowired": false, "secret": false, "description": "A custom
ConnectionStrategy for all the endpoints of this component, that gets the
java.sql.Connection from the DataSource (the default calls
DataSource.getConnection()) and t [...]
},
"headers": {
"CamelJdbcUpdateCount": { "index": 0, "kind": "header", "displayName": "",
"group": "producer", "label": "producer", "required": false, "javaType": "int",
"deprecated": false, "autowired": false, "secret": false, "description": "The
update count (Statement.getUpdateCount()) of a statement that returns no
ResultSet, such as an INSERT, UPDATE, DELETE or DDL statement; the body is then
unchanged.", "constantName":
"org.apache.camel.component.jdbc.JdbcConstants#JDBC_UPDATE_COUNT" },
@@ -50,14 +50,14 @@
"outputType": { "index": 3, "kind": "parameter", "displayName": "Output
Type", "group": "producer", "label": "", "required": false, "type": "enum",
"javaType": "org.apache.camel.component.jdbc.JdbcOutputType", "enum": [
"SelectOne", "SelectList", "StreamList" ], "deprecated": false, "autowired":
false, "secret": false, "defaultValue": "SelectList", "description": "The body
the producer returns for the ResultSet of a select. SelectList: a List with a
Map of the column names and values [...]
"parameters": { "index": 4, "kind": "parameter", "displayName":
"Parameters", "group": "producer", "label": "", "required": false, "type":
"object", "javaType": "java.util.Map<java.lang.String, java.lang.Object>",
"prefix": "statement.", "multiValue": true, "deprecated": false, "autowired":
false, "secret": false, "description": "Properties to set on each
java.sql.Statement (or PreparedStatement) before it runs, by the name of its
setter, for example statement.maxRows (Statement.setM [...]
"readSize": { "index": 5, "kind": "parameter", "displayName": "Read Size",
"group": "producer", "label": "", "required": false, "type": "integer",
"javaType": "int", "deprecated": false, "autowired": false, "secret": false,
"description": "The maximum number of rows to read from the java.sql.ResultSet
into the List body with outputType=SelectList; the rows after that are not
read. 0 means no limit. It is not used with SelectOne or StreamList, nor for
the generated keys, and it is not [...]
- "resetAutoCommit": { "index": 6, "kind": "parameter", "displayName":
"Reset Auto Commit", "group": "producer", "label": "", "required": false,
"type": "boolean", "javaType": "boolean", "deprecated": false, "autowired":
false, "secret": false, "defaultValue": true, "description": "Whether the
producer runs each statement in a transaction of its own on the
java.sql.Connection. When true, it turns off the auto-commit mode of the
connection (Connection.setAutoCommit(false)) if it is on, [...]
+ "resetAutoCommit": { "index": 6, "kind": "parameter", "displayName":
"Reset Auto Commit", "group": "producer", "label": "", "required": false,
"type": "boolean", "javaType": "boolean", "deprecated": false, "autowired":
false, "secret": false, "defaultValue": true, "description": "Whether the
producer runs each statement in a transaction of its own on the
java.sql.Connection. When true, it turns off the auto-commit mode of the
connection (Connection.setAutoCommit(false)) if it is on, [...]
"transacted": { "index": 7, "kind": "parameter", "displayName":
"Transacted", "group": "producer", "label": "", "required": false, "type":
"boolean", "javaType": "boolean", "deprecated": true, "deprecationNote": "Not
used: the producer does not read this option", "autowired": false, "secret":
false, "defaultValue": false, "description": "Has no effect. To take part in a
Spring transaction, use the spring-jdbc component. Deprecation note: Not used:
the producer does not read this option" },
"useGetBytesForBlob": { "index": 8, "kind": "parameter", "displayName":
"Use Get Bytes For Blob", "group": "producer", "label": "", "required": false,
"type": "boolean", "javaType": "boolean", "deprecated": false, "autowired":
false, "secret": false, "defaultValue": false, "description": "Whether to read
the BLOB columns with ResultSet.getBytes, as a byte[], instead of with
ResultSet.getBlob, as a java.sql.Blob, which can be read only while the
connection is open: the producer closes [...]
"useHeadersAsParameters": { "index": 9, "kind": "parameter",
"displayName": "Use Headers As Parameters", "group": "producer", "label": "",
"required": false, "type": "boolean", "javaType": "boolean", "deprecated":
false, "autowired": false, "secret": false, "defaultValue": false,
"description": "Whether to run the query as a java.sql.PreparedStatement and
set the values of its parameters from the message: the named parameters
(:?name) from the Map in the CamelJdbcParameters header, o [...]
"useJDBC4ColumnNameAndLabelSemantics": { "index": 10, "kind": "parameter",
"displayName": "Use JDBC4 Column Name And Label Semantics", "group":
"producer", "label": "", "required": false, "type": "boolean", "javaType":
"boolean", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": true, "description": "How to get the column names, which are
the keys of the row Maps and the values of the CamelJdbcColumnNames header.
When true, with ResultSetMetaData.getColumnLabe [...]
"lazyStartProducer": { "index": 11, "kind": "parameter", "displayName":
"Lazy Start Producer", "group": "producer (advanced)", "label":
"producer,advanced", "required": false, "type": "boolean", "javaType":
"boolean", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": false, "description": "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 produ [...]
"beanRowMapper": { "index": 12, "kind": "parameter", "displayName": "Bean
Row Mapper", "group": "advanced", "label": "advanced", "required": false,
"type": "object", "javaType": "org.apache.camel.component.jdbc.BeanRowMapper",
"deprecated": false, "autowired": false, "secret": false, "description": "A
custom BeanRowMapper that maps a column name to the name of the outputClass
property to set. The default lower-cases the name, drops underscores and
dashes, and upper-cases the letter a [...]
- "connectionStrategy": { "index": 13, "kind": "parameter", "displayName":
"Connection Strategy", "group": "advanced", "label": "advanced", "required":
false, "type": "object", "javaType":
"org.apache.camel.component.jdbc.ConnectionStrategy", "deprecated": false,
"autowired": false, "secret": false, "description": "A custom
ConnectionStrategy that gets the java.sql.Connection from the DataSource (the
default calls DataSource.getConnection()) and tells whether the connection
takes part [...]
+ "connectionStrategy": { "index": 13, "kind": "parameter", "displayName":
"Connection Strategy", "group": "advanced", "label": "advanced", "required":
false, "type": "object", "javaType":
"org.apache.camel.component.jdbc.ConnectionStrategy", "deprecated": false,
"autowired": false, "secret": false, "description": "A custom
ConnectionStrategy that gets the java.sql.Connection from the DataSource (the
default calls DataSource.getConnection()) and tells whether the connection
takes part [...]
"prepareStatementStrategy": { "index": 14, "kind": "parameter",
"displayName": "Prepare Statement Strategy", "group": "advanced", "label":
"advanced", "required": false, "type": "object", "javaType":
"org.apache.camel.component.jdbc.JdbcPrepareStatementStrategy", "deprecated":
false, "autowired": false, "secret": false, "description": "A custom
JdbcPrepareStatementStrategy, used with useHeadersAsParameters=true, that turns
the query into the SQL to prepare (the default replaces the n [...]
}
}
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/spring-jdbc.json
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/spring-jdbc.json
index 9ee4a3126dc5..87ea534367e1 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/spring-jdbc.json
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/spring-jdbc.json
@@ -27,7 +27,7 @@
"dataSource": { "index": 0, "kind": "property", "displayName": "Data
Source", "group": "producer", "label": "", "required": false, "type": "object",
"javaType": "javax.sql.DataSource", "deprecated": false, "autowired": false,
"secret": false, "description": "The javax.sql.DataSource to use for all the
endpoints of this component, instead of looking up the dataSourceName of the
endpoint in the registry." },
"lazyStartProducer": { "index": 1, "kind": "property", "displayName":
"Lazy Start Producer", "group": "producer", "label": "producer", "required":
false, "type": "boolean", "javaType": "boolean", "deprecated": false,
"autowired": false, "secret": false, "defaultValue": false, "description":
"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 [...]
"autowiredEnabled": { "index": 2, "kind": "property", "displayName":
"Autowired Enabled", "group": "advanced", "label": "advanced", "required":
false, "type": "boolean", "javaType": "boolean", "deprecated": false,
"autowired": false, "secret": false, "defaultValue": true, "description":
"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 t [...]
- "connectionStrategy": { "index": 3, "kind": "property", "displayName":
"Connection Strategy", "group": "advanced", "label": "advanced", "required":
false, "type": "object", "javaType":
"org.apache.camel.component.jdbc.ConnectionStrategy", "deprecated": false,
"autowired": false, "secret": false, "description": "A custom
ConnectionStrategy for all the endpoints of this component, that gets the
java.sql.Connection from the DataSource (the default calls
DataSource.getConnection()) and t [...]
+ "connectionStrategy": { "index": 3, "kind": "property", "displayName":
"Connection Strategy", "group": "advanced", "label": "advanced", "required":
false, "type": "object", "javaType":
"org.apache.camel.component.jdbc.ConnectionStrategy", "deprecated": false,
"autowired": false, "secret": false, "description": "A custom
ConnectionStrategy for all the endpoints of this component, that gets the
java.sql.Connection from the DataSource (the default calls
DataSource.getConnection()) and t [...]
},
"headers": {
"CamelJdbcUpdateCount": { "index": 0, "kind": "header", "displayName": "",
"group": "producer", "label": "producer", "required": false, "javaType": "int",
"deprecated": false, "autowired": false, "secret": false, "description": "The
update count (Statement.getUpdateCount()) of a statement that returns no
ResultSet, such as an INSERT, UPDATE, DELETE or DDL statement; the body is then
unchanged.", "constantName":
"org.apache.camel.component.jdbc.JdbcConstants#JDBC_UPDATE_COUNT" },
@@ -50,14 +50,14 @@
"outputType": { "index": 3, "kind": "parameter", "displayName": "Output
Type", "group": "producer", "label": "", "required": false, "type": "enum",
"javaType": "org.apache.camel.component.jdbc.JdbcOutputType", "enum": [
"SelectOne", "SelectList", "StreamList" ], "deprecated": false, "autowired":
false, "secret": false, "defaultValue": "SelectList", "description": "The body
the producer returns for the ResultSet of a select. SelectList: a List with a
Map of the column names and values [...]
"parameters": { "index": 4, "kind": "parameter", "displayName":
"Parameters", "group": "producer", "label": "", "required": false, "type":
"object", "javaType": "java.util.Map<java.lang.String, java.lang.Object>",
"prefix": "statement.", "multiValue": true, "deprecated": false, "autowired":
false, "secret": false, "description": "Properties to set on each
java.sql.Statement (or PreparedStatement) before it runs, by the name of its
setter, for example statement.maxRows (Statement.setM [...]
"readSize": { "index": 5, "kind": "parameter", "displayName": "Read Size",
"group": "producer", "label": "", "required": false, "type": "integer",
"javaType": "int", "deprecated": false, "autowired": false, "secret": false,
"description": "The maximum number of rows to read from the java.sql.ResultSet
into the List body with outputType=SelectList; the rows after that are not
read. 0 means no limit. It is not used with SelectOne or StreamList, nor for
the generated keys, and it is not [...]
- "resetAutoCommit": { "index": 6, "kind": "parameter", "displayName":
"Reset Auto Commit", "group": "producer", "label": "", "required": false,
"type": "boolean", "javaType": "boolean", "deprecated": false, "autowired":
false, "secret": false, "defaultValue": true, "description": "Whether the
producer runs each statement in a transaction of its own on the
java.sql.Connection. When true, it turns off the auto-commit mode of the
connection (Connection.setAutoCommit(false)) if it is on, [...]
+ "resetAutoCommit": { "index": 6, "kind": "parameter", "displayName":
"Reset Auto Commit", "group": "producer", "label": "", "required": false,
"type": "boolean", "javaType": "boolean", "deprecated": false, "autowired":
false, "secret": false, "defaultValue": true, "description": "Whether the
producer runs each statement in a transaction of its own on the
java.sql.Connection. When true, it turns off the auto-commit mode of the
connection (Connection.setAutoCommit(false)) if it is on, [...]
"transacted": { "index": 7, "kind": "parameter", "displayName":
"Transacted", "group": "producer", "label": "", "required": false, "type":
"boolean", "javaType": "boolean", "deprecated": true, "deprecationNote": "Not
used: the producer does not read this option", "autowired": false, "secret":
false, "defaultValue": false, "description": "Has no effect. To take part in a
Spring transaction, use the spring-jdbc component. Deprecation note: Not used:
the producer does not read this option" },
"useGetBytesForBlob": { "index": 8, "kind": "parameter", "displayName":
"Use Get Bytes For Blob", "group": "producer", "label": "", "required": false,
"type": "boolean", "javaType": "boolean", "deprecated": false, "autowired":
false, "secret": false, "defaultValue": false, "description": "Whether to read
the BLOB columns with ResultSet.getBytes, as a byte[], instead of with
ResultSet.getBlob, as a java.sql.Blob, which can be read only while the
connection is open: the producer closes [...]
"useHeadersAsParameters": { "index": 9, "kind": "parameter",
"displayName": "Use Headers As Parameters", "group": "producer", "label": "",
"required": false, "type": "boolean", "javaType": "boolean", "deprecated":
false, "autowired": false, "secret": false, "defaultValue": false,
"description": "Whether to run the query as a java.sql.PreparedStatement and
set the values of its parameters from the message: the named parameters
(:?name) from the Map in the CamelJdbcParameters header, o [...]
"useJDBC4ColumnNameAndLabelSemantics": { "index": 10, "kind": "parameter",
"displayName": "Use JDBC4 Column Name And Label Semantics", "group":
"producer", "label": "", "required": false, "type": "boolean", "javaType":
"boolean", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": true, "description": "How to get the column names, which are
the keys of the row Maps and the values of the CamelJdbcColumnNames header.
When true, with ResultSetMetaData.getColumnLabe [...]
"lazyStartProducer": { "index": 11, "kind": "parameter", "displayName":
"Lazy Start Producer", "group": "producer (advanced)", "label":
"producer,advanced", "required": false, "type": "boolean", "javaType":
"boolean", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": false, "description": "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 produ [...]
"beanRowMapper": { "index": 12, "kind": "parameter", "displayName": "Bean
Row Mapper", "group": "advanced", "label": "advanced", "required": false,
"type": "object", "javaType": "org.apache.camel.component.jdbc.BeanRowMapper",
"deprecated": false, "autowired": false, "secret": false, "description": "A
custom BeanRowMapper that maps a column name to the name of the outputClass
property to set. The default lower-cases the name, drops underscores and
dashes, and upper-cases the letter a [...]
- "connectionStrategy": { "index": 13, "kind": "parameter", "displayName":
"Connection Strategy", "group": "advanced", "label": "advanced", "required":
false, "type": "object", "javaType":
"org.apache.camel.component.jdbc.ConnectionStrategy", "deprecated": false,
"autowired": false, "secret": false, "description": "A custom
ConnectionStrategy that gets the java.sql.Connection from the DataSource (the
default calls DataSource.getConnection()) and tells whether the connection
takes part [...]
+ "connectionStrategy": { "index": 13, "kind": "parameter", "displayName":
"Connection Strategy", "group": "advanced", "label": "advanced", "required":
false, "type": "object", "javaType":
"org.apache.camel.component.jdbc.ConnectionStrategy", "deprecated": false,
"autowired": false, "secret": false, "description": "A custom
ConnectionStrategy that gets the java.sql.Connection from the DataSource (the
default calls DataSource.getConnection()) and tells whether the connection
takes part [...]
"prepareStatementStrategy": { "index": 14, "kind": "parameter",
"displayName": "Prepare Statement Strategy", "group": "advanced", "label":
"advanced", "required": false, "type": "object", "javaType":
"org.apache.camel.component.jdbc.JdbcPrepareStatementStrategy", "deprecated":
false, "autowired": false, "secret": false, "description": "A custom
JdbcPrepareStatementStrategy, used with useHeadersAsParameters=true, that turns
the query into the SQL to prepare (the default replaces the n [...]
}
}
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/spring-jdbc-component.adoc
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/spring-jdbc-component.adoc
index aa4cdc6867cc..a49c1f6b3767 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/spring-jdbc-component.adoc
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/spring-jdbc-component.adoc
@@ -43,8 +43,9 @@ include::partial$component-endpoint-headers.adoc[]
The message body is the SQL to run, as with the xref:jdbc-component.adoc[JDBC]
component.
The component gets its connections through Spring's `DataSourceUtils`, so it
uses the connection of a Spring-managed
-transaction. Set `resetAutoCommit` to `false` in that case: by default, the
component commits and closes the
-connection itself.
+transaction. The connection of a transaction is then committed or rolled back
by the transaction manager only:
+the component does not change its auto-commit mode, and gives it back with
`DataSourceUtils.releaseConnection`
+instead of closing it, whatever the value of `resetAutoCommit`.
[source,yaml]
----
diff --git
a/components/camel-jdbc/src/generated/resources/META-INF/org/apache/camel/component/jdbc/jdbc.json
b/components/camel-jdbc/src/generated/resources/META-INF/org/apache/camel/component/jdbc/jdbc.json
index ee73682ba25f..735bf8baf70d 100644
---
a/components/camel-jdbc/src/generated/resources/META-INF/org/apache/camel/component/jdbc/jdbc.json
+++
b/components/camel-jdbc/src/generated/resources/META-INF/org/apache/camel/component/jdbc/jdbc.json
@@ -27,7 +27,7 @@
"dataSource": { "index": 0, "kind": "property", "displayName": "Data
Source", "group": "producer", "label": "", "required": false, "type": "object",
"javaType": "javax.sql.DataSource", "deprecated": false, "autowired": false,
"secret": false, "description": "The javax.sql.DataSource to use for all the
endpoints of this component, instead of looking up the dataSourceName of the
endpoint in the registry." },
"lazyStartProducer": { "index": 1, "kind": "property", "displayName":
"Lazy Start Producer", "group": "producer", "label": "producer", "required":
false, "type": "boolean", "javaType": "boolean", "deprecated": false,
"autowired": false, "secret": false, "defaultValue": false, "description":
"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 [...]
"autowiredEnabled": { "index": 2, "kind": "property", "displayName":
"Autowired Enabled", "group": "advanced", "label": "advanced", "required":
false, "type": "boolean", "javaType": "boolean", "deprecated": false,
"autowired": false, "secret": false, "defaultValue": true, "description":
"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 t [...]
- "connectionStrategy": { "index": 3, "kind": "property", "displayName":
"Connection Strategy", "group": "advanced", "label": "advanced", "required":
false, "type": "object", "javaType":
"org.apache.camel.component.jdbc.ConnectionStrategy", "deprecated": false,
"autowired": false, "secret": false, "description": "A custom
ConnectionStrategy for all the endpoints of this component, that gets the
java.sql.Connection from the DataSource (the default calls
DataSource.getConnection()) and t [...]
+ "connectionStrategy": { "index": 3, "kind": "property", "displayName":
"Connection Strategy", "group": "advanced", "label": "advanced", "required":
false, "type": "object", "javaType":
"org.apache.camel.component.jdbc.ConnectionStrategy", "deprecated": false,
"autowired": false, "secret": false, "description": "A custom
ConnectionStrategy for all the endpoints of this component, that gets the
java.sql.Connection from the DataSource (the default calls
DataSource.getConnection()) and t [...]
},
"headers": {
"CamelJdbcUpdateCount": { "index": 0, "kind": "header", "displayName": "",
"group": "producer", "label": "producer", "required": false, "javaType": "int",
"deprecated": false, "autowired": false, "secret": false, "description": "The
update count (Statement.getUpdateCount()) of a statement that returns no
ResultSet, such as an INSERT, UPDATE, DELETE or DDL statement; the body is then
unchanged.", "constantName":
"org.apache.camel.component.jdbc.JdbcConstants#JDBC_UPDATE_COUNT" },
@@ -50,14 +50,14 @@
"outputType": { "index": 3, "kind": "parameter", "displayName": "Output
Type", "group": "producer", "label": "", "required": false, "type": "enum",
"javaType": "org.apache.camel.component.jdbc.JdbcOutputType", "enum": [
"SelectOne", "SelectList", "StreamList" ], "deprecated": false, "autowired":
false, "secret": false, "defaultValue": "SelectList", "description": "The body
the producer returns for the ResultSet of a select. SelectList: a List with a
Map of the column names and values [...]
"parameters": { "index": 4, "kind": "parameter", "displayName":
"Parameters", "group": "producer", "label": "", "required": false, "type":
"object", "javaType": "java.util.Map<java.lang.String, java.lang.Object>",
"prefix": "statement.", "multiValue": true, "deprecated": false, "autowired":
false, "secret": false, "description": "Properties to set on each
java.sql.Statement (or PreparedStatement) before it runs, by the name of its
setter, for example statement.maxRows (Statement.setM [...]
"readSize": { "index": 5, "kind": "parameter", "displayName": "Read Size",
"group": "producer", "label": "", "required": false, "type": "integer",
"javaType": "int", "deprecated": false, "autowired": false, "secret": false,
"description": "The maximum number of rows to read from the java.sql.ResultSet
into the List body with outputType=SelectList; the rows after that are not
read. 0 means no limit. It is not used with SelectOne or StreamList, nor for
the generated keys, and it is not [...]
- "resetAutoCommit": { "index": 6, "kind": "parameter", "displayName":
"Reset Auto Commit", "group": "producer", "label": "", "required": false,
"type": "boolean", "javaType": "boolean", "deprecated": false, "autowired":
false, "secret": false, "defaultValue": true, "description": "Whether the
producer runs each statement in a transaction of its own on the
java.sql.Connection. When true, it turns off the auto-commit mode of the
connection (Connection.setAutoCommit(false)) if it is on, [...]
+ "resetAutoCommit": { "index": 6, "kind": "parameter", "displayName":
"Reset Auto Commit", "group": "producer", "label": "", "required": false,
"type": "boolean", "javaType": "boolean", "deprecated": false, "autowired":
false, "secret": false, "defaultValue": true, "description": "Whether the
producer runs each statement in a transaction of its own on the
java.sql.Connection. When true, it turns off the auto-commit mode of the
connection (Connection.setAutoCommit(false)) if it is on, [...]
"transacted": { "index": 7, "kind": "parameter", "displayName":
"Transacted", "group": "producer", "label": "", "required": false, "type":
"boolean", "javaType": "boolean", "deprecated": true, "deprecationNote": "Not
used: the producer does not read this option", "autowired": false, "secret":
false, "defaultValue": false, "description": "Has no effect. To take part in a
Spring transaction, use the spring-jdbc component. Deprecation note: Not used:
the producer does not read this option" },
"useGetBytesForBlob": { "index": 8, "kind": "parameter", "displayName":
"Use Get Bytes For Blob", "group": "producer", "label": "", "required": false,
"type": "boolean", "javaType": "boolean", "deprecated": false, "autowired":
false, "secret": false, "defaultValue": false, "description": "Whether to read
the BLOB columns with ResultSet.getBytes, as a byte[], instead of with
ResultSet.getBlob, as a java.sql.Blob, which can be read only while the
connection is open: the producer closes [...]
"useHeadersAsParameters": { "index": 9, "kind": "parameter",
"displayName": "Use Headers As Parameters", "group": "producer", "label": "",
"required": false, "type": "boolean", "javaType": "boolean", "deprecated":
false, "autowired": false, "secret": false, "defaultValue": false,
"description": "Whether to run the query as a java.sql.PreparedStatement and
set the values of its parameters from the message: the named parameters
(:?name) from the Map in the CamelJdbcParameters header, o [...]
"useJDBC4ColumnNameAndLabelSemantics": { "index": 10, "kind": "parameter",
"displayName": "Use JDBC4 Column Name And Label Semantics", "group":
"producer", "label": "", "required": false, "type": "boolean", "javaType":
"boolean", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": true, "description": "How to get the column names, which are
the keys of the row Maps and the values of the CamelJdbcColumnNames header.
When true, with ResultSetMetaData.getColumnLabe [...]
"lazyStartProducer": { "index": 11, "kind": "parameter", "displayName":
"Lazy Start Producer", "group": "producer (advanced)", "label":
"producer,advanced", "required": false, "type": "boolean", "javaType":
"boolean", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": false, "description": "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 produ [...]
"beanRowMapper": { "index": 12, "kind": "parameter", "displayName": "Bean
Row Mapper", "group": "advanced", "label": "advanced", "required": false,
"type": "object", "javaType": "org.apache.camel.component.jdbc.BeanRowMapper",
"deprecated": false, "autowired": false, "secret": false, "description": "A
custom BeanRowMapper that maps a column name to the name of the outputClass
property to set. The default lower-cases the name, drops underscores and
dashes, and upper-cases the letter a [...]
- "connectionStrategy": { "index": 13, "kind": "parameter", "displayName":
"Connection Strategy", "group": "advanced", "label": "advanced", "required":
false, "type": "object", "javaType":
"org.apache.camel.component.jdbc.ConnectionStrategy", "deprecated": false,
"autowired": false, "secret": false, "description": "A custom
ConnectionStrategy that gets the java.sql.Connection from the DataSource (the
default calls DataSource.getConnection()) and tells whether the connection
takes part [...]
+ "connectionStrategy": { "index": 13, "kind": "parameter", "displayName":
"Connection Strategy", "group": "advanced", "label": "advanced", "required":
false, "type": "object", "javaType":
"org.apache.camel.component.jdbc.ConnectionStrategy", "deprecated": false,
"autowired": false, "secret": false, "description": "A custom
ConnectionStrategy that gets the java.sql.Connection from the DataSource (the
default calls DataSource.getConnection()) and tells whether the connection
takes part [...]
"prepareStatementStrategy": { "index": 14, "kind": "parameter",
"displayName": "Prepare Statement Strategy", "group": "advanced", "label":
"advanced", "required": false, "type": "object", "javaType":
"org.apache.camel.component.jdbc.JdbcPrepareStatementStrategy", "deprecated":
false, "autowired": false, "secret": false, "description": "A custom
JdbcPrepareStatementStrategy, used with useHeadersAsParameters=true, that turns
the query into the SQL to prepare (the default replaces the n [...]
}
}
diff --git
a/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/ConnectionStrategy.java
b/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/ConnectionStrategy.java
index 3ec731d361ee..4164842d09b7 100644
---
a/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/ConnectionStrategy.java
+++
b/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/ConnectionStrategy.java
@@ -29,4 +29,12 @@ public interface ConnectionStrategy {
boolean isConnectionTransactional(Connection connection, DataSource
dataSource);
+ /**
+ * Gives back a connection obtained from {@link
#getConnection(DataSource)} when the producer is done with it. The
+ * default implementation closes the connection.
+ */
+ default void releaseConnection(Connection connection, DataSource
dataSource) throws Exception {
+ connection.close();
+ }
+
}
diff --git
a/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcComponent.java
b/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcComponent.java
index a9eacf613654..467287f8fa15 100644
---
a/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcComponent.java
+++
b/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcComponent.java
@@ -44,8 +44,9 @@ public class JdbcComponent extends DefaultComponent
implements SecretRotationAwa
@Metadata(label = "advanced",
description = "A custom ConnectionStrategy for all the endpoints
of this component, that gets the"
+ " java.sql.Connection from the DataSource (the
default calls DataSource.getConnection())"
- + " and tells whether the connection takes part in
a transaction: with resetAutoCommit=false"
- + " such a connection is closed only when the
exchange is done. Do not set it with the"
+ + " and tells whether the connection takes part in
a transaction: the producer does not"
+ + " commit or roll back such a connection, and
gives it back with"
+ + " ConnectionStrategy.releaseConnection (the
default closes it). Do not set it with the"
+ " spring-jdbc component, whose strategy takes
part in Spring transactions.")
private ConnectionStrategy connectionStrategy;
diff --git
a/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcEndpoint.java
b/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcEndpoint.java
index 8449fff84b13..76ed905e0af3 100644
---
a/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcEndpoint.java
+++
b/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcEndpoint.java
@@ -88,6 +88,8 @@ public class JdbcEndpoint extends DefaultEndpoint {
+ " rollback when the exchange fails, happens when
the exchange is done. When false, the"
+ " producer neither changes the auto-commit mode
nor commits or rolls back: the auto-commit"
+ " mode of the connection, or a transaction
manager (XA or Spring), ends the transaction."
+ + " A connection that takes part in a transaction
of the connectionStrategy (such as a Spring"
+ + " transaction with the spring-jdbc component) is
always left to its transaction manager."
+ " Set it to false also for a driver that does
not support changing the auto-commit mode.")
private boolean resetAutoCommit = true;
@UriParam(prefix = "statement.", multiValue = true,
@@ -149,9 +151,9 @@ public class JdbcEndpoint extends DefaultEndpoint {
@UriParam(label = "advanced",
description = "A custom ConnectionStrategy that gets the
java.sql.Connection from the DataSource (the"
+ " default calls DataSource.getConnection()) and
tells whether the connection takes part in"
- + " a transaction: with resetAutoCommit=false such
a connection is closed only when the"
- + " exchange is done. Do not set it with the
spring-jdbc component, whose strategy takes"
- + " part in Spring transactions.")
+ + " a transaction: the producer does not commit or
roll back such a connection, and gives"
+ + " it back with
ConnectionStrategy.releaseConnection (the default closes it). Do not set it"
+ + " with the spring-jdbc component, whose strategy
takes part in Spring transactions.")
private ConnectionStrategy connectionStrategy = new
DefaultConnectionStrategy();
public JdbcEndpoint() {
diff --git
a/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcProducer.java
b/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcProducer.java
index c7ce2c1aeadc..fd8b2218c626 100644
---
a/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcProducer.java
+++
b/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcProducer.java
@@ -66,21 +66,29 @@ public class JdbcProducer extends DefaultProducer {
*/
@Override
public void process(Exchange exchange) throws Exception {
- if (getEndpoint().isResetAutoCommit()) {
- processingSqlBySettingAutoCommit(exchange);
+ String sql = exchange.getIn().getMandatoryBody(String.class);
+ Connection conn = connectionStrategy.getConnection(dataSource);
+ boolean resetAutoCommit;
+ try {
+ // the connection of a running transaction is committed or rolled
back by its transaction manager
+ resetAutoCommit = getEndpoint().isResetAutoCommit()
+ && !connectionStrategy.isConnectionTransactional(conn,
dataSource);
+ } catch (RuntimeException e) {
+ releaseQuietly(conn);
+ throw e;
+ }
+ if (resetAutoCommit) {
+ processingSqlBySettingAutoCommit(exchange, sql, conn);
} else {
- processingSqlWithoutSettingAutoCommit(exchange);
+ processingSqlWithoutSettingAutoCommit(exchange, sql, conn);
}
}
- private void processingSqlBySettingAutoCommit(Exchange exchange) throws
Exception {
- String sql = exchange.getIn().getMandatoryBody(String.class);
- Connection conn = null;
+ private void processingSqlBySettingAutoCommit(Exchange exchange, String
sql, Connection conn) throws Exception {
boolean autoCommit = false;
boolean shouldCloseResources = true;
try {
- conn = connectionStrategy.getConnection(dataSource);
autoCommit = conn.getAutoCommit();
if (autoCommit) {
conn.setAutoCommit(false);
@@ -100,7 +108,7 @@ public class JdbcProducer extends DefaultProducer {
} finally {
if (shouldCloseResources) {
resetAutoCommit(conn, autoCommit);
- closeQuietly(conn);
+ releaseQuietly(conn);
} else {
final Connection finalConn = conn;
final boolean finalAutoCommit = autoCommit;
@@ -121,7 +129,7 @@ public class JdbcProducer extends DefaultProducer {
@Override
public void onDone(Exchange exchange) {
resetAutoCommit(finalConn, finalAutoCommit);
- closeQuietly(finalConn);
+ releaseQuietly(finalConn);
}
@Override
@@ -133,23 +141,20 @@ public class JdbcProducer extends DefaultProducer {
}
}
- private void processingSqlWithoutSettingAutoCommit(Exchange exchange)
throws Exception {
- String sql = exchange.getIn().getMandatoryBody(String.class);
- Connection conn = null;
+ private void processingSqlWithoutSettingAutoCommit(Exchange exchange,
String sql, Connection conn) throws Exception {
boolean shouldCloseResources = true;
try {
- conn = connectionStrategy.getConnection(dataSource);
shouldCloseResources = createAndExecuteSqlStatement(exchange, sql,
conn);
} finally {
- if (shouldCloseResources &&
!connectionStrategy.isConnectionTransactional(conn, dataSource)) {
- closeQuietly(conn);
+ if (shouldCloseResources) {
+ releaseQuietly(conn);
} else {
final Connection finalConn = conn;
exchange.getExchangeExtension().addOnCompletion(new
SynchronizationAdapter() {
@Override
public void onDone(Exchange exchange) {
- closeQuietly(finalConn);
+ releaseQuietly(finalConn);
}
@Override
@@ -377,13 +382,13 @@ public class JdbcProducer extends DefaultProducer {
}
}
- private void closeQuietly(Connection con) {
+ private void releaseQuietly(Connection con) {
if (con != null) {
try {
- // always close, even when isClosed() returns true: a driver
closes its connection itself after a
+ // always release, even when isClosed() returns true: a driver
closes its connection itself after a
// fatal error (e.g. the database session has ended), and a
pool whose wrapper reports that state
// (e.g. Agroal) gets the connection back only from close()
- con.close();
+ connectionStrategy.releaseConnection(con, dataSource);
} catch (Exception sqle) {
LOG.debug("Error by closing connection", sqle);
}
diff --git
a/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcConnectionStrategyFailureTest.java
b/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcConnectionStrategyFailureTest.java
new file mode 100644
index 000000000000..0bcaee33595d
--- /dev/null
+++
b/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcConnectionStrategyFailureTest.java
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.jdbc;
+
+import java.sql.Connection;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import javax.sql.DataSource;
+
+import org.apache.camel.BindToRegistry;
+import org.apache.camel.Exchange;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+
+public class JdbcConnectionStrategyFailureTest extends AbstractJdbcTestSupport
{
+
+ @BindToRegistry("failingStrategy")
+ private final FailingConnectionStrategy strategy = new
FailingConnectionStrategy();
+
+ @Test
+ public void connectionIsReleasedWhenIsConnectionTransactionalFails() {
+ Exchange exchange =
template.send("jdbc:testdb?connectionStrategy=#failingStrategy",
+ e -> e.getMessage().setBody("select * from customer"));
+
+ assertInstanceOf(IllegalStateException.class, exchange.getException());
+ assertEquals(1, strategy.released.get(), "connection not released");
+ }
+
+ private static final class FailingConnectionStrategy implements
ConnectionStrategy {
+
+ private final AtomicInteger released = new AtomicInteger();
+
+ @Override
+ public Connection getConnection(DataSource dataSource) throws
Exception {
+ return dataSource.getConnection();
+ }
+
+ @Override
+ public boolean isConnectionTransactional(Connection connection,
DataSource dataSource) {
+ throw new IllegalStateException("cannot tell whether the
connection is transactional");
+ }
+
+ @Override
+ public void releaseConnection(Connection connection, DataSource
dataSource) throws Exception {
+ released.incrementAndGet();
+ connection.close();
+ }
+ }
+}
diff --git
a/components/camel-spring-parent/camel-spring-jdbc/src/generated/resources/META-INF/org/apache/camel/component/spring/jdbc/spring-jdbc.json
b/components/camel-spring-parent/camel-spring-jdbc/src/generated/resources/META-INF/org/apache/camel/component/spring/jdbc/spring-jdbc.json
index 9ee4a3126dc5..87ea534367e1 100644
---
a/components/camel-spring-parent/camel-spring-jdbc/src/generated/resources/META-INF/org/apache/camel/component/spring/jdbc/spring-jdbc.json
+++
b/components/camel-spring-parent/camel-spring-jdbc/src/generated/resources/META-INF/org/apache/camel/component/spring/jdbc/spring-jdbc.json
@@ -27,7 +27,7 @@
"dataSource": { "index": 0, "kind": "property", "displayName": "Data
Source", "group": "producer", "label": "", "required": false, "type": "object",
"javaType": "javax.sql.DataSource", "deprecated": false, "autowired": false,
"secret": false, "description": "The javax.sql.DataSource to use for all the
endpoints of this component, instead of looking up the dataSourceName of the
endpoint in the registry." },
"lazyStartProducer": { "index": 1, "kind": "property", "displayName":
"Lazy Start Producer", "group": "producer", "label": "producer", "required":
false, "type": "boolean", "javaType": "boolean", "deprecated": false,
"autowired": false, "secret": false, "defaultValue": false, "description":
"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 [...]
"autowiredEnabled": { "index": 2, "kind": "property", "displayName":
"Autowired Enabled", "group": "advanced", "label": "advanced", "required":
false, "type": "boolean", "javaType": "boolean", "deprecated": false,
"autowired": false, "secret": false, "defaultValue": true, "description":
"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 t [...]
- "connectionStrategy": { "index": 3, "kind": "property", "displayName":
"Connection Strategy", "group": "advanced", "label": "advanced", "required":
false, "type": "object", "javaType":
"org.apache.camel.component.jdbc.ConnectionStrategy", "deprecated": false,
"autowired": false, "secret": false, "description": "A custom
ConnectionStrategy for all the endpoints of this component, that gets the
java.sql.Connection from the DataSource (the default calls
DataSource.getConnection()) and t [...]
+ "connectionStrategy": { "index": 3, "kind": "property", "displayName":
"Connection Strategy", "group": "advanced", "label": "advanced", "required":
false, "type": "object", "javaType":
"org.apache.camel.component.jdbc.ConnectionStrategy", "deprecated": false,
"autowired": false, "secret": false, "description": "A custom
ConnectionStrategy for all the endpoints of this component, that gets the
java.sql.Connection from the DataSource (the default calls
DataSource.getConnection()) and t [...]
},
"headers": {
"CamelJdbcUpdateCount": { "index": 0, "kind": "header", "displayName": "",
"group": "producer", "label": "producer", "required": false, "javaType": "int",
"deprecated": false, "autowired": false, "secret": false, "description": "The
update count (Statement.getUpdateCount()) of a statement that returns no
ResultSet, such as an INSERT, UPDATE, DELETE or DDL statement; the body is then
unchanged.", "constantName":
"org.apache.camel.component.jdbc.JdbcConstants#JDBC_UPDATE_COUNT" },
@@ -50,14 +50,14 @@
"outputType": { "index": 3, "kind": "parameter", "displayName": "Output
Type", "group": "producer", "label": "", "required": false, "type": "enum",
"javaType": "org.apache.camel.component.jdbc.JdbcOutputType", "enum": [
"SelectOne", "SelectList", "StreamList" ], "deprecated": false, "autowired":
false, "secret": false, "defaultValue": "SelectList", "description": "The body
the producer returns for the ResultSet of a select. SelectList: a List with a
Map of the column names and values [...]
"parameters": { "index": 4, "kind": "parameter", "displayName":
"Parameters", "group": "producer", "label": "", "required": false, "type":
"object", "javaType": "java.util.Map<java.lang.String, java.lang.Object>",
"prefix": "statement.", "multiValue": true, "deprecated": false, "autowired":
false, "secret": false, "description": "Properties to set on each
java.sql.Statement (or PreparedStatement) before it runs, by the name of its
setter, for example statement.maxRows (Statement.setM [...]
"readSize": { "index": 5, "kind": "parameter", "displayName": "Read Size",
"group": "producer", "label": "", "required": false, "type": "integer",
"javaType": "int", "deprecated": false, "autowired": false, "secret": false,
"description": "The maximum number of rows to read from the java.sql.ResultSet
into the List body with outputType=SelectList; the rows after that are not
read. 0 means no limit. It is not used with SelectOne or StreamList, nor for
the generated keys, and it is not [...]
- "resetAutoCommit": { "index": 6, "kind": "parameter", "displayName":
"Reset Auto Commit", "group": "producer", "label": "", "required": false,
"type": "boolean", "javaType": "boolean", "deprecated": false, "autowired":
false, "secret": false, "defaultValue": true, "description": "Whether the
producer runs each statement in a transaction of its own on the
java.sql.Connection. When true, it turns off the auto-commit mode of the
connection (Connection.setAutoCommit(false)) if it is on, [...]
+ "resetAutoCommit": { "index": 6, "kind": "parameter", "displayName":
"Reset Auto Commit", "group": "producer", "label": "", "required": false,
"type": "boolean", "javaType": "boolean", "deprecated": false, "autowired":
false, "secret": false, "defaultValue": true, "description": "Whether the
producer runs each statement in a transaction of its own on the
java.sql.Connection. When true, it turns off the auto-commit mode of the
connection (Connection.setAutoCommit(false)) if it is on, [...]
"transacted": { "index": 7, "kind": "parameter", "displayName":
"Transacted", "group": "producer", "label": "", "required": false, "type":
"boolean", "javaType": "boolean", "deprecated": true, "deprecationNote": "Not
used: the producer does not read this option", "autowired": false, "secret":
false, "defaultValue": false, "description": "Has no effect. To take part in a
Spring transaction, use the spring-jdbc component. Deprecation note: Not used:
the producer does not read this option" },
"useGetBytesForBlob": { "index": 8, "kind": "parameter", "displayName":
"Use Get Bytes For Blob", "group": "producer", "label": "", "required": false,
"type": "boolean", "javaType": "boolean", "deprecated": false, "autowired":
false, "secret": false, "defaultValue": false, "description": "Whether to read
the BLOB columns with ResultSet.getBytes, as a byte[], instead of with
ResultSet.getBlob, as a java.sql.Blob, which can be read only while the
connection is open: the producer closes [...]
"useHeadersAsParameters": { "index": 9, "kind": "parameter",
"displayName": "Use Headers As Parameters", "group": "producer", "label": "",
"required": false, "type": "boolean", "javaType": "boolean", "deprecated":
false, "autowired": false, "secret": false, "defaultValue": false,
"description": "Whether to run the query as a java.sql.PreparedStatement and
set the values of its parameters from the message: the named parameters
(:?name) from the Map in the CamelJdbcParameters header, o [...]
"useJDBC4ColumnNameAndLabelSemantics": { "index": 10, "kind": "parameter",
"displayName": "Use JDBC4 Column Name And Label Semantics", "group":
"producer", "label": "", "required": false, "type": "boolean", "javaType":
"boolean", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": true, "description": "How to get the column names, which are
the keys of the row Maps and the values of the CamelJdbcColumnNames header.
When true, with ResultSetMetaData.getColumnLabe [...]
"lazyStartProducer": { "index": 11, "kind": "parameter", "displayName":
"Lazy Start Producer", "group": "producer (advanced)", "label":
"producer,advanced", "required": false, "type": "boolean", "javaType":
"boolean", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": false, "description": "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 produ [...]
"beanRowMapper": { "index": 12, "kind": "parameter", "displayName": "Bean
Row Mapper", "group": "advanced", "label": "advanced", "required": false,
"type": "object", "javaType": "org.apache.camel.component.jdbc.BeanRowMapper",
"deprecated": false, "autowired": false, "secret": false, "description": "A
custom BeanRowMapper that maps a column name to the name of the outputClass
property to set. The default lower-cases the name, drops underscores and
dashes, and upper-cases the letter a [...]
- "connectionStrategy": { "index": 13, "kind": "parameter", "displayName":
"Connection Strategy", "group": "advanced", "label": "advanced", "required":
false, "type": "object", "javaType":
"org.apache.camel.component.jdbc.ConnectionStrategy", "deprecated": false,
"autowired": false, "secret": false, "description": "A custom
ConnectionStrategy that gets the java.sql.Connection from the DataSource (the
default calls DataSource.getConnection()) and tells whether the connection
takes part [...]
+ "connectionStrategy": { "index": 13, "kind": "parameter", "displayName":
"Connection Strategy", "group": "advanced", "label": "advanced", "required":
false, "type": "object", "javaType":
"org.apache.camel.component.jdbc.ConnectionStrategy", "deprecated": false,
"autowired": false, "secret": false, "description": "A custom
ConnectionStrategy that gets the java.sql.Connection from the DataSource (the
default calls DataSource.getConnection()) and tells whether the connection
takes part [...]
"prepareStatementStrategy": { "index": 14, "kind": "parameter",
"displayName": "Prepare Statement Strategy", "group": "advanced", "label":
"advanced", "required": false, "type": "object", "javaType":
"org.apache.camel.component.jdbc.JdbcPrepareStatementStrategy", "deprecated":
false, "autowired": false, "secret": false, "description": "A custom
JdbcPrepareStatementStrategy, used with useHeadersAsParameters=true, that turns
the query into the SQL to prepare (the default replaces the n [...]
}
}
diff --git
a/components/camel-spring-parent/camel-spring-jdbc/src/main/docs/spring-jdbc-component.adoc
b/components/camel-spring-parent/camel-spring-jdbc/src/main/docs/spring-jdbc-component.adoc
index aa4cdc6867cc..a49c1f6b3767 100644
---
a/components/camel-spring-parent/camel-spring-jdbc/src/main/docs/spring-jdbc-component.adoc
+++
b/components/camel-spring-parent/camel-spring-jdbc/src/main/docs/spring-jdbc-component.adoc
@@ -43,8 +43,9 @@ include::partial$component-endpoint-headers.adoc[]
The message body is the SQL to run, as with the xref:jdbc-component.adoc[JDBC]
component.
The component gets its connections through Spring's `DataSourceUtils`, so it
uses the connection of a Spring-managed
-transaction. Set `resetAutoCommit` to `false` in that case: by default, the
component commits and closes the
-connection itself.
+transaction. The connection of a transaction is then committed or rolled back
by the transaction manager only:
+the component does not change its auto-commit mode, and gives it back with
`DataSourceUtils.releaseConnection`
+instead of closing it, whatever the value of `resetAutoCommit`.
[source,yaml]
----
diff --git
a/components/camel-spring-parent/camel-spring-jdbc/src/main/java/org/apache/camel/component/spring/jdbc/SpringConnectionStrategy.java
b/components/camel-spring-parent/camel-spring-jdbc/src/main/java/org/apache/camel/component/spring/jdbc/SpringConnectionStrategy.java
index 9f2764ca91ae..82b8464806a0 100644
---
a/components/camel-spring-parent/camel-spring-jdbc/src/main/java/org/apache/camel/component/spring/jdbc/SpringConnectionStrategy.java
+++
b/components/camel-spring-parent/camel-spring-jdbc/src/main/java/org/apache/camel/component/spring/jdbc/SpringConnectionStrategy.java
@@ -34,4 +34,9 @@ public class SpringConnectionStrategy implements
ConnectionStrategy {
public boolean isConnectionTransactional(Connection connection, DataSource
dataSource) {
return DataSourceUtils.isConnectionTransactional(connection,
dataSource);
}
+
+ @Override
+ public void releaseConnection(Connection connection, DataSource
dataSource) {
+ DataSourceUtils.releaseConnection(connection, dataSource);
+ }
}
diff --git
a/components/camel-spring-parent/camel-spring-jdbc/src/test/java/org/apache/camel/component/spring/jdbc/JdbcSpringTransactionTest.java
b/components/camel-spring-parent/camel-spring-jdbc/src/test/java/org/apache/camel/component/spring/jdbc/JdbcSpringTransactionTest.java
new file mode 100644
index 000000000000..cfa9bfa1e7a8
--- /dev/null
+++
b/components/camel-spring-parent/camel-spring-jdbc/src/test/java/org/apache/camel/component/spring/jdbc/JdbcSpringTransactionTest.java
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.spring.jdbc;
+
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.jdbc.datasource.DataSourceTransactionManager;
+import org.springframework.transaction.support.TransactionTemplate;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class JdbcSpringTransactionTest extends AbstractJdbcTestSupport {
+
+ @ParameterizedTest
+ @ValueSource(booleans = { true, false })
+ public void testRollback(boolean resetAutoCommit) {
+ String uri = "spring-jdbc:testdb?resetAutoCommit=" + resetAutoCommit;
+ TransactionTemplate tx = new TransactionTemplate(new
DataSourceTransactionManager(db));
+
+ tx.executeWithoutResult(status -> {
+ template.sendBody(uri, "insert into customer
values('cust4','rollback')");
+ status.setRollbackOnly();
+ });
+
+ assertEquals(0, countCustomers("cust4"));
+ }
+
+ @ParameterizedTest
+ @ValueSource(booleans = { true, false })
+ public void testCommit(boolean resetAutoCommit) {
+ String uri = "spring-jdbc:testdb?resetAutoCommit=" + resetAutoCommit;
+ TransactionTemplate tx = new TransactionTemplate(new
DataSourceTransactionManager(db));
+
+ tx.executeWithoutResult(status -> {
+ template.sendBody(uri, "insert into customer
values('cust5','commit')");
+ template.sendBody(uri, "insert into customer
values('cust6','commit')");
+ });
+
+ assertEquals(1, countCustomers("cust5"));
+ assertEquals(1, countCustomers("cust6"));
+ }
+
+ private int countCustomers(String id) {
+ return new JdbcTemplate(db).queryForObject("select count(*) from
customer where id = ?", Integer.class, id);
+ }
+}
diff --git
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_25.adoc
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_25.adoc
index f95b8bc9d366..7c3f1cb32b94 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_25.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_25.adoc
@@ -453,6 +453,18 @@ behaviour.
selected Redis database, including data that does not belong to the
repository. The `flushOnStartup` option is
unchanged: it still deletes all keys of the database when the repository
starts.
+=== camel-spring-jdbc - the connection of a Spring transaction is left to the
transaction manager
+
+Inside a Spring transaction, the `spring-jdbc` producer used the connection of
the transaction, but with the default
+`resetAutoCommit=true` it committed after each statement and then closed the
connection, so the work could no longer
+be rolled back and the transaction manager failed on the closed connection.
With `resetAutoCommit=false` it also
+closed the connection when the exchange was done. The producer now leaves such
a connection to the transaction
+manager, whatever the value of `resetAutoCommit`, and gives it back with
`DataSourceUtils.releaseConnection`.
+Setting `resetAutoCommit=false` as a workaround is no longer needed.
+
+`org.apache.camel.component.jdbc.ConnectionStrategy` has a new default method
`releaseConnection(Connection,
+DataSource)`, which the producer calls instead of `Connection.close()`.
+
=== camel-mail - To default derived from username now only applied when no
recipient is configured
When no explicit `to`, `cc` or `bcc` recipient is configured on a mail
producer endpoint, the `username` option
diff --git
a/dsl/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/JdbcComponentBuilderFactory.java
b/dsl/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/JdbcComponentBuilderFactory.java
index 9627d62762bb..f59cc9477fe5 100644
---
a/dsl/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/JdbcComponentBuilderFactory.java
+++
b/dsl/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/JdbcComponentBuilderFactory.java
@@ -120,10 +120,11 @@ public interface JdbcComponentBuilderFactory {
* A custom ConnectionStrategy for all the endpoints of this component,
* that gets the java.sql.Connection from the DataSource (the default
* calls DataSource.getConnection()) and tells whether the connection
- * takes part in a transaction: with resetAutoCommit=false such a
- * connection is closed only when the exchange is done. Do not set it
- * with the spring-jdbc component, whose strategy takes part in Spring
- * transactions.
+ * takes part in a transaction: the producer does not commit or roll
+ * back such a connection, and gives it back with
+ * ConnectionStrategy.releaseConnection (the default closes it). Do not
+ * set it with the spring-jdbc component, whose strategy takes part in
+ * Spring transactions.
*
* The option is a:
*
<code>org.apache.camel.component.jdbc.ConnectionStrategy</code>
type.
diff --git
a/dsl/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/SpringJdbcComponentBuilderFactory.java
b/dsl/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/SpringJdbcComponentBuilderFactory.java
index 1dba913acd3c..a50863c0359a 100644
---
a/dsl/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/SpringJdbcComponentBuilderFactory.java
+++
b/dsl/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/SpringJdbcComponentBuilderFactory.java
@@ -122,10 +122,11 @@ public interface SpringJdbcComponentBuilderFactory {
* A custom ConnectionStrategy for all the endpoints of this component,
* that gets the java.sql.Connection from the DataSource (the default
* calls DataSource.getConnection()) and tells whether the connection
- * takes part in a transaction: with resetAutoCommit=false such a
- * connection is closed only when the exchange is done. Do not set it
- * with the spring-jdbc component, whose strategy takes part in Spring
- * transactions.
+ * takes part in a transaction: the producer does not commit or roll
+ * back such a connection, and gives it back with
+ * ConnectionStrategy.releaseConnection (the default closes it). Do not
+ * set it with the spring-jdbc component, whose strategy takes part in
+ * Spring transactions.
*
* The option is a:
*
<code>org.apache.camel.component.jdbc.ConnectionStrategy</code>
type.
diff --git
a/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/JdbcEndpointBuilderFactory.java
b/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/JdbcEndpointBuilderFactory.java
index 9a5e00adbd25..1571c62fde17 100644
---
a/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/JdbcEndpointBuilderFactory.java
+++
b/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/JdbcEndpointBuilderFactory.java
@@ -246,9 +246,11 @@ public interface JdbcEndpointBuilderFactory {
* or the rollback when the exchange fails, happens when the exchange
is
* done. When false, the producer neither changes the auto-commit mode
* nor commits or rolls back: the auto-commit mode of the connection,
or
- * a transaction manager (XA or Spring), ends the transaction. Set it
to
- * false also for a driver that does not support changing the
- * auto-commit mode.
+ * a transaction manager (XA or Spring), ends the transaction. A
+ * connection that takes part in a transaction of the
connectionStrategy
+ * (such as a Spring transaction with the spring-jdbc component) is
+ * always left to its transaction manager. Set it to false also for a
+ * driver that does not support changing the auto-commit mode.
*
* The option is a: <code>boolean</code> type.
*
@@ -272,9 +274,11 @@ public interface JdbcEndpointBuilderFactory {
* or the rollback when the exchange fails, happens when the exchange
is
* done. When false, the producer neither changes the auto-commit mode
* nor commits or rolls back: the auto-commit mode of the connection,
or
- * a transaction manager (XA or Spring), ends the transaction. Set it
to
- * false also for a driver that does not support changing the
- * auto-commit mode.
+ * a transaction manager (XA or Spring), ends the transaction. A
+ * connection that takes part in a transaction of the
connectionStrategy
+ * (such as a Spring transaction with the spring-jdbc component) is
+ * always left to its transaction manager. Set it to false also for a
+ * driver that does not support changing the auto-commit mode.
*
* The option will be converted to a <code>boolean</code> type.
*
@@ -541,10 +545,11 @@ public interface JdbcEndpointBuilderFactory {
/**
* A custom ConnectionStrategy that gets the java.sql.Connection from
* the DataSource (the default calls DataSource.getConnection()) and
- * tells whether the connection takes part in a transaction: with
- * resetAutoCommit=false such a connection is closed only when the
- * exchange is done. Do not set it with the spring-jdbc component,
whose
- * strategy takes part in Spring transactions.
+ * tells whether the connection takes part in a transaction: the
+ * producer does not commit or roll back such a connection, and gives
it
+ * back with ConnectionStrategy.releaseConnection (the default closes
+ * it). Do not set it with the spring-jdbc component, whose strategy
+ * takes part in Spring transactions.
*
* The option is a:
* <code>org.apache.camel.component.jdbc.ConnectionStrategy</code>
type.
@@ -561,10 +566,11 @@ public interface JdbcEndpointBuilderFactory {
/**
* A custom ConnectionStrategy that gets the java.sql.Connection from
* the DataSource (the default calls DataSource.getConnection()) and
- * tells whether the connection takes part in a transaction: with
- * resetAutoCommit=false such a connection is closed only when the
- * exchange is done. Do not set it with the spring-jdbc component,
whose
- * strategy takes part in Spring transactions.
+ * tells whether the connection takes part in a transaction: the
+ * producer does not commit or roll back such a connection, and gives
it
+ * back with ConnectionStrategy.releaseConnection (the default closes
+ * it). Do not set it with the spring-jdbc component, whose strategy
+ * takes part in Spring transactions.
*
* The option will be converted to a
* <code>org.apache.camel.component.jdbc.ConnectionStrategy</code>
type.
diff --git
a/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/SpringJdbcEndpointBuilderFactory.java
b/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/SpringJdbcEndpointBuilderFactory.java
index cb69d7bdfa5b..a163be3fafe4 100644
---
a/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/SpringJdbcEndpointBuilderFactory.java
+++
b/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/SpringJdbcEndpointBuilderFactory.java
@@ -247,9 +247,11 @@ public interface SpringJdbcEndpointBuilderFactory {
* or the rollback when the exchange fails, happens when the exchange
is
* done. When false, the producer neither changes the auto-commit mode
* nor commits or rolls back: the auto-commit mode of the connection,
or
- * a transaction manager (XA or Spring), ends the transaction. Set it
to
- * false also for a driver that does not support changing the
- * auto-commit mode.
+ * a transaction manager (XA or Spring), ends the transaction. A
+ * connection that takes part in a transaction of the
connectionStrategy
+ * (such as a Spring transaction with the spring-jdbc component) is
+ * always left to its transaction manager. Set it to false also for a
+ * driver that does not support changing the auto-commit mode.
*
* The option is a: <code>boolean</code> type.
*
@@ -273,9 +275,11 @@ public interface SpringJdbcEndpointBuilderFactory {
* or the rollback when the exchange fails, happens when the exchange
is
* done. When false, the producer neither changes the auto-commit mode
* nor commits or rolls back: the auto-commit mode of the connection,
or
- * a transaction manager (XA or Spring), ends the transaction. Set it
to
- * false also for a driver that does not support changing the
- * auto-commit mode.
+ * a transaction manager (XA or Spring), ends the transaction. A
+ * connection that takes part in a transaction of the
connectionStrategy
+ * (such as a Spring transaction with the spring-jdbc component) is
+ * always left to its transaction manager. Set it to false also for a
+ * driver that does not support changing the auto-commit mode.
*
* The option will be converted to a <code>boolean</code> type.
*
@@ -542,10 +546,11 @@ public interface SpringJdbcEndpointBuilderFactory {
/**
* A custom ConnectionStrategy that gets the java.sql.Connection from
* the DataSource (the default calls DataSource.getConnection()) and
- * tells whether the connection takes part in a transaction: with
- * resetAutoCommit=false such a connection is closed only when the
- * exchange is done. Do not set it with the spring-jdbc component,
whose
- * strategy takes part in Spring transactions.
+ * tells whether the connection takes part in a transaction: the
+ * producer does not commit or roll back such a connection, and gives
it
+ * back with ConnectionStrategy.releaseConnection (the default closes
+ * it). Do not set it with the spring-jdbc component, whose strategy
+ * takes part in Spring transactions.
*
* The option is a:
* <code>org.apache.camel.component.jdbc.ConnectionStrategy</code>
type.
@@ -562,10 +567,11 @@ public interface SpringJdbcEndpointBuilderFactory {
/**
* A custom ConnectionStrategy that gets the java.sql.Connection from
* the DataSource (the default calls DataSource.getConnection()) and
- * tells whether the connection takes part in a transaction: with
- * resetAutoCommit=false such a connection is closed only when the
- * exchange is done. Do not set it with the spring-jdbc component,
whose
- * strategy takes part in Spring transactions.
+ * tells whether the connection takes part in a transaction: the
+ * producer does not commit or roll back such a connection, and gives
it
+ * back with ConnectionStrategy.releaseConnection (the default closes
+ * it). Do not set it with the spring-jdbc component, whose strategy
+ * takes part in Spring transactions.
*
* The option will be converted to a
* <code>org.apache.camel.component.jdbc.ConnectionStrategy</code>
type.