连接不能转换为oracle.jdbc.OracleConnection [英] Connection cannot be cast to oracle.jdbc.OracleConnection

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

问题描述

为什么java.sql.Connection不能转换为oracle.jdbc.OracleConnection的代码如下?



我的主要目标是传递给Oracle连接的新用户名和将其保存在'SESSION'表中,例如'osuser'列,因为我想跟踪数据库用户更改并将其显示在表中。

  @Repository 
public class AuditLogDAOImpl实现AuditLogDAO {

@PersistenceContext(unitName =myUnitName)
EntityManager em;

@Resource(name =dataSource)
DataSource dataSource;

public void init(){

try {
Connection connection = DataSourceUtils.getConnection(dataSource);
OracleConnection oracleConnection =(OracleConnection)连接; //在这里我被抛出异常!

String metrics [] = new String [OracleConnection.END_TO_END_STATE_INDEX_MAX];
metrics [OracleConnection.END_TO_END_CLIENTID_INDEX] =my_new_username;

oracleConnection.setEndToEndMetrics(metrics,(short)0);

java.util.Properties props = new java.util.Properties();
props.put(osuser,newValue);

oracleConnection.setClientInfo(道具);

catch(SQLException e){
e.printStackTrace();



code
$ b

这里是错误日志:

  10:42:29,251信息[标准输出] org.jboss.resource.adapter.jdbc.jdk6.WrappedConnectionJDK6@bcc8cb 
10:42:51,701错误[STDERR] java.lang.ClassCastException:$ Proxy286不能转换为oracle.jdbc.OracleConnection

通常,在这种情况下,我有两个问题:


  • 为什么从Connection连接到OracleConnection失败并且

  • 实现我的意图的最佳方式是什么(我的意思是将新用户名设置为Oracle数据库中的v $ session.osuser?



我使用Oracle 11g,Hibernate(使用实体管理器),数据源通过jndi。



请帮助,谢谢!



编辑:



经过一些改进后,铸造问题仍然存在。



改进:

 连接连接= DataSourceUtils.getConnection(dataSource); 
connect ion =((org.jboss.resource.adapter.jdbc.WrappedConnection)连接).getUnderlyingConnection();
OracleConnection oracleConnection =(OracleConnection)连接;

错误:

  java.lang.ClassCastException:$ Proxy287不能转换为org.jboss.resource.adapter.jdbc.WrappedConnection 


如果您确实需要获得您应该使用的底层Oracle连接:

  if(connection.isWrapperFor(OracleConnection.class)){ 
OracleConnection oracleConnection = connection.unwrap(OracleConnection.class);
} else {
//恢复,不是oracle连接
}



自Java 1.6以来, isWrapperFor unwrap 方法是可用的,并且应该由A / S连接包装。


Why java.sql.Connection cannot be cast to oracle.jdbc.OracleConnection in code below?

My main goal is to pass to Oracle connection new user name and save it in 'SESSION' table in for example 'osuser' column because I want to trace in DB user changes and display it in the table.

@Repository
public class AuditLogDAOImpl implements AuditLogDAO {

    @PersistenceContext(unitName="myUnitName")
    EntityManager em;

    @Resource(name = "dataSource")
    DataSource dataSource;

    public void init() {

        try {
            Connection connection = DataSourceUtils.getConnection(dataSource);
            OracleConnection oracleConnection = (OracleConnection) connection; //Here I got cast exception!

            String metrics[] = new String[OracleConnection.END_TO_END_STATE_INDEX_MAX];
            metrics[OracleConnection.END_TO_END_CLIENTID_INDEX] = "my_new_username";

            oracleConnection.setEndToEndMetrics(metrics, (short) 0);

            java.util.Properties props = new java.util.Properties();
            props.put("osuser", "newValue");

            oracleConnection.setClientInfo(props);

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

Here is error log:

10:42:29,251 INFO  [STDOUT] org.jboss.resource.adapter.jdbc.jdk6.WrappedConnectionJDK6@bcc8cb
10:42:51,701 ERROR [STDERR] java.lang.ClassCastException: $Proxy286 cannot be cast to oracle.jdbc.OracleConnection

Generally I have 2 problem in this case:

  • why cast from Connection to OracleConnection fails and
  • what is the best way to implement my intend (I mean set the new user name to v$session.osuser in Oracle DB?

I work with Oracle 11g, Hibernate (using entity manager), data source via jndi.

Please help, thanks!

EDIT:

After some improvement the problem with casting still exists.

Improvement:

Connection connection = DataSourceUtils.getConnection(dataSource);
connection = ((org.jboss.resource.adapter.jdbc.WrappedConnection)connection).getUnderlyingConnection();
OracleConnection oracleConnection = (OracleConnection) connection;

Error:

java.lang.ClassCastException: $Proxy287 cannot be cast to org.jboss.resource.adapter.jdbc.WrappedConnection

解决方案

The connection you are retrieving is probably a wrapped connection.

If you really need to get the underlying Oracle connection you should use:

if (connection.isWrapperFor(OracleConnection.class)){
   OracleConnection oracleConnection= connection.unwrap(OracleConnection.class);  
}else{
   // recover, not an oracle connection
}

The isWrapperFor and unwrap methods are available since Java 1.6, and should be meaningfully implemented by the A/S connection wrappers.

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

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