【问题标题】:Can't make MXBean to connect to database through JConsole无法使 MXBean 通过 JConsole 连接到数据库
【发布时间】:2023-03-14 00:18:01
【问题描述】:

我只是希望是否有人看到并解决了这个问题。我有一个 MBean 和一个 MXBean 类。两者都使用相同的服务类通过 DAO 类访问数据库。我正在使用 spring 3.0 来初始化这些类并使用 JConsole 来测试这些 bean。这两个 bean 具有相同的方法名称来访问它们的服务类,比如说 methodA()。但是连接oracle数据库时,只有MBean类返回数据。另一个 MXBean 类会产生一些错误。


以下是错误


EL Severe]: 2012-05-18 10:37:54.134--ServerSession(1992289)--Thread(Thread[RMI TCP Connection(6)-10.208.138.241,5,RMI Runtime])--java.lang.IllegalArgumentException: interface oracle.ucp.jdbc.LabelableConnection is not visible from class loader
    at java.lang.reflect.Proxy.getProxyClass(Proxy.java:353)
    at java.lang.reflect.Proxy.newProxyInstance(Proxy.java:581)
    at oracle.ucp.jdbc.proxy.ConnectionProxyFactory.createConnectionProxy(ConnectionProxyFactory.java:78)
    at oracle.ucp.jdbc.PoolDataSourceImpl.getConnection(PoolDataSourceImpl.java:658)
    at oracle.ucp.jdbc.PoolDataSourceImpl.getConnection(PoolDataSourceImpl.java:613)
    at oracle.ucp.jdbc.PoolDataSourceImpl.getConnection(PoolDataSourceImpl.java:607)
    at org.springframework.jdbc.datasource.DelegatingDataSource.getConnection(DelegatingDataSource.java:83)
    at com.trgr.cobalt.dataorch.datasource.BaseDODataSource.getRawConnection(BaseDODataSource.java:76)
    at com.trgr.cobalt.dataorch.datasource.BaseDODataSource.getConnection(BaseDODataSource.java:46)
    at com.trgr.cobalt.dataorch.datasource.BaseDODataSource.getConnection(BaseDODataSource.java:35)
    at org.eclipse.persistence.sessions.JNDIConnector.connect(JNDIConnector.java:126)
    at org.eclipse.persistence.sessions.JNDIConnector.connect(JNDIConnector.java:94)
    at org.eclipse.persistence.sessions.DatasourceLogin.connectToDatasource(DatasourceLogin.java:162)
    at org.eclipse.persistence.internal.databaseaccess.DatasourceAccessor.connectInternal(DatasourceAccessor.java:327)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.connectInternal(DatabaseAccessor.java:291)
    at org.eclipse.persistence.internal.databaseaccess.DatasourceAccessor.connect(DatasourceAccessor.java:415)
    at org.eclipse.persistence.sessions.server.ConnectionPool.buildConnection(ConnectionPool.java:155)
    at org.eclipse.persistence.sessions.server.ExternalConnectionPool.startUp(ExternalConnectionPool.java:118)
    at org.eclipse.persistence.sessions.server.ServerSession.connect(ServerSession.java:495)
    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.login(DatabaseSessionImpl.java:627)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:230)
    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:389)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.getServerSession(EntityManagerFactoryImpl.java:164)



这是我的弹簧配置

<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter"
    lazy-init="false">
    <property name="namingStrategy" ref="namingStrategy"></property>
    <property name="assembler" ref="assembler"></property>
    <property name="autodetect" value="true" />
</bean>

<bean id="attributeSource"
    class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource" />
<bean id="assembler"
    class="org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler">
    <property name="attributeSource" ref="attributeSource" />
</bean>
<bean id="namingStrategy"
    class="org.springframework.jmx.export.naming.MetadataNamingStrategy">
    <property name="attributeSource" ref="attributeSource" />
</bean>


<bean class="myclass.jmx.DocumentMBean"
    p:schedulerService-ref="documentService" />

<bean class="myclass.jmx.DocumentMXBean"
    p:schedulerService-ref="documentService" />

DocumentMBean 是一个常规的 MBean。 DocumentMXBean 是一个 MXBean。这两个 bean 使用相同的 documentService 服务类,它使用相同的 DAO 类从 Oracle 数据库中获取数据。 DocumentMBean 正确返回数据。 DocumentMXBean 有上面提到的错误。

有谁知道为什么类加载器看不到 oracle.ucp.jdbc.LabelableConnection 类?仅当我执行 MXBean 时才会发生这种情况。我的 MBean 正确返回数据。我的 WEB-INF/lib 文件夹中有包含该类的 jar 文件。这部署在Tomcat中。我从 Eclipse 中启动 Tomcat。

更新 1:

我能够通过将那些“不可见”类的 jar 文件添加到 Eclipse 内的 Tomcat 类路径来临时解决此问题。似乎为了加载 MBean,JConsole/java 正在使用我的 Web 应用程序类加载器,它可以访问类加载器所需的所有库。但是在加载 MXBeans 时,JConsole/java 使用 Tomcat 的类加载器。

我的问题是:在加载 MBean 或 MXBean 时,有没有办法强制 Tomcat/Eclipse/Java 使用相同的类加载器(这是我的 Web 应用程序类加载器)?

更新 2:

我发现在加载我的 Web 应用程序时,Spring 使用 Web 应用程序的类加载器,而 Tomcat 在加载任何 MXBean 时,它使用 JVM 类加载器,其中没有我的 oracle 类路径。因此,变通的解决方案是使用我的类的类加载器而不是来自 JVM 的类加载器。代码应该是:


    try
    {
    // your codes here

    // Get the current class-loader. This might be the class-loader from Tomcat
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();

    // Set the class-loader to be the one from the DO and not the one from Tomcat
    Thread.currentThread().setContextClassLoader(getClass().getClassLoader());

    // your codes here
    }
    catch (Exception e)
    {
       // your codes here
    }
    finally
    {
       // Remember to set back the original class-loader
       Thread.currentThread().setContextClassLoader(contextClassLoader);
    }

【问题讨论】:

    标签: java spring tomcat jmx jconsole


    【解决方案1】:

    MXBean 可能是由容器类加载器加载的,而不是由 Web 应用程序加载的。如果驱动程序位于应用程序 lib 目录中,那么这将导致您看到的问题。

    如果是这种情况,请将驱动程序移至 tomcat/lib 目录。

    【讨论】:

    • 感谢您的回复。此任务的要求要求我在应用程序的 lib 文件夹中拥有所有 jar 文件。有没有办法让 MXBean 访问这些 jar 文件而不将它们移动到 tomcat/lib 文件夹中?顺便说一句,我尝试了您的建议,但没有奏效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-17
    • 2015-01-08
    • 2010-10-02
    • 2019-02-03
    • 2012-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多