MSSQL2000的官方JDBC在应用动态代理机制遇到的异常问题解决
来源:互联网 发布:mysql id命名规范 编辑:程序博客网 时间:2024/06/05 06:08
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驱动出了什么毛病。
- MSSQL2000的官方JDBC在应用动态代理机制遇到的异常问题解决
- 动态代理在JDBC的DataSource中的应用
- MSSQL2000安装过程中挂起和JDBC连接时异常的解决方法
- 动态代理的应用
- 反射机制在代理中的应用(二) 动态代理
- 使用Hive-JDBC遇到的一些问题解决
- Spring的动态代理机制
- Java的动态代理机制
- JDK的动态代理机制
- JDK的动态代理机制
- JDK的动态代理机制
- JDK的动态代理机制
- JDK的动态代理机制
- java的动态代理机制
- JDK的动态代理机制
- java的动态代理机制
- JDK的动态代理机制
- JDK的动态代理机制
- SQL Server中全角和半角字符的比较问题
- SQLPlus命令使用指南(二)
- 如何让PowerDesigner支持自动生成含SQL Server 2000的表和列注释的角本
- SQLPlus命令使用指南(三)
- ASE12.5数据库内嵌JAVA开发指南
- MSSQL2000的官方JDBC在应用动态代理机制遇到的异常问题解决
- ASP.net实现信用卡检查和自定义确认控件(一)
- ASP.net实现信用卡检查和自定义确认控件(二)
- 一对多关联的数据库在VB.NET中如何新增记录
- scanf()函数的用法和实践
- define与typedef数据类型表示的区别
- 将Excel中的数据导入至MsSQLSERVER中(示例)
- 无组件上传图片至SQLSERVER数据库(示例)
- JAVA与模式 学习笔记(一) 统一的建模语言UML介绍(1)