title: P6SpyDriver cannot be cast to javax.sql.XADataSource exception while trying to configure P6Spy 2.0 with xa-datasource using Jboss AS 7 and potgres
body: <p>P6spy v2 has a support for xa datasources - <a href="https://github.com/p6spy/p6spy" rel="nofollow">P6spy on github</a></p>
  
  <p>In my jboss AS 7 standalone-full-ha.xml, i have the following datasource configurations.</p>
  
  <pre><code>&lt;xa-datasource jndi-name="java:jboss/datasources/MyDS" pool-name="v" enabled="true" use-java-context="true" use-ccm="false"&gt;
                  &lt;xa-datasource-property name="ServerName"&gt;
                      localhost
                  &lt;/xa-datasource-property&gt;
                  &lt;xa-datasource-property name="PortNumber"&gt;
                      5432
                  &lt;/xa-datasource-property&gt;
                  &lt;xa-datasource-property name="DatabaseName"&gt;
                      MyDB
                  &lt;/xa-datasource-property&gt;
                  &lt;driver&gt;p6spy&lt;/driver&gt;
                  &lt;xa-pool&gt;
                      &lt;min-pool-size&gt;50&lt;/min-pool-size&gt;
                      &lt;max-pool-size&gt;150&lt;/max-pool-size&gt;
                      &lt;is-same-rm-override&gt;false&lt;/is-same-rm-override&gt;
                      &lt;interleaving&gt;false&lt;/interleaving&gt;
                      &lt;pad-xid&gt;false&lt;/pad-xid&gt;
                      &lt;wrap-xa-resource&gt;false&lt;/wrap-xa-resource&gt;
                  &lt;/xa-pool&gt;
                  &lt;security&gt;
                      &lt;user-name&gt;postgres&lt;/user-name&gt;
                      &lt;password&gt;postgres&lt;/password&gt;
                  &lt;/security&gt;
                  &lt;validation&gt;
                      &lt;validate-on-match&gt;false&lt;/validate-on-match&gt;
                      &lt;background-validation&gt;false&lt;/background-validation&gt;
                  &lt;/validation&gt;
                  &lt;statement&gt;
                      &lt;share-prepared-statements&gt;false&lt;/share-prepared-statements&gt;
                  &lt;/statement&gt;
              &lt;/xa-datasource&gt;
              &lt;drivers&gt;
                  &lt;driver name="h2" module="com.h2database.h2"&gt;
                      &lt;xa-datasource-class&gt;org.h2.jdbcx.JdbcDataSource&lt;/xa-datasource-class&gt;
                  &lt;/driver&gt;
                  &lt;driver name="postgresql" module="org.postgresql"&gt;
                      &lt;xa-datasource-class&gt;org.postgresql.xa.PGXADataSource&lt;/xa-datasource-class&gt;
                  &lt;/driver&gt;
                  &lt;driver name="p6spy" module="com.p6spy"&gt;
                      &lt;xa-datasource-class&gt;com.p6spy.engine.spy.P6SpyDriver&lt;/xa-datasource-class&gt;
                  &lt;/driver&gt;
              &lt;/drivers&gt;
  </code></pre>
  
  <p>After that, i put the p6spy-2.0.1.jar in jboss\modules\com\p6spy\main folder and also defined the module.xml file. Also, i put the spy.properties file alongwith the P6spy.jar and in that added the line to enable postgres driver</p>
  
  <pre><code>realdriver=org.postgresql.Driver
  </code></pre>
  
  <p>With all these settings, when i start my server, i get the following exception.</p>
  
  <pre><code>    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.&lt;init&gt;(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
  </code></pre>
  
  <p>At this step i am clueless as to how should i make it work.</p>
  
  <p><strong>Edit:</strong>
  As per quintonm's reply, i used com.p6spy.engine.spy.P6ConnectionPoolDataSource class which implements XADatasource. But now i am getting the following exception.</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>Also, following the generic instructions stated in P6spy's documentation, i used the 'RealDataSource' property in xa-datasource-properties but it then says no such property exist. </p>