问题描述
我一直在尝试从IBM JNDI(WebSphere liberty)解开PostgreSQL连接,但是没有运气,请提供任何帮助:
I have been trying to unwrap PostgreSQL connection from the IBM JNDI (WebSphere liberty ) and have had no luck please any help with that :
Context ctx = new InitialContext();
DataSource dataSource = (DataSource) ctx.lookup("jdbc/indi");
Connection cnx = dataSource.getConnection();
我收到此异常:
java.lang.ClassCastException:
com.ibm.ws.rsadapter.jdbc.v41.WSJdbc41Connection cannot be cast to
org.postgresql.PGConnection
我尝试过:
if(cnx.isWrapperFor(PGConnectionPoolDataSource.class)) {
//unwrap
}
if (cnx.isWrapperFor(org.postgresql.ds.jdbc4.AbstractJdbc4SimpleDataSource.class)) {
//unwrap
}
if (cnx.isWrapperFor(org.postgresql.core.v3.ConnectionFactoryImpl.class)) {
//unwrap
}
if (cnx.isWrapperFor(org.postgresql.jdbc2.AbstractJdbc2Connection.class)) {
//unwrap
}
if (cnx.isWrapperFor(org.postgresql.jdbc3.AbstractJdbc3Connection.class)) {
//unwrap
}
谢谢
推荐答案
如果要将JDBC对象(数据源,连接等)解包到特定于供应商的接口,请在已配置的中将JDBC驱动程序<; datSource>
必须对应用程序类加载器可用.配置将如下所示:
If you would like to unwrap a JDBC object (DataSource, Connection, etc) to a vendor-specific interface, the JDBC driver in the configured <datSource>
must be available to the application classloader. The configuration will look something like this:
<application location="oraclejdbcfat.war" >
<!-- expose the 'DBLib' containing the JDBC driver jar to the app classloader -->
<classloader commonLibraryRef="DBLib"/>
</application>
<library id="DBLib">
<fileset dir="${server.config.dir}/postgresql/" includes="*.jar"/>
</library>
<dataSource jndiName="jdbc/myDS">
<jdbcDriver libraryRef="DBLib"/>
<properties .../>
</dataSource>
从那里,您可以像以前一样解开对象,即:
From there, you can unwrap the object in the same way you were doing before, namely:
DataSource ds = InitialContext.doLookup("jdbc/myDS");
Connection conn = ds.getConnection();
PGConnection pgConn = conn.unwrap(org.postgresql.PGConnection.class);
此外,在< dataSource>
配置上还有一个 enableConnectionCasting
布尔属性,该属性会在 getConnection()
时自动为您调用unwrap.
Also, there is an enableConnectionCasting
boolean attribute on <dataSource>
configuration which will automatically call unwrap for you upon getConnection()
.
<dataSource jndiName="jdbc/myDS" enableConnectionCasting="true">
然后,Java代码会更简单:
Then the java code is a bit simpler:
DataSource ds = InitialContext.lookup("jdbc/indi");
PGConnection pgConn = (PGConnection) ds.getConnection();
这篇关于如何从IBM WSJdbc41Connection解开PostgreSQL连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!