Results 1 to 10 of 21

Thread: Specification violation [EJB3 JPA 6.2.1.2] -You have not defined non-jta-data-source

Hybrid View

  1. #1
    Join Date
    Sep 2010
    Posts
    19

    Default Specification violation [EJB3 JPA 6.2.1.2] -You have not defined non-jta-data-source

    Hello friends,

    I am trying to create a web application using Spring 3.0.3, JPA 1.0, Hibernate 3, zkoss 5 and JBOSS 5.1 server with jdk support.

    Following is list of jar files in my /WEB-INF/lib directory
    Code:
    commons-dbcp-all-1.3.jar
    commons-io.jar
    org.springframework.*-3.0.3.RELEASE.jar
    zcommon.jar
    zcommons-el.jar
    zhtml.jar
    zk.jar
    zkplus.jar
    zul.jar
    zweb.jar

    Following is my /WEB-INF/web.xml file

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
      <display-name>zk5</display-name>
      
      <!-- Spring -->
        
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        
        <listener>
            <!-- The Spring RequestContextLister uses thread bound variables so to 
            use this Spring freatures requires that we apply <disable-event-thread /> 
            within zk.xml -->
            <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
        </listener>
        
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:dataSourceContext.xml,classpath:spring-context.xml</param-value>
        </context-param>
        
        <!-- Spring -->
        
      <listener>
          <description>
          Used to cleanup when a session is destroyed</description>
          <display-name>
          
          ZK Session cleaner</display-name>
          
          <listener-class>org.zkoss.zk.ui.http.HttpSessionListener</listener-class>
      </listener>
      <servlet>
          <description>
          The ZK loader for ZUML pages</description>
          <servlet-name>zkLoader</servlet-name>
          <servlet-class>
          org.zkoss.zk.ui.http.DHtmlLayoutServlet</servlet-class>
          <init-param>
              <param-name>update-uri</param-name>
              <param-value>/zkau</param-value>
          </init-param>
          <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet>
          <description>
          The asynchronous update engine for ZK</description>
          <servlet-name>auEngine</servlet-name>
          <servlet-class>
          org.zkoss.zk.au.http.DHtmlUpdateServlet</servlet-class>
      </servlet>
      <servlet-mapping>
          <servlet-name>zkLoader</servlet-name>
          <url-pattern>*.zul</url-pattern>
      </servlet-mapping>
      <servlet-mapping>
          <servlet-name>zkLoader</servlet-name>
          <url-pattern>*.zhtml</url-pattern>
      </servlet-mapping>
      <servlet-mapping>
          <servlet-name>auEngine</servlet-name>
          <url-pattern>/zkau/*</url-pattern>
      </servlet-mapping>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
        <welcome-file>index.zul</welcome-file>
      </welcome-file-list>
    </web-app>

    Following is my /src/spring-context.xml file

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        ">
        
        <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
        
        <!-- import the dataSource definition -->
        <import resource="dataSourceContex.xml"/>
        
        <!--
            Activates a load-time weaver for the context. Any bean within the context that
            implements LoadTimeWeaverAware (such as LocalContainerEntityManagerFactoryBean)
            will receive a reference to the autodetected load-time weaver.
        -->
        <context:load-time-weaver/>
        
        <bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
        
        <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
        
        
        <!-- JPA primary EntityManagerFactory -->
          <bean id="entityManagerFactory" lazy-init="true"
              class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
              p:persistenceUnitName="ExamplePU"
              p:jpaVendorAdapter-ref="jpaVendorAdapter"
              p:jpaDialect-ref="jpaDialect"
              p:dataSource-ref="dataSource"/>
    
    
        
        
        <bean id="transactionManager"
            class="org.springframework.orm.jpa.JpaTransactionManager" 
            p:entity-manager-factory-ref="entityManagerFactory"
        />
        
        <!-- 
        JPA config   
        -->    
        
        <tx:annotation-driven />
        
        <!-- 
        JPA helpers
        --> 
        
        <bean id="basicDao" class="org.zkforge.zktodo2.BasicDao" />
    
        <!-- 
        First article classes
        --> 
    
        <bean id="reminderService" class="org.zkforge.zktodo2.ReminderService" 
            p:basicDao-ref="basicDao"
        />
    
        <bean id="toDoControllerV1" class="org.zkforge.zktodo2.ZkToDoControllerV1" 
            p:reminderService-ref="reminderService" scope="prototype"
        />
    
        <!-- 
        Data binding enhancement classes
        --> 
    
        <!-- 
        Note that the following bean is session scoped. 
        --> 
        <bean id="toDoModel" class="org.zkforge.zktodo2.ZkToDoModelImpl" 
                p:reminderService-ref="reminderService" scope="session">
            <!-- 
            // scoped proxy is commented out as only referenced by a prototype bean
            // if you wanted to pass this bean to a singleton you need to include
            // the following configurration.   
            <aop:scoped-proxy proxy-target-class="false"/>
            -->    
        </bean>
    
        <bean id="toDoControllerV2" class="org.zkforge.zktodo2.ZkToDoControllerV2" 
            p:toDoModel-ref="toDoModel" scope="prototype"
        />
        
    </beans>


    Following is my /src/dataSourceContext.xml file

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:util="http://www.springframework.org/schema/util"
        xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
        ">
        
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
                p:driverClassName="org.hsqldb.jdbcDriver" p:url="jdbc:hsqldb:mem:zktodo"
                p:username="sa" p:password=""/>
        
     
    
    </beans>

    Following is my /src/META-INF/persistence.xml file

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="1.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_1_0.xsd">
      <persistence-unit name="ExamplePU" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
          <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
          <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
          <property name="hibernate.showsql" value="true"/>
          <property name="hibernate.cache.use_second_level_cache" value="false"/>
        </properties>
      </persistence-unit>
    </persistence>

    JOBSS server gives me following error

    Code:
    13:27:02,250 INFO  [PersistenceUnitDeployment] Starting persistence unit persistence.unit:unitName=#ExamplePU
    13:27:02,265 ERROR [AbstractKernelController] Error installing to Start: name=persistence.unit:unitName=#ExamplePU state=Create
    java.lang.RuntimeException: Specification violation [EJB3 JPA 6.2.1.2] - You have not defined a non-jta-data-source for a RESOURCE_LOCAL enabled persistence context named: ExamplePU
    	at org.jboss.jpa.deployment.PersistenceUnitInfoImpl.<init>(PersistenceUnitInfoImpl.java:124)
    	at org.jboss.jpa.deployment.PersistenceUnitDeployment.start(PersistenceUnitDeployment.java:275)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    Can any one help me in solving the problem.
    Thanks.

  2. #2
    Join Date
    Jul 2010
    Location
    Venice, Italy
    Posts
    709

    Default

    Add following line to persistence.xml:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="1.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_1_0.xsd">
      <persistence-unit name="ExamplePU" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
          <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
          <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
          <property name="hibernate.showsql" value="true"/>
          <property name="hibernate.cache.use_second_level_cache" value="false"/>
        </properties>
      </persistence-unit>
      <non-jta-data-source>jndi_name_of_jboss_datasource</non-jta-data-source>
    </persistence>
    You need to define a data source in your container (JBoss) and give it a jndi name and access it through org.springframework.jndi.JndiObjectFactoryBean.
    Use BasicDataSource only for test purposes, it is not meant for production environment.

  3. #3
    Join Date
    Sep 2010
    Posts
    19

    Default

    Hello Enrico Pizzi,

    Thanks for the reply.

    I will try creating jndi in my JBOSS and give it try too.

    But can you please tell me what is wrong with current code using Basic data source? I am just making a test application currently to learn things in spring.

    Thanks.

  4. #4
    Join Date
    Jul 2010
    Location
    Venice, Italy
    Posts
    709

    Default

    Your current persistence configuration requires a non-jta data source declared in your persistence.xml. The declaration involves giving the jndi name of the resource. Your jndi host is your container, so you must either register your SimpleDataSource as a jndi resource on JBoss, OR use a Datasource defined directly in JBoss.

    The second option is the way to go, because it's simpler and more correct. Why more correct? Because in a j2ee application access to database should always be container-driven. This is because your application server gives you a more secure, monitorable, testable, and enhanced environment: think, for example, of the transactional services (jta transaction managers) that are tied to the datasource defined in the container, or the multi-datasource and datasource factory patterns.

    Thus, always use container-driven datasources for your j2ee applications. Spring's own defined datasources like SimpleDataSource are only there for:
    - non-web applications that still need database access;
    - unit testing (with junit etc.).

  5. #5
    Join Date
    Sep 2010
    Posts
    19

    Default

    Hello Enrico Pizzi,

    Thanks for the answer.

    Can you tell me how to make a configuration of Hibernate so that it does not require <non-jta-data-source>

    Is there any settings for that.

    I tried https://src.springframework.org/svn/...les/petclinic/ from my pc and it configured and run fine without any problem.

    Now I am trying same things with some changes and it asks for <non-jta-data-source>

    In that application they have used hsql in memory database and in my application also I am using hsql in memory database. I am using same Spring 3.0.3 jar files that I used with that application.

    Only difference is that they used toplink classes and I am using hibernate classes instead.


    Thanks.

  6. #6
    Join Date
    Jul 2010
    Location
    Venice, Italy
    Posts
    709

    Default

    TopLink and Hibernate are two very different ORM frameworks, and so their Spring integration also differs, but you shouldn't have any problem with Hibernate since it's maybe the most Spring supported ORM. I suggest you read chapter 13.3 of the Spring 3 reference guide.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •