2

P6spy v2 支持 xa 数据源 - P6spy on github

在我的 jboss AS 7 Standalone-full-ha.xml 中,我有以下数据源配置。

<xa-datasource jndi-name="java:jboss/datasources/MyDS" pool-name="v" enabled="true" use-java-context="true" use-ccm="false">
                <xa-datasource-property name="ServerName">
                    localhost
                </xa-datasource-property>
                <xa-datasource-property name="PortNumber">
                    5432
                </xa-datasource-property>
                <xa-datasource-property name="DatabaseName">
                    MyDB
                </xa-datasource-property>
                <driver>p6spy</driver>
                <xa-pool>
                    <min-pool-size>50</min-pool-size>
                    <max-pool-size>150</max-pool-size>
                    <is-same-rm-override>false</is-same-rm-override>
                    <interleaving>false</interleaving>
                    <pad-xid>false</pad-xid>
                    <wrap-xa-resource>false</wrap-xa-resource>
                </xa-pool>
                <security>
                    <user-name>postgres</user-name>
                    <password>postgres</password>
                </security>
                <validation>
                    <validate-on-match>false</validate-on-match>
                    <background-validation>false</background-validation>
                </validation>
                <statement>
                    <share-prepared-statements>false</share-prepared-statements>
                </statement>
            </xa-datasource>
            <drivers>
                <driver name="h2" module="com.h2database.h2">
                    <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
                </driver>
                <driver name="postgresql" module="org.postgresql">
                    <xa-datasource-class>org.postgresql.xa.PGXADataSource</xa-datasource-class>
                </driver>
                <driver name="p6spy" module="com.p6spy">
                    <xa-datasource-class>com.p6spy.engine.spy.P6SpyDriver</xa-datasource-class>
                </driver>
            </drivers>

之后,我将 p6spy-2.0.1.jar 放在 jboss\modules\com\p6spy\main 文件夹中,并定义了 module.xml 文件。此外,我将 spy.properties 文件与 P6spy.jar 一起放入,并在其中添加了启用 postgres 驱动程序的行

realdriver=org.postgresql.Driver

使用所有这些设置,当我启动我的服务器时,我得到以下异常。

    Throwable while attempting to get a new connection: null: javax.resource.ResourceException: Could not create connection
    at org.jboss.jca.adapters.jdbc.xa.XAManagedConnectionFactory.getXAManagedConnection(XAManagedConnectionFactory.java:447)
    at org.jboss.jca.adapters.jdbc.xa.XAManagedConnectionFactory.createManagedConnection(XAManagedConnectionFactory.java:385)
    at org.jboss.jca.core.connectionmanager.pool.mcp.SemaphoreArrayListManagedConnectionPool.createConnectionEventListener(SemaphoreArrayListManagedConnectionPool.java:761)
    at org.jboss.jca.core.connectionmanager.pool.mcp.SemaphoreArrayListManagedConnectionPool.getConnection(SemaphoreArrayListManagedConnectionPool.java:343)
    at org.jboss.jca.core.connectionmanager.pool.AbstractPool.getSimpleConnection(AbstractPool.java:397)
    at org.jboss.jca.core.connectionmanager.pool.AbstractPool.getConnection(AbstractPool.java:365)
    at org.jboss.jca.core.connectionmanager.AbstractConnectionManager.getManagedConnection(AbstractConnectionManager.java:329)
    at org.jboss.jca.core.connectionmanager.tx.TxConnectionManagerImpl.getManagedConnection(TxConnectionManagerImpl.java:368)
    at org.jboss.jca.core.connectionmanager.AbstractConnectionManager.allocateConnection(AbstractConnectionManager.java:464)
    at org.jboss.jca.adapters.jdbc.WrapperDataSource.getConnection(WrapperDataSource.java:129)
    at org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider.getConnection(InjectedDataSourceConnectionProvider.java:67) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcServicesImpl.java:253) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:119) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:71) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2270) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2266) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1735) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:84) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:904) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:889) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:73) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
    at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.createContainerEntityManagerFactory(PersistenceUnitServiceImpl.java:162) [jboss-as-jpa-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.start(PersistenceUnitServiceImpl.java:85) [jboss-as-jpa-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811)
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [rt.jar:1.7.0_40]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.7.0_40]
    at java.lang.Thread.run(Thread.java:724) [rt.jar:1.7.0_40]
Caused by: java.lang.ClassCastException: com.p6spy.engine.spy.P6SpyDriver cannot be cast to javax.sql.XADataSource
    at org.jboss.jca.adapters.jdbc.xa.XAManagedConnectionFactory.getXADataSource(XAManagedConnectionFactory.java:553)
    at org.jboss.jca.adapters.jdbc.xa.XAManagedConnectionFactory.getXAManagedConnection(XAManagedConnectionFactory.java:430)
    ... 30 more

在这一步,我对如何使它工作一无所知。

编辑: 根据 quintonm 的回复,我使用了实现 XADatasource 的 com.p6spy.engine.spy.P6ConnectionPoolDataSource 类。但现在我得到以下异常。

Caused by: javax.resource.ResourceException: Could not find accessor on XADataSource: 
    at org.jboss.jca.adapters.jdbc.xa.XAManagedConnectionFactory.getXADataSource(XAManagedConnectionFactory.java:629)
    at org.jboss.jca.adapters.jdbc.xa.XAManagedConnectionFactory.getXAManagedConnection(XAManagedConnectionFactory.java:430)
    ... 30 more
Caused by: java.lang.NoSuchMethodException: com.p6spy.engine.spy.P6ConnectionPoolDataSource.setDatabaseName(java.lang.String)
    at java.lang.Class.getMethod(Class.java:1655) [rt.jar:1.7.0_40]
    at org.jboss.jca.adapters.jdbc.xa.XAManagedConnectionFactory.getXADataSource(XAManagedConnectionFactory.java:597)

此外,按照 P6spy 文档中所述的通用说明,我在 xa-datasource-properties 中使用了“RealDataSource”属性,但它随后表示不存在此类属性。

4

2 回答 2

4

您当前面临的问题是 JBoss 期望您使用实现 xa-datasource-class 中的 Datasource 接口的类。对于 P6Spy 驱动程序定义,您使用的是实现 java.sql.Driver 的 P6SpyDriver。您想使用 com.p6spy.engine.spy.P6ConnectionPoolDataSource 而不是使用 P6SpyDriver。

P6Spy 在 JNDI 中对 DataSource 实现的支持与使用 Driver 类略有不同。您必须设置第二个 JDNI 数据源,而不是修改 JDBC URL。应用程序连接的将是 P6Spy 数据源。该 DataSource 将成为真实数据源的代理。可以在此处找到设置说明。

注意:说明说您应该使用 P6DataSource。但是,由于您需要 XA 支持,您将需要使用 P6ConnectionPoolDataSource。

为了更好地理解这是如何工作的,您可以查看 XADataSources 的测试 - XADataSourceTest

如果您在使其正常工作时遇到任何问题,只需向 p6spy-users@googlegroups.com 发送消息或输入问题

于 2014-03-27T02:45:17.050 回答
2

好吧,更新问题后,您的配置应该如下所示。没有给它一个测试尝试,但一般来说,你需要通过 p6spy 代理你现有的一个(postgres 一个):

<xa-datasource jndi-name="java:jboss/datasources/MyDSReal" pool-name="v" enabled="true" use-java-context="true" use-ccm="false">
            <xa-datasource-property name="ServerName">
                localhost
            </xa-datasource-property>
            <xa-datasource-property name="PortNumber">
                5432
            </xa-datasource-property>
            <xa-datasource-property name="DatabaseName">
                MyDB
            </xa-datasource-property>
            <driver>postgresql</driver>
            <xa-pool>
                <min-pool-size>50</min-pool-size>
                <max-pool-size>150</max-pool-size>
                <is-same-rm-override>false</is-same-rm-override>
                <interleaving>false</interleaving>
                <pad-xid>false</pad-xid>
                <wrap-xa-resource>false</wrap-xa-resource>
            </xa-pool>
            <security>
                <user-name>postgres</user-name>
                <password>postgres</password>
            </security>
            <validation>
                <validate-on-match>false</validate-on-match>
                <background-validation>false</background-validation>
            </validation>
            <statement>
                <share-prepared-statements>false</share-prepared-statements>
            </statement>
        </xa-datasource>
<xa-datasource jndi-name="java:jboss/datasources/MyDS" pool-name="v" enabled="true" use-java-context="true" use-ccm="false">
            <driver>p6spy</driver>
            <xa-pool>
                <min-pool-size>50</min-pool-size>
                <max-pool-size>150</max-pool-size>
                <is-same-rm-override>false</is-same-rm-override>
                <interleaving>false</interleaving>
                <pad-xid>false</pad-xid>
                <wrap-xa-resource>false</wrap-xa-resource>
            </xa-pool>
            <security>
                <user-name>postgres</user-name>
                <password>postgres</password>
            </security>
            <validation>
                <validate-on-match>false</validate-on-match>
                <background-validation>false</background-validation>
            </validation>
            <statement>
                <share-prepared-statements>false</share-prepared-statements>
            </statement>
        </xa-datasource>

        <drivers>
            <driver name="h2" module="com.h2database.h2">
                <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
            </driver>
            <driver name="postgresql" module="org.postgresql">
                <xa-datasource-class>org.postgresql.xa.PGXADataSource</xa-datasource-class>
            </driver>
            <driver name="p6spy" module="com.p6spy">
                <xa-datasource-class>com.p6spy.engine.spy.P6ConnectionPoolDataSource</xa-datasource-class>
            </driver>
        </drivers>

此外,如果您对默认配置没问题,我认为您不需要 spy.properties 文件。因为如果采用驱动方式(而不是像现在这样的数据源),则需要 RealDriver 属性。

更新

报告为有问题的已删除部分:

            <xa-datasource-property name="RealDataSource">
                java:jboss/datasources/MyDSReal
            </xa-datasource-property>                

如果这是 DS 配置中的问题 => 让我们以不同的方式配置它,您可以选择任何可用的:

  • 系统属性
  • 环境变量或
  • 间谍属性

您需要在哪里设置:

realdatasource=java:jboss/datasources/MyDSReal

更新 2

我试了一下,似乎 p6spy 不支持 JBoss / Wildfly 的 XA 数据源(还)。更多详情请查看官方文档: http: //p6spy.github.io/p6spy/2.0/install.html

于 2014-04-02T13:22:49.740 回答