prepared Spring's DataSource and RowSet adapters for forward compatibility with JDBC 4.1

This commit is contained in:
Juergen Hoeller 2011-06-09 23:09:55 +00:00
parent fdeeeac5d0
commit e1d81e04bc
5 changed files with 66 additions and 10 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2008 the original author or authors. * Copyright 2002-2011 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,15 +16,13 @@
package org.springframework.jndi; package org.springframework.jndi;
import static org.junit.Assert.*;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.Map; import java.util.Map;
import java.util.logging.Logger;
import javax.naming.Binding; import javax.naming.Binding;
import javax.naming.Context; import javax.naming.Context;
import javax.naming.InitialContext; import javax.naming.InitialContext;
@ -36,9 +34,12 @@ import javax.naming.spi.InitialContextFactory;
import javax.sql.DataSource; import javax.sql.DataSource;
import org.junit.Test; import org.junit.Test;
import org.springframework.mock.jndi.SimpleNamingContext; import org.springframework.mock.jndi.SimpleNamingContext;
import org.springframework.mock.jndi.SimpleNamingContextBuilder; import org.springframework.mock.jndi.SimpleNamingContextBuilder;
import static org.junit.Assert.*;
/** /**
* @author Juergen Hoeller * @author Juergen Hoeller
@ -250,4 +251,7 @@ class StubDataSource implements DataSource {
return null; return null;
} }
public Logger getParentLogger() {
return null;
}
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2008 the original author or authors. * Copyright 2002-2011 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,6 +16,7 @@
package org.springframework.jdbc.core; package org.springframework.jdbc.core;
import java.lang.reflect.Method;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import javax.sql.rowset.CachedRowSet; import javax.sql.rowset.CachedRowSet;
@ -24,6 +25,7 @@ import com.sun.rowset.CachedRowSetImpl;
import org.springframework.jdbc.support.rowset.ResultSetWrappingSqlRowSet; import org.springframework.jdbc.support.rowset.ResultSetWrappingSqlRowSet;
import org.springframework.jdbc.support.rowset.SqlRowSet; import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.util.ReflectionUtils;
/** /**
* ResultSetExtractor implementation that returns a Spring SqlRowSet * ResultSetExtractor implementation that returns a Spring SqlRowSet
@ -31,7 +33,8 @@ import org.springframework.jdbc.support.rowset.SqlRowSet;
* *
* <p>The default implementation uses a standard JDBC CachedRowSet underneath. * <p>The default implementation uses a standard JDBC CachedRowSet underneath.
* This means that JDBC RowSet support needs to be available at runtime: * This means that JDBC RowSet support needs to be available at runtime:
* by default, Sun's <code>com.sun.rowset.CachedRowSetImpl</code> class. * by default, Sun's <code>com.sun.rowset.CachedRowSetImpl</code> class on Java 5 and 6,
* or the <code>javax.sql.rowset.RowSetProvider</code> mechanism on Java 7 / JDBC 4.1.
* *
* @author Juergen Hoeller * @author Juergen Hoeller
* @since 1.2 * @since 1.2
@ -42,6 +45,24 @@ import org.springframework.jdbc.support.rowset.SqlRowSet;
*/ */
public class SqlRowSetResultSetExtractor implements ResultSetExtractor<SqlRowSet> { public class SqlRowSetResultSetExtractor implements ResultSetExtractor<SqlRowSet> {
private static Object rowSetFactory = null;
private static Method createCachedRowSet = null;
static {
ClassLoader cl = SqlRowSetResultSetExtractor.class.getClassLoader();
try {
Class rowSetProviderClass = cl.loadClass("javax.sql.rowset.RowSetProvider");
Method newFactory = rowSetProviderClass.getMethod("newFactory");
rowSetFactory = ReflectionUtils.invokeMethod(newFactory, null);
createCachedRowSet = rowSetFactory.getClass().getMethod("createCachedRowSet");
}
catch (Exception ex) {
// JDBC 4.1 API not available - fall back to Sun CachedRowSetImpl
}
}
public SqlRowSet extractData(ResultSet rs) throws SQLException { public SqlRowSet extractData(ResultSet rs) throws SQLException {
return createSqlRowSet(rs); return createSqlRowSet(rs);
} }
@ -75,7 +96,13 @@ public class SqlRowSetResultSetExtractor implements ResultSetExtractor<SqlRowSet
* @see com.sun.rowset.CachedRowSetImpl * @see com.sun.rowset.CachedRowSetImpl
*/ */
protected CachedRowSet newCachedRowSet() throws SQLException { protected CachedRowSet newCachedRowSet() throws SQLException {
return new CachedRowSetImpl(); if (createCachedRowSet != null) {
// RowSetProvider.newFactory().createCachedRowSet();
return (CachedRowSet) ReflectionUtils.invokeJdbcMethod(createCachedRowSet, rowSetFactory);
}
else {
return new CachedRowSetImpl();
}
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2008 the original author or authors. * Copyright 2002-2011 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -18,6 +18,7 @@ package org.springframework.jdbc.datasource;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.logging.Logger;
import javax.sql.DataSource; import javax.sql.DataSource;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
@ -90,4 +91,13 @@ public abstract class AbstractDataSource implements DataSource {
return DataSource.class.equals(iface); return DataSource.class.equals(iface);
} }
//---------------------------------------------------------------------
// Implementation of JDBC 4.1's getParentLogger method
//---------------------------------------------------------------------
public Logger getParentLogger() {
return Logger.getGlobal();
}
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2008 the original author or authors. * Copyright 2002-2011 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -19,6 +19,7 @@ package org.springframework.jdbc.datasource;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.logging.Logger;
import javax.sql.DataSource; import javax.sql.DataSource;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
@ -117,4 +118,13 @@ public class DelegatingDataSource implements DataSource, InitializingBean {
return getTargetDataSource().isWrapperFor(iface); return getTargetDataSource().isWrapperFor(iface);
} }
//---------------------------------------------------------------------
// Implementation of JDBC 4.1's getParentLogger method
//---------------------------------------------------------------------
public Logger getParentLogger() {
return Logger.getGlobal();
}
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2010 the original author or authors. * Copyright 2002-2011 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -19,6 +19,7 @@ package org.springframework.jdbc.datasource.embedded;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.logging.Logger;
import javax.sql.DataSource; import javax.sql.DataSource;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
@ -227,6 +228,10 @@ public class EmbeddedDatabaseFactory {
return this.dataSource.isWrapperFor(iface); return this.dataSource.isWrapperFor(iface);
} }
public Logger getParentLogger() {
return Logger.getGlobal();
}
public void shutdown() { public void shutdown() {
shutdownDatabase(); shutdownDatabase();
} }