MSSQL2000的官方JDBC在应用动态代理机制遇到的异常问题解决

类别:Java 点击:0 评论:0 推荐:

      MSSQL2000的官方JDBC在应用动态代理机制遇到的异常问题解决

      最近,翻出以前做的connection pool,结果在调用SQL Server 2000的官方JDBC遇到了错误。connection pool的原理基本参照使用JAVA中的动态代理实现数据库连接池这个来实现的。

     下面的代码,在Microsoft sql2000的官方jdbc通不过。
public java.sql.Connection getConnection()
 { return (java.sql.Connection)Proxy.newProxyInstance(

                      conn.getClass().getClassLoader(),
                      conn.getClass().getInterfaces(),
                      this); }

会导致,ClassCastException异常。没有搞明白到底why?估计和驱动程序有关。

     我想出了一个很笨的方法,希望和大家讨论。最简单,自己做一个EnhydraboyPooledConnection类,继承了java.sql.connection。

public final class EnhydraboyPooledConnection implements Connection {
  private Connection conn=null;

  public EnhydraboyPooledConnection(Connection conn) {
    this.conn=conn;
  }
  public Statement createStatement() throws SQLException {
    return conn.createStatement();
  }
  public PreparedStatement prepareStatement(String sql) throws SQLException {
    return conn.prepareStatement(sql);
  }
  public CallableStatement prepareCall(String sql) throws SQLException {
    return conn.prepareCall(sql);
  }

  public String nativeSQL(String sql) throws SQLException {
   return conn.nativeSQL(sql);
  }

  public void setAutoCommit(boolean autoCommit) throws SQLException {
    if(conn.getAutoCommit()!=autoCommit){
      conn.setAutoCommit(autoCommit);
    }
  }
  public boolean getAutoCommit() throws SQLException {
    return conn.getAutoCommit();
  }
  public void commit() throws SQLException {
   conn.commit();
  }
  public void rollback() throws SQLException {
    conn.rollback();
  }
  public void close() throws SQLException {
    conn.close();
  }
  public boolean isClosed() throws SQLException {
    return conn.isClosed();
  }
  public DatabaseMetaData getMetaData() throws SQLException {
    return conn.getMetaData();
  }
  public void setReadOnly(boolean readOnly) throws SQLException {
    conn.setReadOnly(readOnly);
  }
  public boolean isReadOnly() throws SQLException {
    return conn.isReadOnly();
  }
  public void setCatalog(String catalog) throws SQLException {
    conn.setCatalog(catalog);
  }
  public String getCatalog() throws SQLException {
    return conn.getCatalog();
  }
  public void setTransactionIsolation(int level) throws SQLException {
    conn.setTransactionIsolation(level);
  }
  public int getTransactionIsolation() throws SQLException {
    return conn.getTransactionIsolation();
  }
  public SQLWarning getWarnings() throws SQLException {
    return conn.getWarnings();
  }
  public void clearWarnings() throws SQLException {
    conn.clearWarnings();
  }
  public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
    return conn.createStatement(resultSetType,resultSetConcurrency);
  }

  public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
    return conn.prepareStatement(sql,resultSetType ,resultSetConcurrency);
  }

  public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
    return conn.prepareCall(sql,resultSetType,resultSetConcurrency);
  }

  public Map getTypeMap() throws SQLException {
    return conn.getTypeMap();
  }

  public void setTypeMap(Map map) throws SQLException {
    conn.setTypeMap(map);
  }

  public void setHoldability(int holdability) throws SQLException {
   conn.setHoldability(holdability);
  }

  public int getHoldability() throws SQLException {
    return conn.getHoldability();
  }
  public Savepoint setSavepoint() throws SQLException {
    return conn.setSavepoint();
  }

  public Savepoint setSavepoint(String name) throws SQLException {
    return conn.setSavepoint(name);
  }
  public void rollback(Savepoint savepoint) throws SQLException {
    conn.rollback(savepoint);
  }

  public void releaseSavepoint(Savepoint savepoint) throws SQLException {
    conn.releaseSavepoint(savepoint);
  }
  public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
    return conn.createStatement(resultSetType,resultSetConcurrency,resultSetHoldability);
  }

  public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
    return conn.prepareStatement(sql,resultSetConcurrency,resultSetHoldability);
  }

  public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
    return conn.prepareCall(sql,resultSetConcurrency,resultSetHoldability);
  }
  public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
    return conn.prepareStatement(sql,autoGeneratedKeys);
  }
  public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
    return conn.prepareStatement(sql,columnIndexes);
  }
  public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
    return conn.prepareStatement(sql,columnNames);
  }
}

java.sql.connection接口的方法都要自己实现一遍,不过也简单。调用conn.对应方法就可以了。

然后,所以,在DataSourceImpl里面,凡是产生新的代理Connection的地方,都修改一下。

Connection cn=newConnection();
_Connection conn=new _Connection(new EnhydraboyPooledConnection(cn),false,i);

   不过,没搞明白,到底SQLServer jdbc驱动出了什么毛病。

 


 

本文地址:http://com.8s8s.com/it/it16462.htm