WSJDBCConnection does not wrap objects of type Oracle jdbc Connection

吃可爱长大的小学妹 提交于 2019-11-29 12:11:24

I just tested this on WebSphere Liberty and the following code worked for me:

@Resource(lookup = "jdbc/oracle")
private DataSource ds;

// ...

Connection conn = ds.getConnection();
OracleConnection oracleConn = conn.unwrap(oracle.jdbc.OracleConnection.class);

My server.xml looks like this:

<dataSource jndiName="jdbc/oracle">
    <jdbcDriver libraryRef="oracleLib"/>
    <properties.oracle URL="${jdbc.URL}" user="${jdbc.user}" password="${jdbc.password}"/>
</dataSource>

<library id="oracleLib">
    <fileset dir="${server.config.dir}/oracle"/>
</library>

<application location="myApp.war" >
    <classloader commonLibraryRef="oracleLib"/>
</application>

The important thing to note here is the use of commonLibraryRef on the <classloader> element. If you use privateLibraryRef it will not work because the app and server-defined datasource will use isolated classloaders to load the Oracle JDBC classes.

If this answer isn't helpful to you, please update your question with your server.xml configuration, and also how you are obtaining an instance of your DataSource.

Stanciu Marian Madalin

If someone has this problem here is my solution.

My mistake was that I was deploying my application to the dropins folder and if you define an application/webApplication in your server.xml it is ignored. I deployed it somewhere else, set this new location to the application tag and also added the <classloader> in server.xml, left the ojdbc jar as provided in pom.xml and it works now.

But still I had a problem when I was running the application locally, because the ojdbc jar was provided in pom.xml.
My solution was to use reflection to get the system class loader and load the jar at runtime for the local profile.

Hope this helps someone.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!