views:

271

answers:

1

Hello,

I need to set an application context through Hibernate. I found there is a method setApplicationContext on oracle.jdbc.internal.OracleConnection. I wrote a test, in which I was getting the Oracle connection from the Hibernate session and it worked fine. However, when I moved the code to my application running under JBoss where connections are obtained from the pool the solution won't work. The error is: $Proxy51 cannot be cast to oracle.jdbc.internal.OracleConnection. (1) How can I get the internal connection in that environment? (2) Is there a better way to set an application context through Hibernate (docs don't say anything about it).

Thanks so much.

Kris

A: 

hi, u r right the hibernate gives u the proxy connection not the actual connection. so if u will try to set the application context on proxy connection then it will give u class case exception. however you can use the following snippet to achieve the same.

        oracle.jdbc.driver.OracleConnection nativeOraConn = null;
        oracle.jdbc.OracleConnection connection = null;
        try {
            java.sql.Connection connect = org.hibernate.jdbc.BorrowedConnectionProxy.getWrappedConnection(conn);
            connection = (oracle.jdbc.OracleConnection)connect;
            connection = connection.physicalConnectionWithin();
            nativeOraConn=  (oracle.jdbc.driver.OracleConnection)connection;
            nativeOraConn.setApplicationContext("context", "context", valueToSet);
        } catch (Exception e) {
           e.printStackTrace();
        }

I think that this should work.

Mrityunjay