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的答复,我使用了com.p6spy.engine.spy.P6ConnectionPoolDataSource类,该类实现了XADatasource。但是现在我收到以下异常。

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'属性,但随后它说不存在这样的属性。

最佳答案

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

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

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

为了更好地了解它是如何工作的,可以看一下XADataSources的测试-XADataSourceTest

如果您无法解决此问题,只需将消息发布到[email protected]或输入issue

关于jpa-2.0 - 尝试使用Jboss AS 7和potgres使用xa-datasource配置P6Spy 2.0时,无法将P6SpyDriver强制转换为javax.sql.XADataSource异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22659924/

10-13 01:34