diff --git a/spring-integration-jdbc/pom.xml b/spring-integration-jdbc/pom.xml
index b8a8eadfc1..64de7a0f75 100644
--- a/spring-integration-jdbc/pom.xml
+++ b/spring-integration-jdbc/pom.xml
@@ -79,6 +79,12 @@
10.5.3.0_1
test
+
+ com.h2database
+ h2
+ 1.2.125
+ test
+
@@ -97,8 +103,7 @@
-
+
@@ -111,8 +116,7 @@
-
+
@@ -125,8 +129,7 @@
-
+
@@ -139,8 +142,7 @@
-
+
@@ -153,8 +155,7 @@
-
+
@@ -167,8 +168,7 @@
-
+
@@ -181,8 +181,7 @@
-
+
@@ -195,8 +194,7 @@
-
+
@@ -209,8 +207,7 @@
-
+
@@ -262,7 +259,9 @@
repository.objectstyle
ObjectStyle.org Repository
http://objectstyle.org/maven2/
- false
+
+ false
+
diff --git a/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/ExpressionEvaluatingSqlParameterSourceFactory.java b/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/ExpressionEvaluatingSqlParameterSourceFactory.java
index 3ad92ddd5e..8a76174132 100644
--- a/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/ExpressionEvaluatingSqlParameterSourceFactory.java
+++ b/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/ExpressionEvaluatingSqlParameterSourceFactory.java
@@ -28,12 +28,13 @@ import org.springframework.jdbc.core.namedparam.SqlParameterSource;
/**
* An implementation of {@link SqlParameterSourceFactory} which creates an {@link SqlParameterSource} that evaluates
- * Spring EL expressions. In addition the user can supply static parameters that always take precedence.
+ * Spring EL expressions. In addition the user can supply static parameters that always take precedence.
*
* @author Dave Syer
* @since 2.0
*/
-public class ExpressionEvaluatingSqlParameterSourceFactory extends AbstractExpressionEvaluator implements SqlParameterSourceFactory {
+public class ExpressionEvaluatingSqlParameterSourceFactory extends AbstractExpressionEvaluator implements
+ SqlParameterSourceFactory {
private final static Log logger = LogFactory.getLog(ExpressionEvaluatingSqlParameterSourceFactory.class);
@@ -77,20 +78,24 @@ public class ExpressionEvaluatingSqlParameterSourceFactory extends AbstractExpre
}
String expression = paramName;
if (input instanceof Collection>) {
- expression = "#root.!["+paramName+"]";
+ expression = "#root.![" + paramName + "]";
}
Object value = evaluateExpression(expression, input);
values.put(paramName, value);
+ if (logger.isDebugEnabled()) {
+ logger.debug("Resolved expression " + expression + " to " + value);
+ }
return value;
}
public boolean hasValue(String paramName) {
try {
Object value = getValue(paramName);
- if (value==ERROR) {
- return false;
+ if (value == ERROR) {
+ return false;
}
- } catch (ExpressionException e) {
+ }
+ catch (ExpressionException e) {
if (logger.isDebugEnabled()) {
logger.debug("Could not evaluate expression", e);
}
diff --git a/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/JdbcMessageHandler.java b/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/JdbcMessageHandler.java
index 9176a9a265..0e56d96c28 100644
--- a/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/JdbcMessageHandler.java
+++ b/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/JdbcMessageHandler.java
@@ -13,6 +13,10 @@
package org.springframework.integration.jdbc;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
import javax.sql.DataSource;
import org.springframework.integration.Message;
@@ -21,9 +25,13 @@ import org.springframework.integration.MessageHandlingException;
import org.springframework.integration.MessageRejectedException;
import org.springframework.integration.handler.AbstractMessageHandler;
import org.springframework.jdbc.core.JdbcOperations;
+import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcOperations;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
+import org.springframework.jdbc.support.GeneratedKeyHolder;
+import org.springframework.jdbc.support.KeyHolder;
+import org.springframework.util.LinkedCaseInsensitiveMap;
/**
* A message handler that executes an SQL update. Dynamic query parameters are supported through the
@@ -48,6 +56,8 @@ public class JdbcMessageHandler extends AbstractMessageHandler {
private volatile SqlParameterSourceFactory sqlParameterSourceFactory = new BeanPropertySqlParameterSourceFactory();
+ private boolean keysGenerated;
+
/**
* Constructor taking {@link DataSource} from which the DB Connection can be obtained and the select query to
* execute to retrieve new rows.
@@ -72,6 +82,14 @@ public class JdbcMessageHandler extends AbstractMessageHandler {
this.updateSql = updateSql;
}
+ /**
+ * Flag to indicate that the update query is an insert with autogenerated keys, which will be logged at debug level.
+ * @param keysGenerated the flag value to set
+ */
+ public void setKeysGenerated(boolean keysGenerated) {
+ this.keysGenerated = keysGenerated;
+ }
+
public void setUpdateSql(String updateSql) {
this.updateSql = updateSql;
}
@@ -85,17 +103,30 @@ public class JdbcMessageHandler extends AbstractMessageHandler {
*/
protected void handleMessageInternal(Message> message) throws MessageRejectedException, MessageHandlingException,
MessageDeliveryException {
- executeUpdateQuery(message);
- }
-
- private void executeUpdateQuery(Object obj) {
- SqlParameterSource updateParamaterSource = null;
- if (this.sqlParameterSourceFactory != null) {
- updateParamaterSource = this.sqlParameterSourceFactory.createParameterSource(obj);
- this.jdbcOperations.update(this.updateSql, updateParamaterSource);
- } else {
- this.jdbcOperations.update(this.updateSql);
+ List extends Map> keys = executeUpdateQuery(message, keysGenerated);
+ if (logger.isDebugEnabled() && !keys.isEmpty()) {
+ logger.debug("Generated keys: "+keys);
}
}
+ protected List extends Map> executeUpdateQuery(Object obj, boolean keysGenerated) {
+ SqlParameterSource updateParameterSource = new MapSqlParameterSource();
+ if (this.sqlParameterSourceFactory != null) {
+ updateParameterSource = this.sqlParameterSourceFactory.createParameterSource(obj);
+ }
+ if (keysGenerated) {
+ KeyHolder keyHolder = new GeneratedKeyHolder();
+ this.jdbcOperations.getNamedParameterJdbcOperations().update(this.updateSql, updateParameterSource,
+ keyHolder);
+ return keyHolder.getKeyList();
+ }
+ else {
+ int updated = this.jdbcOperations.update(this.updateSql, updateParameterSource);
+ LinkedCaseInsensitiveMap