support spring Jdbc4NativeJdbcExtractor

This commit is contained in:
wenshao
2012-01-04 16:37:29 +08:00
parent 408b5c778f
commit 8bbb737dad
25 changed files with 499 additions and 384 deletions
File diff suppressed because it is too large Load Diff
@@ -30,6 +30,8 @@ import java.sql.SQLFeatureNotSupportedException;
import java.sql.SQLXML;
import java.util.Calendar;
import com.alibaba.druid.proxy.jdbc.CallableStatementProxy;
/**
* @author wenshao<szujobs@hotmail.com>
*/
@@ -1054,4 +1056,15 @@ public class DruidPooledCallableStatement extends DruidPooledPreparedStatement i
throw new SQLFeatureNotSupportedException();
}
@SuppressWarnings("unchecked")
public <T> T unwrap(Class<T> iface) throws SQLException {
if (iface == CallableStatement.class) {
if (stmt instanceof CallableStatementProxy) {
return stmt.unwrap(iface);
}
return (T) stmt;
}
return super.unwrap(iface);
}
}
@@ -43,6 +43,7 @@ import com.alibaba.druid.logging.Log;
import com.alibaba.druid.logging.LogFactory;
import com.alibaba.druid.pool.DruidPooledPreparedStatement.PreparedStatementKey;
import com.alibaba.druid.pool.PreparedStatementPool.MethodType;
import com.alibaba.druid.proxy.jdbc.ConnectionProxy;
import com.alibaba.druid.util.TransactionInfo;
/**
@@ -540,6 +541,14 @@ public class DruidPooledConnection implements javax.sql.PooledConnection, Connec
if (iface == null) {
return null;
}
if (iface == Connection.class) {
if (conn instanceof ConnectionProxy) {
return conn.unwrap(iface);
}
return (T) conn;
}
if (iface.isInstance(this)) {
return (T) this;
@@ -38,6 +38,7 @@ import java.util.Arrays;
import java.util.Calendar;
import com.alibaba.druid.pool.PreparedStatementPool.MethodType;
import com.alibaba.druid.proxy.jdbc.PreparedStatementProxy;
import com.alibaba.druid.util.OracleUtils;
/**
@@ -987,4 +988,16 @@ public class DruidPooledPreparedStatement extends DruidPooledStatement implement
}
}
@SuppressWarnings("unchecked")
public <T> T unwrap(Class<T> iface) throws SQLException {
if (iface == PreparedStatement.class) {
if (stmt instanceof PreparedStatementProxy) {
return stmt.unwrap(iface);
}
return (T) stmt;
}
return super.unwrap(iface);
}
}
@@ -33,6 +33,8 @@ import java.sql.SQLXML;
import java.sql.Statement;
import java.util.Calendar;
import com.alibaba.druid.proxy.jdbc.ResultSetProxy;
/**
* @author wenshao<szujobs@hotmail.com>
*/
@@ -1766,4 +1768,17 @@ public final class DruidPooledResultSet extends PoolableWrapper implements Resul
public <T> T getObject(String columnLabel, Class<T> type) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@SuppressWarnings("unchecked")
public <T> T unwrap(Class<T> iface) throws SQLException {
if (iface == ResultSet.class) {
if (rs instanceof ResultSetProxy) {
return rs.unwrap(iface);
}
return (T) rs;
}
return super.unwrap(iface);
}
}
@@ -25,6 +25,7 @@ import java.util.List;
import com.alibaba.druid.logging.Log;
import com.alibaba.druid.logging.LogFactory;
import com.alibaba.druid.proxy.jdbc.StatementProxy;
/**
* @author wenshao<szujobs@hotmail.com>
@@ -578,4 +579,16 @@ public class DruidPooledStatement extends PoolableWrapper implements Statement {
public boolean isCloseOnCompletion() throws SQLException {
return false;
}
@SuppressWarnings("unchecked")
public <T> T unwrap(Class<T> iface) throws SQLException {
if (iface == Statement.class) {
if (stmt instanceof StatementProxy) {
return stmt.unwrap(iface);
}
return (T) stmt;
}
return super.unwrap(iface);
}
}
@@ -559,7 +559,7 @@ public class MultiDataSourceConnection extends WrapperAdapter implements Connect
}
@Override
public Object getRawObject() {
public Connection getRawObject() {
return conn;
}
@@ -571,11 +571,6 @@ public class MultiDataSourceConnection extends WrapperAdapter implements Connect
return attributes;
}
@Override
public Connection getConnectionRaw() {
return conn;
}
@Override
public Properties getProperties() {
return multiDataSource.getProperties();
@@ -29,11 +29,11 @@ public class MultiDataSourceStatement extends WrapperAdapter implements Statemen
conn.checkConnection(sql);
if (resultSetType != null && resultSetConcurrency != null && resultSetHoldability != null) {
stmt = conn.getConnectionRaw().createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
stmt = conn.getRawObject().createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
} else if (resultSetType != null && resultSetConcurrency != null) {
stmt = conn.getConnectionRaw().createStatement(resultSetType, resultSetConcurrency);
stmt = conn.getRawObject().createStatement(resultSetType, resultSetConcurrency);
} else {
stmt = conn.getConnectionRaw().createStatement();
stmt = conn.getRawObject().createStatement();
}
if (maxFieldSize != null) {
@@ -62,7 +62,7 @@ public class OracleValidConnectionChecker implements ValidConnectionChecker, Ser
}
if (conn instanceof ConnectionProxy) {
conn = ((ConnectionProxy) conn).getConnectionRaw();
conn = ((ConnectionProxy) conn).getRawObject();
}
int status = OracleUtils.pingDatabase(conn);
@@ -22,5 +22,5 @@ import java.sql.CallableStatement;
*/
public interface CallableStatementProxy extends CallableStatement, PreparedStatementProxy {
CallableStatement getRawCallableStatement();
CallableStatement getRawObject();
}
@@ -23,6 +23,7 @@ import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.NClob;
import java.sql.PreparedStatement;
import java.sql.Ref;
import java.sql.RowId;
import java.sql.SQLException;
@@ -42,7 +43,7 @@ public class CallableStatementProxyImpl extends PreparedStatementProxyImpl imple
this.statement = statement;
}
public CallableStatement getRawCallableStatement() {
public CallableStatement getRawObject() {
return this.statement;
}
@@ -609,4 +610,12 @@ public class CallableStatementProxyImpl extends PreparedStatementProxyImpl imple
throw new SQLFeatureNotSupportedException();
}
@SuppressWarnings("unchecked")
public <T> T unwrap(Class<T> iface) throws SQLException {
if (iface == PreparedStatement.class) {
return (T) statement;
}
return super.unwrap(iface);
}
}
@@ -24,7 +24,7 @@ import java.util.Properties;
*/
public interface ConnectionProxy extends Connection, WrapperProxy {
Connection getConnectionRaw();
Connection getRawObject();
Properties getProperties();
@@ -71,6 +71,10 @@ public class ConnectionProxyImpl extends WrapperProxyImpl implements ConnectionP
public Connection getConnectionRaw() {
return connection;
}
public Connection getRawObject() {
return connection;
}
public DataSourceProxy getDirectDataSource() {
return this.dataSource;
@@ -341,4 +345,12 @@ public class ConnectionProxyImpl extends WrapperProxyImpl implements ConnectionP
throw new SQLFeatureNotSupportedException();
}
@SuppressWarnings("unchecked")
public <T> T unwrap(Class<T> iface) throws SQLException {
if (iface == Connection.class) {
return (T) connection;
}
return super.unwrap(iface);
}
}
@@ -24,5 +24,5 @@ public interface PreparedStatementProxy extends PreparedStatement, StatementProx
String getSql();
PreparedStatement getRawPreparedStatement();
PreparedStatement getRawObject();
}
@@ -54,7 +54,7 @@ public class PreparedStatementProxyImpl extends StatementProxyImpl implements Pr
return this.sql;
}
public PreparedStatement getRawPreparedStatement() {
public PreparedStatement getRawObject() {
return this.statement;
}
@@ -342,4 +342,13 @@ public class PreparedStatementProxyImpl extends StatementProxyImpl implements Pr
public String getLastExecuteSql() {
return this.sql;
}
@SuppressWarnings("unchecked")
public <T> T unwrap(Class<T> iface) throws SQLException {
if (iface == PreparedStatement.class) {
return (T) statement;
}
return super.unwrap(iface);
}
}
@@ -1050,4 +1050,12 @@ public class ResultSetProxyImpl extends WrapperProxyImpl implements ResultSetPro
throw new SQLFeatureNotSupportedException();
}
@SuppressWarnings("unchecked")
public <T> T unwrap(Class<T> iface) throws SQLException {
if (iface == ResultSet.class) {
return (T) resultSet;
}
return super.unwrap(iface);
}
}
@@ -25,7 +25,7 @@ public interface StatementProxy extends Statement, WrapperProxy {
ConnectionProxy getConnectionProxy();
Statement getStatementRaw();
Statement getRawObject();
List<String> getBatchSqlList();
@@ -54,7 +54,7 @@ public class StatementProxyImpl extends WrapperProxyImpl implements StatementPro
return connection;
}
public Statement getStatementRaw() {
public Statement getRawObject() {
return this.statement;
}
@@ -314,4 +314,13 @@ public class StatementProxyImpl extends WrapperProxyImpl implements StatementPro
public boolean isCloseOnCompletion() throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@SuppressWarnings("unchecked")
public <T> T unwrap(Class<T> iface) throws SQLException {
if (iface == Statement.class) {
return (T) statement;
}
return super.unwrap(iface);
}
}
@@ -1,111 +1,7 @@
package com.alibaba.druid.spring;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.springframework.jdbc.support.nativejdbc.Jdbc4NativeJdbcExtractor;
import org.springframework.jdbc.support.nativejdbc.NativeJdbcExtractor;
import com.alibaba.druid.pool.DruidPooledCallableStatement;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.alibaba.druid.pool.DruidPooledPreparedStatement;
import com.alibaba.druid.pool.DruidPooledResultSet;
import com.alibaba.druid.pool.DruidPooledStatement;
import com.alibaba.druid.proxy.jdbc.CallableStatementProxy;
import com.alibaba.druid.proxy.jdbc.ConnectionProxy;
import com.alibaba.druid.proxy.jdbc.PreparedStatementProxy;
import com.alibaba.druid.proxy.jdbc.ResultSetProxy;
import com.alibaba.druid.proxy.jdbc.StatementProxy;
public class DruidJdbcExtractor implements NativeJdbcExtractor {
@Override
public boolean isNativeConnectionNecessaryForNativeStatements() {
return true;
}
@Override
public boolean isNativeConnectionNecessaryForNativePreparedStatements() {
return true;
}
@Override
public boolean isNativeConnectionNecessaryForNativeCallableStatements() {
return true;
}
@Override
public Connection getNativeConnection(Connection conn) throws SQLException {
if (conn instanceof DruidPooledConnection) {
conn = ((DruidPooledConnection) conn).getConnection();
}
if (conn instanceof ConnectionProxy) {
conn = ((ConnectionProxy) conn).getConnectionRaw();
}
return conn;
}
@Override
public Connection getNativeConnectionFromStatement(Statement stmt) throws SQLException {
return getNativeConnection(stmt.getConnection());
}
@Override
public Statement getNativeStatement(Statement stmt) throws SQLException {
if (stmt instanceof DruidPooledStatement) {
stmt = ((DruidPooledStatement) stmt).getStatement();
}
if (stmt instanceof StatementProxy) {
stmt = ((StatementProxy) stmt).getStatementRaw();
}
return stmt;
}
@Override
public PreparedStatement getNativePreparedStatement(PreparedStatement stmt) throws SQLException {
if (stmt instanceof DruidPooledPreparedStatement) {
stmt = ((DruidPooledPreparedStatement) stmt).getRawPreparedStatement();
}
if (stmt instanceof PreparedStatementProxy) {
stmt = ((PreparedStatementProxy) stmt).getRawPreparedStatement();
}
return stmt;
}
@Override
public CallableStatement getNativeCallableStatement(CallableStatement stmt) throws SQLException {
if (stmt instanceof DruidPooledCallableStatement) {
stmt = ((DruidPooledCallableStatement) stmt).getCallableStatementRaw();
}
if (stmt instanceof CallableStatementProxy) {
stmt = ((CallableStatementProxy) stmt).getRawCallableStatement();
}
return stmt;
}
@Override
public ResultSet getNativeResultSet(ResultSet rs) throws SQLException {
if (rs instanceof DruidPooledResultSet) {
rs = ((DruidPooledResultSet) rs).getRawResultSet();
}
if (rs instanceof ResultSetProxy) {
rs = ((ResultSetProxy) rs).getResultSetRaw();
}
return rs;
}
public class DruidJdbcExtractor extends Jdbc4NativeJdbcExtractor {
}
@@ -44,7 +44,7 @@ public class DruidNativeJdbcExtractor implements NativeJdbcExtractor {
}
if (conn instanceof ConnectionProxy) {
conn = ((ConnectionProxy) conn).getConnectionRaw();
conn = ((ConnectionProxy) conn).getRawObject();
}
return conn;
@@ -63,7 +63,7 @@ public class DruidNativeJdbcExtractor implements NativeJdbcExtractor {
}
if (stmt instanceof StatementProxy) {
stmt = ((StatementProxy) stmt).getStatementRaw();
stmt = ((StatementProxy) stmt).getRawObject();
}
return stmt;
@@ -76,7 +76,7 @@ public class DruidNativeJdbcExtractor implements NativeJdbcExtractor {
}
if (stmt instanceof PreparedStatementProxy) {
stmt = ((PreparedStatementProxy) stmt).getRawPreparedStatement();
stmt = ((PreparedStatementProxy) stmt).getRawObject();
}
return stmt;
@@ -89,7 +89,7 @@ public class DruidNativeJdbcExtractor implements NativeJdbcExtractor {
}
if (stmt instanceof CallableStatementProxy) {
stmt = ((CallableStatementProxy) stmt).getRawCallableStatement();
stmt = ((CallableStatementProxy) stmt).getRawObject();
}
return stmt;
@@ -88,6 +88,7 @@ public class OracleUtils {
oracleConn.setStatementCacheSize(size);
}
@SuppressWarnings("deprecation")
public static int pingDatabase(Connection conn) throws SQLException {
OracleConnection oracleConn = unwrap(conn);
return oracleConn.pingDatabase(1000);