support spring Jdbc4NativeJdbcExtractor

This commit is contained in:
wenshao
2012-01-04 16:16:09 +08:00
parent 408b5c778f
commit e7c4095020
15 changed files with 224 additions and 109 deletions
@@ -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);
}
}
@@ -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;
@@ -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);
}
}
@@ -341,4 +341,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);
}
}
@@ -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);
}
}
@@ -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 {
}
@@ -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);