MSSQL2000的官方JDBC在應用動態代理機制遇到的異常問題解決

      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驅動出了什麼毛病。

 


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章