1

Unable to get an Oracle connection.

I am using the following piece of code in order to get an oracle connection in a custom smart service plugin :-

public static Connection openNewConnection(String url, String username, String password) throws ClassNotFoundException, SQLException {

    DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
    Connection connection = DriverManager.getConnection(url, username, password);
    return connection;
}

It works perfectly fine as a standalone, but when used inside JBoss, it gives "ClassCastExcepton" with the following message :-

ClassCastException : "oracle.jdbc.driver.T4CConnection cannot be cast to oracle.jdbc.OracleConnection".

I have used this with ojdbc5.jar, ojdbc6.jar, ojdbc7.jar and ojdbc14.jar files respectively.

Any clues to this issue ?

yash
  • 2,101
  • 2
  • 23
  • 32
user6276653
  • 140
  • 1
  • 2
  • 11
  • This may help you! http://stackoverflow.com/questions/15483356/connection-cannot-be-cast-to-oracle-jdbc-oracleconnection – ankit dave Sep 19 '16 at 10:39

2 Answers2

0

According to "Display Name is missing's" answer on this Error casting T4CConnection to OracleConnection , ojdbcN.jar causes a conflict that you have to resolve by re-referencing correctly.

Community
  • 1
  • 1
FreeMan
  • 1,417
  • 14
  • 20
0

The class oracle.jdbc.driver.T4CConnection extends the class oracle.jdbc.driver.PhysicalConnection, which in turn implements the interface oracle.jdbc.OracleConnection. So it would seem that the cast should succeed.

The reason it does not can only be a class loader problem, i.e. there are two jar files with the same classes involved or more specifically: ojdbcN.jar is on the class path twice, most likely somewhere in the JBoss installation in in your .ear/.war file. And the cast fails because the source and target class and interfaces are in two different jar files.

Fix your class path. For JBoss, ojdbc7.jar needs to go into a shared JBoss directory and must not be included in your .war or .ear file.

Codo
  • 75,595
  • 17
  • 168
  • 206