如何将dbcp连接转换为oracle连接? [英] how to cast dbcp connection to oracle connection?

查看:194
本文介绍了如何将dbcp连接转换为oracle连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在运行时将PoolableConnection转换为OracleConnection,但我不知道该怎么做。
因为我得到了classCastException并且如果所有类都扩展了Connection,我可以这样做吗?

I need cast PoolableConnection to OracleConnection in runtime but I don't know how to do it. becasuse i got classCastException and if all classes extends Connection, can I do it ?

推荐答案

你应该能够转换为DBCP特定的Connection类,并从那里检索内部Oracle连接:

You should just be able to cast to the DBCP specific Connection class and from there retrieve the inner Oracle connection:

import org.apache.commons.dbcp.DelegatingConnection;

DelegatingConnection dc = (DelegatingConnection)conn;
OracleConnection oc = (OracleConnection)pc.getInnermostDelegate();

如果您使用的是Tomcat的DBCP内置副本,那么您需要的导入是:

If you are using Tomcat's built-in copy of DBCP then the import you will need is:

import org.apache.tomcat.dbcp.dbcp.DelegatingConnection;

或者您可以使用Oracle JDBC驱动程序实现中内置的连接池。这将返回Oracle连接。一个简单的设置是:

Or you can use the connection pooling built into the Oracle JDBC driver implementation. This returns an Oracle connection. A simple setup would be:

<Resource auth="Container"
          connectionCacheName="CXCACHE"
          connectionCacheProperties="{MaxStatementsLimit=5,MinLimit=1, MaxLimit=1, ValidateConnection=true}"
          connectionCachingEnabled="true"
          description="Oracle Datasource"
          factory="oracle.jdbc.pool.OracleDataSourceFactory"
          name="jdbc/TestDB"
          user="default_user" 
          password="password"
          type="oracle.jdbc.pool.OracleDataSource"
          url="jdbc:oracle:thin:@//localhost:1521/orcl"
          />

这篇关于如何将dbcp连接转换为oracle连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆