diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplate.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplate.java
index 439eb08574..ca8a402f15 100644
--- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplate.java
+++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplate.java
@@ -105,14 +105,26 @@ public class NamedParameterJdbcTemplate implements NamedParameterJdbcOperations
/**
- * Expose the classic Spring JdbcTemplate to allow invocation of
- * less commonly used methods.
+ * Expose the classic Spring JdbcTemplate operations to allow invocation
+ * of less commonly used methods.
*/
@Override
public JdbcOperations getJdbcOperations() {
return this.classicJdbcTemplate;
}
+ /**
+ * Expose the classic Spring {@link JdbcTemplate} itself, if available,
+ * in particular for passing it on to other {@code JdbcTemplate} consumers.
+ *
If sufficient for the purposes at hand, {@link #getJdbcOperations()}
+ * is recommended over this variant.
+ * @since 5.0.3
+ */
+ public JdbcTemplate getJdbcTemplate() {
+ Assert.state(this.classicJdbcTemplate instanceof JdbcTemplate, "No JdbcTemplate available");
+ return (JdbcTemplate) this.classicJdbcTemplate;
+ }
+
/**
* Specify the maximum number of entries for this template's SQL cache.
* Default is 256.
diff --git a/spring-jdbc/src/test/java/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplateTests.java b/spring-jdbc/src/test/java/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplateTests.java
index a64f85e7ac..33af4c29f8 100644
--- a/spring-jdbc/src/test/java/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplateTests.java
+++ b/spring-jdbc/src/test/java/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplateTests.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2016 the original author or authors.
+ * Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -34,14 +34,10 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
-import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.Customer;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCallback;
-import org.springframework.jdbc.core.ResultSetExtractor;
-import org.springframework.jdbc.core.RowCallbackHandler;
-import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.SqlParameterValue;
import static org.junit.Assert.*;
@@ -79,8 +75,9 @@ public class NamedParameterJdbcTemplateTests {
private Map params = new HashMap<>();
private NamedParameterJdbcTemplate namedParameterTemplate;
+
@Before
- public void setUp() throws Exception {
+ public void setup() throws Exception {
connection = mock(Connection.class);
dataSource = mock(DataSource.class);
preparedStatement = mock(PreparedStatement.class);
@@ -95,18 +92,24 @@ public class NamedParameterJdbcTemplateTests {
given(databaseMetaData.supportsBatchUpdates()).willReturn(true);
}
+
@Test
- public void testNullDataSourceProvidedToCtor() throws Exception {
+ public void testNullDataSourceProvidedToCtor() {
thrown.expect(IllegalArgumentException.class);
new NamedParameterJdbcTemplate((DataSource) null);
}
@Test
- public void testNullJdbcTemplateProvidedToCtor() throws Exception {
+ public void testNullJdbcTemplateProvidedToCtor() {
thrown.expect(IllegalArgumentException.class);
new NamedParameterJdbcTemplate((JdbcOperations) null);
}
+ @Test
+ public void testTemplateConfiguration() {
+ assertSame(dataSource, namedParameterTemplate.getJdbcTemplate().getDataSource());
+ }
+
@Test
public void testExecute() throws SQLException {
given(preparedStatement.executeUpdate()).willReturn(1);
@@ -114,14 +117,10 @@ public class NamedParameterJdbcTemplateTests {
params.put("perfId", 1);
params.put("priceId", 1);
Object result = namedParameterTemplate.execute(UPDATE_NAMED_PARAMETERS, params,
- new PreparedStatementCallback