Drop NativeJdbcExtractor mechanism in favor of JDBC 4 unwrap

Issue: SPR-14670
This commit is contained in:
Juergen Hoeller
2017-02-15 18:04:08 +01:00
parent 54f01cffaf
commit 07dd61eabd
36 changed files with 124 additions and 1693 deletions

View File

@@ -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.
@@ -48,8 +48,6 @@ import org.springframework.jdbc.core.support.AbstractInterruptibleBatchPreparedS
import org.springframework.jdbc.datasource.SingleConnectionDataSource;
import org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator;
import org.springframework.jdbc.support.SQLStateSQLExceptionTranslator;
import org.springframework.jdbc.support.nativejdbc.NativeJdbcExtractor;
import org.springframework.jdbc.support.nativejdbc.NativeJdbcExtractorAdapter;
import org.springframework.util.LinkedCaseInsensitiveMap;
import static org.hamcrest.Matchers.*;
@@ -295,19 +293,6 @@ public class JdbcTemplateTests {
verify(this.preparedStatement).close();
}
@Test
public void testConnectionCallback() throws Exception {
this.template.setNativeJdbcExtractor(new PlainNativeJdbcExtractor());
String result = this.template.execute(new ConnectionCallback<String>() {
@Override
public String doInConnection(Connection con) {
assertSame(JdbcTemplateTests.this.connection, con);
return "test";
}
});
assertEquals("test", result);
}
@Test
public void testConnectionCallbackWithStatementSettings() throws Exception {
String result = this.template.execute(new ConnectionCallback<String>() {
@@ -317,7 +302,6 @@ public class JdbcTemplateTests {
ps.setFetchSize(10);
ps.setMaxRows(20);
ps.close();
assertSame(JdbcTemplateTests.this.connection, new PlainNativeJdbcExtractor().getNativeConnection(con));
return "test";
}
});
@@ -1096,105 +1080,6 @@ public class JdbcTemplateTests {
}
}
@Test
public void testNativeJdbcExtractorInvoked() throws Exception {
final Statement statement2 = mock(Statement.class);
given(statement2.executeQuery(anyString())).willReturn(this.resultSet);
final PreparedStatement preparedStatement2 = mock(PreparedStatement.class);
given(preparedStatement2.executeQuery()).willReturn(this.resultSet);
final ResultSet returnResultSet = mock(ResultSet.class);
given(returnResultSet.next()).willReturn(false);
final CallableStatement callableStatement = mock(CallableStatement.class);
final CallableStatement callableStatement2 = mock(CallableStatement.class);
given(callableStatement2.execute()).willReturn(true);
given(callableStatement2.getUpdateCount()).willReturn(-1);
given(callableStatement2.getResultSet()).willReturn(returnResultSet);
given(callableStatement2.getUpdateCount()).willReturn(-1);
given(this.connection.createStatement()).willReturn(this.statement);
this.template.setNativeJdbcExtractor(new NativeJdbcExtractor() {
@Override
public boolean isNativeConnectionNecessaryForNativeStatements() {
return false;
}
@Override
public boolean isNativeConnectionNecessaryForNativePreparedStatements() {
return false;
}
@Override
public boolean isNativeConnectionNecessaryForNativeCallableStatements() {
return false;
}
@Override
public Connection getNativeConnection(Connection con) {
return con;
}
@Override
public Connection getNativeConnectionFromStatement(Statement stmt) throws SQLException {
return stmt.getConnection();
}
@Override
public Statement getNativeStatement(Statement stmt) {
assertTrue(stmt == JdbcTemplateTests.this.statement);
return statement2;
}
@Override
public PreparedStatement getNativePreparedStatement(PreparedStatement ps) {
assertTrue(ps == JdbcTemplateTests.this.preparedStatement);
return preparedStatement2;
}
@Override
public CallableStatement getNativeCallableStatement(CallableStatement cs) {
assertTrue(cs == callableStatement);
return callableStatement2;
}
@Override
public ResultSet getNativeResultSet(ResultSet rs) {
return rs;
}
});
this.template.query("my query", new ResultSetExtractor<Object>() {
@Override
public Object extractData(ResultSet rs2) {
assertEquals(JdbcTemplateTests.this.resultSet, rs2);
return null;
}
});
this.template.query(new PreparedStatementCreator() {
@Override
public PreparedStatement createPreparedStatement(Connection conn) {
return JdbcTemplateTests.this.preparedStatement;
}
}, new ResultSetExtractor<Object>() {
@Override
public Object extractData(ResultSet rs2) {
assertEquals(JdbcTemplateTests.this.resultSet, rs2);
return null;
}
});
this.template.call(new CallableStatementCreator() {
@Override
public CallableStatement createCallableStatement(Connection con) {
return callableStatement;
}
}, new ArrayList<>());
verify(this.resultSet, times(2)).close();
verify(this.statement).close();
verify(this.preparedStatement).close();
verify(returnResultSet).close();
verify(callableStatement).close();
verify(this.connection, atLeastOnce()).close();
}
@Test
public void testStaticResultSetClosed() throws Exception {
ResultSet resultSet2 = mock(ResultSet.class);
@@ -1311,16 +1196,8 @@ public class JdbcTemplateTests {
given(this.connection.getMetaData()).willReturn(databaseMetaData);
}
private static class PlainNativeJdbcExtractor extends NativeJdbcExtractorAdapter {
@Override
protected Connection doGetNativeConnection(Connection connection) throws SQLException {
return connection;
}
}
private static interface JdbcTemplateCallback {
private interface JdbcTemplateCallback {
void doInJdbcTemplate(JdbcTemplate template, String sql, RowCallbackHandler rch);
}
@@ -1329,6 +1206,7 @@ public class JdbcTemplateTests {
private static class Dispatcher implements PreparedStatementCreator, SqlProvider {
private int id;
private String sql;
public Dispatcher(int id, String sql) {
@@ -1348,4 +1226,5 @@ public class JdbcTemplateTests {
return this.sql;
}
}
}

View File

@@ -31,7 +31,6 @@ import org.mockito.InOrder;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.jdbc.UncategorizedSQLException;
import org.springframework.jdbc.support.nativejdbc.SimpleNativeJdbcExtractor;
import org.springframework.tests.Assume;
import org.springframework.tests.TestGroup;
import org.springframework.transaction.CannotCreateTransactionException;
@@ -142,7 +141,6 @@ public class DataSourceTransactionManagerTests {
try {
if (createStatement) {
tCon.createStatement();
assertEquals(con, new SimpleNativeJdbcExtractor().getNativeConnection(tCon));
}
}
catch (SQLException ex) {
@@ -993,7 +991,6 @@ public class DataSourceTransactionManagerTests {
TransactionAwareDataSourceProxy dsProxy = new TransactionAwareDataSourceProxy(ds);
try {
assertEquals(con, ((ConnectionProxy) dsProxy.getConnection()).getTargetConnection());
assertEquals(con, new SimpleNativeJdbcExtractor().getNativeConnection(dsProxy.getConnection()));
// should be ignored
dsProxy.getConnection().close();
}
@@ -1027,7 +1024,6 @@ public class DataSourceTransactionManagerTests {
final TransactionAwareDataSourceProxy dsProxy = new TransactionAwareDataSourceProxy(ds);
try {
assertEquals(con, ((ConnectionProxy) dsProxy.getConnection()).getTargetConnection());
assertEquals(con, new SimpleNativeJdbcExtractor().getNativeConnection(dsProxy.getConnection()));
// should be ignored
dsProxy.getConnection().close();
}
@@ -1042,7 +1038,6 @@ public class DataSourceTransactionManagerTests {
assertEquals(con, DataSourceUtils.getConnection(ds));
try {
assertEquals(con, ((ConnectionProxy) dsProxy.getConnection()).getTargetConnection());
assertEquals(con, new SimpleNativeJdbcExtractor().getNativeConnection(dsProxy.getConnection()));
// should be ignored
dsProxy.getConnection().close();
}
@@ -1088,7 +1083,6 @@ public class DataSourceTransactionManagerTests {
dsProxy.setReobtainTransactionalConnections(true);
try {
assertEquals(con, ((ConnectionProxy) dsProxy.getConnection()).getTargetConnection());
assertEquals(con, new SimpleNativeJdbcExtractor().getNativeConnection(dsProxy.getConnection()));
// should be ignored
dsProxy.getConnection().close();
}
@@ -1103,7 +1097,6 @@ public class DataSourceTransactionManagerTests {
assertEquals(con, DataSourceUtils.getConnection(ds));
try {
assertEquals(con, ((ConnectionProxy) dsProxy.getConnection()).getTargetConnection());
assertEquals(con, new SimpleNativeJdbcExtractor().getNativeConnection(dsProxy.getConnection()));
// should be ignored
dsProxy.getConnection().close();
}

View File

@@ -1,78 +0,0 @@
/*
* Copyright 2002-2016 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.
* 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.springframework.jdbc.support;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.junit.Test;
import org.springframework.jdbc.support.nativejdbc.SimpleNativeJdbcExtractor;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*;
/**
* @author Andre Biryukov
* @author Juergen Hoeller
*/
public class NativeJdbcExtractorTests {
@Test
public void simpleNativeJdbcExtractor() throws SQLException {
SimpleNativeJdbcExtractor extractor = new SimpleNativeJdbcExtractor();
Connection con = mock(Connection.class);
DatabaseMetaData dbmd = mock(DatabaseMetaData.class);
Connection con2 = mock(Connection.class);
given(con.getMetaData()).willReturn(dbmd);
given(dbmd.getConnection()).willReturn(con2);
Connection nativeCon = extractor.getNativeConnection(con);
assertEquals(con2, nativeCon);
Statement stmt = mock(Statement.class);
given(stmt.getConnection()).willReturn(con);
nativeCon = extractor.getNativeConnectionFromStatement(stmt);
assertEquals(con2, nativeCon);
Statement nativeStmt = extractor.getNativeStatement(stmt);
assertEquals(nativeStmt, stmt);
PreparedStatement ps = mock(PreparedStatement.class);
PreparedStatement nativePs = extractor.getNativePreparedStatement(ps);
assertEquals(ps, nativePs);
CallableStatement cs = mock(CallableStatement.class);
ResultSet rs = mock(ResultSet.class);
given(cs.getResultSet()).willReturn(rs);
CallableStatement nativeCs = extractor.getNativeCallableStatement(cs);
assertEquals(cs, nativeCs);
ResultSet nativeRs = extractor.getNativeResultSet(cs.getResultSet());
assertEquals(nativeRs, rs);
}
}