1 Reply Latest reply on Sep 20, 2011 9:03 AM by stevemaring

    Reference cannot be cast to DataSource w/ standalone app client

    stevemaring

      I am trying to unit test some JPA2 DAO classes I wrote using Maven.  The persistence.xml looks like this:

       

      <?xml version="1.0" encoding="UTF-8"?>

      <persistence version="2.0"

          xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

          xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

         

          <persistence-unit name="lifesmartEntityManager" transaction-type="RESOURCE_LOCAL">

              <provider>org.hibernate.ejb.HibernatePersistence</provider>

              <non-jta-data-source>MyDS</non-jta-data-source>

              <properties>

                  <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />

                  <property name="hibernate.show-sql" value="true" />

              </properties>

          </persistence-unit>

         

      </persistence>

       

      I have deployed the DataSource to a running instance of AS 6.1.0.Final and confirmed that the global JNDIView has:

       

      +- MyDS (class: javax.sql.DataSource)

       

      When I try to run my tests, which rely on Hibernate EntityManager to lookup and associate the DataSource with the persistence-unit, I get:

       

       

      08:05:12,185 ERROR [DatasourceConnectionProvider] Could not find datasource: MyDS

      java.lang.ClassCastException: javax.naming.Reference cannot be cast to javax.sql.DataSource

              at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:75)

              at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:143)

              at org.hibernate.ejb.InjectionSettingsFactory.createConnectionProvider(InjectionSettingsFactory.java:51)

              at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:91)

              at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2833)

              at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2829)

              at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1840)

              at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:902)

              at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:57)

              at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:48)

       

       

      I am certain that it is not truly a naming problem, because when I change the name to something bogus in the persistence.xml and run the test again, the error is different:

       

      08:25:34,525 ERROR [DatasourceConnectionProvider] Could not find datasource: YourDS

      javax.naming.NameNotFoundException: YourDS not bound

              at org.jnp.server.NamingServer.getBinding(NamingServer.java:771)

       

       

      Some have suggested this is really a classpath issue and I have started adding standalone remote client oriented libs in a bit of a shotgun approach.  So far, nothing has helped.  My test classpath, at this point, contains these declared artifacts, and the dependencies declared in their poms:

       

      slf4j-log4j12

      testng

      jnp-client

      jboss-remoting

      jbosssx-client

      jboss-security-spi

      hibernate-validator

      jboss-logging

      jboss-ejb3-vfs-spi

      jboss-ejb3-core

      jboss-kernel

      jboss-dependency

      jboss-classloading-vfs

      jboss-classloading

      jboss-classloader

      jboss-service-binding-core

      jboss-metadata-client

      jboss-transaction-api_1.1_spec

      jboss-jmx

      jboss-integration

      jboss-vfs

      jboss-reflect

      jboss-common-core

      jboss-serialization

      hibernate-jpa-2.0-api

      hibernate-entitymanager

       

       

      I am, perhaps, still missing an artifact?

       

      -Steve Maring

      Tampa, FL

        • 1. Re: Reference cannot be cast to DataSource w/ standalone app client
          stevemaring

          I cracked open the MANIFEST.MF on the jbossall-client.jar and came to the realization that my shotgun was not NEARLY large enough!  I found a much more effective weapon in the form of:

           

          <dependency>

                      <groupId>org.jboss.jbossas</groupId>

                      <artifactId>jboss-as-client</artifactId>

                      <version>6.1.0.Final</version>

                      <type>pom</type>

                      <scope>test</scope>

          </dependency>

           

          Simple, clean, and effective!

           

          Issue SOLVED