【问题标题】:How to include Websphere plugin jars in WAS classpath如何在 WAS 类路径中包含 Websphere 插件 jar
【发布时间】:2015-08-11 23:05:02
【问题描述】:

我面临与 WAS 插件文件夹中的 jar 相关的类路径问题。我的应用程序具有 IBM 特定代码,可以使用 com.ibm.ws.runtime jar 进行编译,如下所述。

位置:C:\Program Files\IBM\Websphere\AppServer\Plugins

源代码:

对象 obj = ((com.ibm.ws.rsadapter.jdbc.WSJdbcUtil.getNativeConnection((com.ibm.ws.rsadapter.jdbc.WSJdbcConnection)connect )));

这两个类都在 com.ibm.ws.runtime 中可用

通过在构建过程的类路径中包含 IBM runtime.jar 成功编译,但在 WAS 中部署后,我得到 ClassNotFoundException。谁能告诉我,如何在 WAS 的类路径中包含该插件文件夹,这样我就不会得到 ClassNotFoundException。我在 JVM 类路径中只添加了 runtime.jar,但它会抛出错误,因为它依赖于 IBM 的其他 jar。

错误: 原因:java.lang.ClassNotFoundException: com.ibm.ws.rsadapter.jdbc.WSJdbcConnection

更新: 目前它与 Jboss 服务器完美配合。代码如下。我的目标是提供与 Webpshere 相同的规定。

调用方法:

Connection connect = null;
connect = mDataSrc.getConnection();
unlockJDBC(connect);

private void unlockJDBC(Connection connect)
{
//This bit is JBoss specific but we are trying to avoid importing JBoss JAR files so we use reflection instead.

if (connect.getClass().getName().equals("org.jboss.resource.adapter.jdbc.WrappedConnection") || connect.getClass().getSuperclass().getName().equals("org.jboss.resource.adapter.jdbc.WrappedConnection"))
{
Method method = null;
try{
    method = connect.getClass().getMethod("getUnderlyingConnection",null);
    Connection conn = (Connection)method.invoke(connect, null);
    if (conn != null){
        connect = conn;
    }
}
catch (InvocationTargetException e){
    mLogger.severe(e.getTargetException().getMessage());
}
catch (Exception e){
    mLogger.severe(e.toString());
}
}
if (connect instanceof ExtEmbeddedConnection){
ExtEmbeddedConnection embConnect = (ExtEmbeddedConnection)connect;
try{
    embConnect.unlock("unlock");
}
catch (Exception e){
    mLogger.severe(e.toString());
}
 }

【问题讨论】:

    标签: java jdbc websphere websphere-8


    【解决方案1】:

    获得底层连接的推荐方法是使用适当的 JDBC API 并将其解包,如下所示(不使用 WebSphere 内部类):

    Context ic = new InitialContext();
    DataSource ds = (DataSource)ic.lookup("jdbc/OracleDS");
    Connection conn = ds.getConnection();
    
    // Returns true if this either implements the interface argument
    // or is directly or indirectly a wrapper for an object that does.
    if (conn.isWrapperFor(oracle.jdbc.OracleConnection.class)) {
        // Returns an object that implements the given interface to
        // allow access to non-standard methods, or standard methods
        // not exposed by the proxy.
        oracle.jdbc.OracleConnection oraCon = conn.unwrap(oracle.jdbc.OracleConnection.class);
        // Do some Oracle-specific work here.
    }
    

    更多详情请查看WebSphere Application Server and the JDBC 4.0 Wrapper Pattern

    更新
    回应 cmets。我不推荐这样做,尽管它在 WAS 8.5.5 中运行良好,所以请修复您的类路径并删除您在此处添加或与应用程序一起打包的任何 WebSphere 相关 jar:

            Connection connection = myDs.getConnection();
            System.out.println("connection: " + connection.getClass().getName());
            WSJdbcConnection conn = null;
            if(connection instanceof WSJdbcConnection) {
                System.out.println("WSJdbcConnection");
                conn = (WSJdbcConnection) connection;
                Object obj = WSJdbcUtil.getNativeConnection(conn);
                System.out.println("native: " + obj.getClass().getName());
            }
    

    带输出:

    [8/11/15 16:55:10:165 CEST] 0000009a SystemOut     O connection: com.ibm.ws.rsadapter.jdbc.WSJccSQLJPDQConnection
    [8/11/15 16:55:10:165 CEST] 0000009a SystemOut     O WSJdbcConnection
    [8/11/15 16:55:10:165 CEST] 0000009a SystemOut     O native: com.ibm.db2.jcc.am.ef
    

    【讨论】:

    • 嗨,Gas,我正在使用品牌 JDBC 连接。所以,我不能直接使用oracle jar。我有我的产品品牌 jar,比如 myproductOracle.jar。我正在尝试的实际代码是 ((com.myproduct.jdbcx.oraclebase.BaseConnectionWrapper)(com.ibm.ws.rsadapter.jdbc.WSJdbcUtil.getNativeConnection((com.ibm.ws.rsadapter.jdbc.WSJdbcConnection)connect)) ).unlock ("密码");.如上所述,我必须将我的对象从 IBM 连接对象转换为我的产品对象。有什么方法可以在 WAS 的类路径中包含 plugins 文件夹。
    • @Naresh 该 jar 包含在类路径中,您无需执行任何操作。 I have added only runtime.jar in JVM classpath - 如果您在任何地方手动添加了该 jar,请将其从类路径或应用程序中删除。这给你带来了问题。我可以使用该类而无需任何更改。
    • @Naresh 您也可以尝试将它与您的包装器一起使用,例如:conn.isWrapperFor(com.myproduct.jdbcx.oraclebase.BaseConnectionWrapper.class)。此处仅以 Oracle 为例。
    • 感谢 Gas 和 Bruce T 的出色技术指导。我从 2 天开始就 OOO 了。我会尽快回复您。
    • 嗨,Gas,我这边有点延迟。我尝试过 isWrapperFor 但没有用。 Bcoz,“连接 = mDataSrc.getConnection();”返回我 WAS“com.ibm.ws.rsadapter.jdbc.WSJdbcConnection”对象。所以“connect.isWrapperFor(com.myproduct.jdbcx.oraclebase.BaseConnectionWrapper.class)”总是假的。如果我错了,请告诉我..
    【解决方案2】:

    Websphere 使用 OSGI 来限制内部类对应用程序的可见性。这可能就是这里发生的事情。 OSGI 可能在您编译它时并没有发挥作用,但是当您尝试运行它时它就会发挥作用。如果我在 v7 的 servlet 中执行此操作,

    ps.println("trying to new up a WSJdbcConnection");
    Object o = com.ibm.ws.rsadapter.jdbc.WSJdbcConnection.class.newInstance();
    ps.println("got it")
    

    我得到一个 IllegalAccessException,所以它不可见。

    正如 Gas 所说,如果您可以使用非内部的东西,那么您应该能够访问它。一般来说, com.ibm.websphere - api,com.ibm.wsspi - spi,com.ibm.ws - 内部

    【讨论】:

      猜你喜欢
      • 2012-02-15
      • 2023-03-18
      • 2010-09-16
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多