问题描述
我正在尝试检测我的jdbc连接.我知道关于此主题也有几个类似的问题.
I'm trying to instrument my jdbc connections. I know there are several similar questions about this topic.
我尽了一切努力,但到目前为止找不到解决我问题的正确方法.还尝试了此问题的答案,但没有结果: Apache Commons DBCP连接对象问题,线程:组织中的ClassCastException .apache.tomcat.dbcp.dbcp.PoolingDataSource $ PoolGuardConnectionWrapper
I tried everything but couldn't find the propper way to solve my issue so far.Also tried the answers to this question, with no result:Apache Commons DBCP connection object problem, Thread: ClassCastException in org.apache.tomcat.dbcp.dbcp.PoolingDataSource$PoolGuardConnectionWrapper
我正在使用Tomcat 7和Java7.在这里,我在context.xml中定义了oracle连接池:
I'm working with Tomcat 7 and Java 7. Here's where I define the oracle connection pool in my context.xml:
<Resource name="jdbc/myDS"
type="javax.sql.DataSource"
auth="Container"
maxActive="350"
maxIdle="50"
minIdle="10"
maxWait="10000"
username="user_own"
password="mypassw"
accessToUnderlyingConnectionAllowed="true"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@192.168.110.173:1521/orcl" />
我的检测代码:
private static void initInstrumentation(Connection con, final String usuario, final String modulo, final String accion) throws Exception {
if (Utils.getParameter("instrumentation.active").equals("1")) {
try {
OracleConnection oracleConnection = null;
//This is where I try to get the oracle connection, but no succeed
if (con != null) {
if (con instanceof OracleConnection) {//NEVER COME IN HERE
oracleConnection = (OracleConnection) con;
} else if (con.isWrapperFor(OracleConnection.class)) {//NEVER COME IN HERE
oracleConnection = con.unwrap(OracleConnection.class);
} else{
//NO ORACLECONNECTION NO isWrapperFor -> ALWAYS ENDS HERE!!!
//oracleConnection = (OracleConnection) ((DelegatingConnection) con).getDelegate();
oracleConnection = (OracleConnection) new DelegatingConnection(con).getInnermostDelegate();
}
}
if (oracleConnection != null) {
String[] metrics = new String[OracleConnection.END_TO_END_STATE_INDEX_MAX];
metrics[OracleConnection.END_TO_END_MODULE_INDEX] = modulo;
metrics[OracleConnection.END_TO_END_ACTION_INDEX] = "Inicio: " + accion;
metrics[OracleConnection.END_TO_END_CLIENTID_INDEX] = usuario;
oracleConnection.setEndToEndMetrics(metrics, (short) 0);
}
} catch (Exception e) {
throw new Exception("Error initInstrumentation " + e);
}
}
}
我的开放连接方法:
private static Connection genericOpenConnection() throws Exception {
Connection con = null;
try {
DataSource dataSource = (DataSource) new InitialContext().lookup(Utils.cStrPlx(Utils.getParameter("dataSourceJndiName")));
con = dataSource.getConnection();
con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
} catch (Exception e) {
Error.escribeLog("Error : " + e.getMessage());
throw new SQLException(e.getMessage());
}
return con;
}
因此,在调用openConnection和initInstrumentation之后,我得到了下一个试图将连接强制转换为oracle连接的异常.有关如何执行此操作的任何想法?我怎么了?预先感谢.
So after calling openConnection and initInstrumentation I'm getting the next exception trying to cast the connection to an oracle connection. Any ideas of how to do this? What am I getting wrong?Thanks in advance.
推荐答案
我发现了我的问题.我希望这可以为遇到相同问题的任何人提供帮助.
I found my problem. I hope this can help anyone with the same issue.
这似乎与与ojdbc驱动程序库的冲突有关.我的tomcat中有一个驱动程序,另外一个是通过maven在pom.xml中声明的.
The thing seems to be related to a conflict with the ojdbc driver libraries.I have one driver in my tomcat, and another one declared in pom.xml via maven.
<!-- Driver oracle -->
<dependency>
<groupId>com.plexus</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0</version>
<scope>provided</scope>
</dependency>
将驱动程序声明为已提供解决了我的问题,现在已按如下所述检索连接
Declaring this driver as provided fixed my problem, and the connection now is been retrieved as described below
if (con.isWrapperFor(OracleConnection.class)) {
oracleConnection = con.unwrap(OracleConnection.class);
}
这篇关于规范:将org.apache.tomcat.dbcp.dbcp.PoolingDataSource $ PoolGuardConnectionWrapper转换为oracle.jdbc.OracleConnection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!