Announcement Announcement Module
Collapse
No announcement yet.
transaction-type="JTA" / "RESOURCE_LOCAL" Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • transaction-type="JTA" / "RESOURCE_LOCAL"

    Hello
    Reading some guides around I have found that transaction-type should be set to "RESOURCE_LOCAL" for Java SE application and to "JTA" for Java EE application.

    Is there any guide that explains in details these two cases?
    I noticed that using RESOURCE_LOCAL on a web application deployed on Tomcat, seems to run fine. I would like to know better what's wrong with this.

    Thanks.

  • #2
    Reading some guides around I have found that transaction-type should be set to "RESOURCE_LOCAL" for Java SE application and to "JTA" for Java EE application.
    That is only true if you need a JTA transaction manager for distributed transactions. If you don't need that it is pretty useless to use JTA as the transaction type. So it actually depends on what TYPE of transaction manager you need.

    Comment


    • #3
      That is only true if you need a JTA transaction manager for distributed transactions. If you don't need that it is pretty useless to use JTA as the transaction type. So it actually depends on what TYPE of transaction manager you need.
      That's not true at all, I'm sorry.
      If you are running your application under glassfish (J2EE env), it requires the JTA transaction type. Glassfish will throw an exception if you don't specify
      JTA, which leads to other problems I haven't fixed yet ...

      Here's the error message you'll receive if you don't specify JTA:

      Code:
      The persistence-context-ref-name [com.cezame.dao.UserDao/entityManager] in module [C:\Documents and Settings\Stéphane\My Documents\NetBeansProjects\CezameWebProject2\build\web] resolves to a persistence unit called [CezamePU] which is of type RESOURCE_LOCAL. Only persistence units with transaction type JTA can be used as a container managed entity manager. Please verify your application.
      If I specify JTA as transaction-type (and the <jta-data-source> tag in persistence.xml) I receive the following message :

      The chosen transaction strategy requires access to the JTA TransactionManager

      So I'm completely stuck as of now. I didn't find any solution to make it work ... sadly. My company will probably choose another solution because
      setting up a JPA+Hibernate+Spring environment is too complexe and didn't work until now ! Sadly.

      Stéphane
      Last edited by tyrell; Jan 2nd, 2009, 09:24 AM.

      Comment


      • #4
        I've ran into this same issue with an app that runs fine with tomcat or any non-j2ee container, and then errors with :

        Only persistence units with transaction type JTA can be used as a container managed entity manager.

        Interesting thing was an app developed with "Roo" that has jpa configured to use Resource_local would work find on glassfish.

        I dove in and started comparing, and it turned out to be the web.xml

        My project was defined as version="2.5"

        Roo's was defined as version="2.4"

        After switching my project's web.xml to 2.4, all of my problems disappeared, and I can now deploy on glassfish with Resource_local and allow the spring framework to handle all of my transactions.

        Comment


        • #5
          I too had a tough time configuring Spring to work with Glassfish.

          Finally I managed to make it work using the following options:

          - Set the "transaction-type" as "JTA" in persistence.xml
          Code:
          <persistence-unit name="myPU" transaction-type="JTA">
          - Set the jndi datasource name in persistence.xml.
          Code:
          <jta-data-source>jdbc/myDS</jta-data-source>
          - Make an entry in web.xml to map the persistence unit
          Code:
             
              <persistence-unit-ref>
                  <persistence-unit-ref-name>persistence/myPU</persistence-unit-ref-name>
                  <persistence-unit-name>myPU</persistence-unit-name>
              </persistence-unit-ref>
          - Instruct Spring to get the entity manager factory by looking up the jndi name of our persistence unit
          Code:
          <jee:jndi-lookup id="entityManagerFactory" jndi-name="persistence/myPU"/>
          - Instruct Spring to find the suitable JTA transaction manager implementation
          Code:
          <tx:jta-transaction-manager/>
          Hmm, the Spring documentation is not up to mark in this area and I had to assemble these instructions by looking at the documentation scattered throughout the web.

          NOTE:
          This will work only if your web.xml is using servlet version "2.5".
          Last edited by James Selvakumar; Nov 11th, 2009, 09:53 PM.

          Comment


          • #6
            Hi,

            I'm having the same problem but I'm deploying onto Tomcat.

            I have a question around your jndi lookup for the persistence unit, have you registered this with Tomcat , if so, can you post how you did it?

            I have registered my datasource with Tomcat. I put this entry into the META-INF/context.xml file of the war and Tomcat extracted it automatically.

            How would you register a persistence unit this way ?

            Code:
            <Context path="/test">
            
            <Resource name="jdbc/misDataSource" auth="Container"
                      type="javax.sql.DataSource"
            	  driverClassName="com.mysql.jdbc.Driver"
            	  url="jdbc:mysql://localhost:3306/duplicates"
            	  username="root"
            	  password=""
            	  maxActive="8"
            	  maxIdle="4"/>
            </Context>
            I then perform the lookup using

            Code:
            <jee:jndi-lookup id="misDataSource" jndi-name="java:comp/env/jdbc/misDataSource"/>

            Comment


            • #7
              Tomcat does not support exporting persistence units through jndi.

              I suppose you are meaning "datasource". In that case look up the jndi datasource using the following code:

              Code:
              <jee:jndi-lookup id="misDataSource" jndi-name="/jdbc/misDataSource"/>

              Comment


              • #8
                Originally posted by James Selvakumar View Post
                I too had a tough time configuring Spring to work with Glassfish.

                - Instruct Spring to get the entity manager factory by looking up the jndi name of our persistence unit
                Code:
                <jee:jndi-lookup id="entityManagerFactory" jndi-name="persistence/myPU"/>
                NOTE:
                This will work only if your web.xml is using servlet version "2.5".
                you mention above performing a jndi lookup for the entityManagerFactory, my question is about how you register this in the JNDI namespace.

                When we do the lookup for the datasource, we have to have the datasource registerd in the JNDI namespace.

                Code:
                <jee:jndi-lookup id="misDataSource" jndi-name="java:comp/env/jdbc/misDataSource"/>
                This will work if we have the datasource registered in the context.xml file.

                Does the lookup for the entityManagerFactory just look in the classpath ?

                Comment


                • #9
                  Hi,

                  Where do we register -

                  <jee:jndi-lookup id="misDataSource" jndi-name="java:comp/env/jdbc/misDataSource"/>

                  Do we add it to the persistent.xml?

                  I am having similar problems.

                  Regards

                  eve

                  Comment


                  • #10
                    Hi,

                    Registering your JNDI datasource is vendor specific. I was using Tomcat 5, I had to create a context.xml file in my war (META-INF/context.xml). When this is deployed, Tomcat extracts the context.xml file and copies it to the $TOMCAT_HOME\conf\Catalina\localhost directory and creates a file called yourappname.xml

                    Here is my context.xml file I used.

                    Code:
                    <?xml version="1.0" encoding="UTF-8"?>
                    <Context path="/duplicatestatement-0.0.1" docBase="/duplicatestatement-0.0.1">
                    
                         <Resource name="jdbc/misDataSource" auth="Container"     
                              type="javax.sql.DataSource"
                    	  driverClassName="org.gjt.mm.mysql.Driver"
                    	  url="jdbc:mysql://localhost:3306/testdb"
                    	  username="root"
                    	  password="root"
                    	  maxActive="8"
                    	  maxIdle="4"/>
                    </Context>

                    Comment


                    • #11
                      Originally posted by knoxor View Post
                      Hi,

                      Registering your JNDI datasource is vendor specific. I was using Tomcat 5, I had to create a context.xml file in my war (META-INF/context.xml). When this is deployed, Tomcat extracts the context.xml file and copies it to the $TOMCAT_HOME\conf\Catalina\localhost directory and creates a file called yourappname.xml

                      Here is my context.xml file I used.

                      Code:
                      <?xml version="1.0" encoding="UTF-8"?>
                      <Context path="/duplicatestatement-0.0.1" docBase="/duplicatestatement-0.0.1">
                      
                           <Resource name="jdbc/misDataSource" auth="Container"     
                                type="javax.sql.DataSource"
                      	  driverClassName="org.gjt.mm.mysql.Driver"
                      	  url="jdbc:mysql://localhost:3306/testdb"
                      	  username="root"
                      	  password="root"
                      	  maxActive="8"
                      	  maxIdle="4"/>
                      </Context>
                      Thank you for your reply.

                      My JNDI is already registered with my application server. I am using glassfish
                      v2.1 and SQL server with Toplink. I am able to retrieve and input data into
                      the database. My problems comes when I add spring to my application.

                      Below is my persistence.xml

                      -

                      <?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="ENTERPRISE-ejbPU" transaction-type="JTA">
                      <jta-data-source>jdbc_/EnterpriseDB2</jta-data-source>
                      <exclude-unlisted-classes>false</exclude-unlisted-classes>
                      <properties />
                      </persistence-unit>
                      </persistence>

                      I have been following the below tutorial to use spring-

                      http://wheelersoftware.com/articles/...-flow-2.0.html

                      He is not connected to any DB and using the following to display
                      data to the site.

                      public CartServiceImpl() {
                      addProduct(1L, "Bag of Skittles", 100, "skittles.jpg");
                      addProduct(2L, "Dieselboy CD", 3995, "dieselboy.jpg");
                      addProduct(3L, "Darth Vader Mask", 159995, "vader.jpg");
                      addProduct(4L, "Lightsaber", 57950, "lightsaber.jpg");
                      }


                      An you can see, this tutorial is very useful.

                      I have to use Persistencecontext to retrieve data and display it to the user.

                      This' where I am stack.

                      Thanking you in advance.

                      eve

                      Comment


                      • #12
                        Interesting thing was an app developed with "Roo" that has jpa configured to use Resource_local..I guess this is better.

                        Regards

                        Daevone
                        Surendettement

                        Comment


                        • #13
                          Originally posted by evepokua View Post
                          Thank you for your reply.

                          My JNDI is already registered with my application server. I am using glassfish
                          v2.1 and SQL server with Toplink. I am able to retrieve and input data into
                          the database. My problems comes when I add spring to my application.

                          Below is my persistence.xml

                          -

                          <?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="ENTERPRISE-ejbPU" transaction-type="JTA">
                          <jta-data-source>jdbc_/EnterpriseDB2</jta-data-source>
                          <exclude-unlisted-classes>false</exclude-unlisted-classes>
                          <properties />
                          </persistence-unit>
                          </persistence>

                          I have been following the below tutorial to use spring-

                          http://wheelersoftware.com/articles/...-flow-2.0.html

                          He is not connected to any DB and using the following to display
                          data to the site.

                          public CartServiceImpl() {
                          addProduct(1L, "Bag of Skittles", 100, "skittles.jpg");
                          addProduct(2L, "Dieselboy CD", 3995, "dieselboy.jpg");
                          addProduct(3L, "Darth Vader Mask", 159995, "vader.jpg");
                          addProduct(4L, "Lightsaber", 57950, "lightsaber.jpg");
                          }


                          An you can see, this tutorial is very useful.

                          I have to use Persistencecontext to retrieve data and display it to the user.

                          This' where I am stack.

                          Thanking you in advance.

                          eve
                          Hi,

                          Its not clear from your post what your problem is. Can you post your errors and your config files? (Please put your code in the code tags as its easier to read)

                          Comment


                          • #14
                            Originally posted by knoxor View Post
                            Hi,

                            Its not clear from your post what your problem is. Can you post your errors and your config files? (Please put your code in the code tags as its easier to read)

                            Hi,

                            Thank you for your reply.

                            I have an application which I am using glassfish as the application server.
                            I recently added spring and webflow to my application to implement shopping cart. So I followed the following spring tutorial except it does not use any
                            database connection.

                            http://wheelersoftware.com/articles/...-flow-2.0.html


                            I ve already connect to the DB with my application server.

                            The error -
                            [#|2009-12-03T12:43:14.338+0000|SEVERE|sun-appserver2.1|javax.enterprise.system.container.web |_ThreadID=19;_ThreadName=httpSSLWorkerThread-8080-0;_RequestID=ba593cd7-64fa-4393-a6b7-b2c20e1ce040;|StandardWrapperValve[dispatcher]: PWC1382: Allocate exception for servlet dispatcher
                            org.springframework.beans.factory.NoSuchBeanDefini tionException: No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 0
                            at org.springframework.orm.jpa.support.PersistenceAnn otationBeanPostProcessor.findDefaultEntityManagerF actory(PersistenceAnnotationBeanPostProcessor.java :514)
                            at org.springframework.orm.jpa.support.PersistenceAnn otationBeanPostProcessor.findEntityManagerFactory( PersistenceAnnotationBeanPostProcessor.java:473)
                            at org.springframework.orm.jpa.support.PersistenceAnn otationBeanPostProcessor$PersistenceElement.resolv eEntityManager(PersistenceAnnotationBeanPostProces sor.java:599)
                            at org.springframework.orm.jpa.support.PersistenceAnn otationBeanPostProcessor$PersistenceElement.getRes ourceToInject(PersistenceAnnotationBeanPostProcess or.java:570)
                            at org.springframework.beans.factory.annotation.Injec tionMetadata$InjectedElement.inject(InjectionMetad ata.java:180)
                            at org.springframework.beans.factory.annotation.Injec tionMetadata.injectFields(InjectionMetadata.java:1 05)
                            at org.springframework.orm.jpa.support.PersistenceAnn otationBeanPostProcessor.postProcessAfterInstantia tion(PersistenceAnnotationBeanPostProcessor.java:3 08)
                            at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.populateBean(AbstractAu towireCapableBeanFactory.java:959)
                            at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:472)
                            at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory$1.run(AbstractAutowireC apableBeanFactory.java:409)
                            at java.security.AccessController.doPrivileged(Native Method)
                            at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:380)
                            at org.springframework.beans.factory.support.Abstract BeanFactory$1.getObject(AbstractBeanFactory.java:2 64)
                            at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.getSingleton(DefaultSingleton BeanRegistry.java:222)
                            at org.springframework.beans.factory.support.Abstract BeanFactory.doGetBean(AbstractBeanFactory.java:261 )
                            at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:185)
                            at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:164)
                            at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.autowireByName(Abstract AutowireCapableBeanFactory.java:1029)
                            at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.populateBean(AbstractAu towireCapableBeanFactory.java:977)
                            at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:472)
                            at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory$1.run(AbstractAutowireC apableBeanFactory.java:409)
                            at java.security.AccessController.doPrivileged(Native Method)
                            at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:380)
                            at org.springframework.beans.factory.support.Abstract BeanFactory$1.getObject(AbstractBeanFactory.java:2 64)
                            at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.getSingleton(DefaultSingleton BeanRegistry.java:222)
                            at org.springframework.beans.factory.support.Abstract BeanFactory.doGetBean(AbstractBeanFactory.java:261 )
                            at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:185)
                            at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:164)
                            at org.springframework.beans.factory.support.DefaultL istableBeanFactory.preInstantiateSingletons(Defaul tListableBeanFactory.java:429)
                            at org.springframework.context.support.AbstractApplic ationContext.finishBeanFactoryInitialization(Abstr actApplicationContext.java:728)
                            at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:380)
                            at org.springframework.web.servlet.FrameworkServlet.c reateWebApplicationContext(FrameworkServlet.java:4 02)
                            at org.springframework.web.servlet.FrameworkServlet.i nitWebApplicationContext(FrameworkServlet.java:316 )
                            at org.springframework.web.servlet.FrameworkServlet.i nitServletBean(FrameworkServlet.java:282)
                            at org.springframework.web.servlet.HttpServletBean.in it(HttpServletBean.java:126)
                            at javax.servlet.GenericServlet.init(GenericServlet.j ava:270)
                            at org.apache.catalina.core.StandardWrapper.loadServl et(StandardWrapper.java:1194)
                            at org.apache.catalina.core.StandardWrapper.allocate( StandardWrapper.java:848)
                            at org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:222)
                            at org.apache.catalina.core.StandardContextValve.invo keInternal(StandardContextValve.java:287)
                            at org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:218)
                            at org.apache.catalina.core.StandardPipeline.doInvoke (StandardPipeline.java:648)
                            at org.apache.catalina.core.StandardPipeline.doInvoke (StandardPipeline.java:593)
                            at com.sun.enterprise.web.WebPipeline.invoke(WebPipel ine.java:94)
                            at com.sun.enterprise.web.PESessionLockingStandardPip eline.invoke(PESessionLockingStandardPipeline.java :98)
                            at org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:222)
                            at org.apache.catalina.core.StandardPipeline.doInvoke (StandardPipeline.java:648)
                            at org.apache.catalina.core.StandardPipeline.doInvoke (StandardPipeline.java:593)
                            at org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:587)
                            at org.apache.catalina.core.ContainerBase.invoke(Cont ainerBase.java:1096)
                            at org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:166)
                            at org.apache.catalina.core.StandardPipeline.doInvoke (StandardPipeline.java:648)
                            at org.apache.catalina.core.StandardPipeline.doInvoke (StandardPipeline.java:593)
                            at org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:587)
                            at org.apache.catalina.core.ContainerBase.invoke(Cont ainerBase.java:1096)
                            at org.apache.coyote.tomcat5.CoyoteAdapter.service(Co yoteAdapter.java:288)
                            at com.sun.enterprise.web.connector.grizzly.DefaultPr ocessorTask.invokeAdapter(DefaultProcessorTask.jav a:647)
                            at com.sun.enterprise.web.connector.grizzly.DefaultPr ocessorTask.doProcess(DefaultProcessorTask.java:57 9)
                            at com.sun.enterprise.web.connector.grizzly.DefaultPr ocessorTask.process(DefaultProcessorTask.java:831)
                            at com.sun.enterprise.web.connector.grizzly.DefaultRe adTask.executeProcessorTask(DefaultReadTask.java:3 41)
                            at com.sun.enterprise.web.connector.grizzly.DefaultRe adTask.doTask(DefaultReadTask.java:263)
                            at com.sun.enterprise.web.connector.grizzly.DefaultRe adTask.doTask(DefaultReadTask.java:214)
                            at com.sun.enterprise.web.connector.grizzly.TaskBase. run(TaskBase.java:265)
                            at com.sun.enterprise.web.connector.grizzly.ssl.SSLWo rkerThread.run(SSLWorkerThread.java:106)
                            |#]

                            This' as a result -

                            @PersistenceContext
                            EntityManager em;



                            public List<Itement> getItems() {
                            List<Itement> list = new ArrayList<Itement>(items.values());
                            Collections.sort(list);
                            try{
                            list=(List<Itement>)em.createNamedQuery("getAllIte ms").getResultList();

                            return list;
                            }catch (Exception ex) {

                            System.err.println("Can not get all items");
                            throw new EJBException(ex);
                            }
                            }


                            I am trying to use my persistence.xml

                            <?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="KAKENTERPRISE-ejbPU" transaction-type="JTA">
                            <jta-data-source>jdbc_/KAKEnterpriseDB2</jta-data-source>
                            <exclude-unlisted-classes>false</exclude-unlisted-classes>
                            <properties>
                            </properties>
                            </persistence-unit>
                            </persistence>
                            This is being called with the servletcontroller -
                            @RequestMapping("/home.do")
                            public ModelMap doHome() {
                            ModelMap model = new ModelMap();
                            model.addAttribute("shoppingCart", cartService.getShoppingCart());
                            model.addAttribute("bales", cartService.getBales());
                            return model;
                            }

                            Basically, I need to tell string I am using the persistence.xml to insert and retrieve data from the DB.

                            How do I do this with spring? I assum I have to write it in my dispatcher-servlet.xml.

                            I hope I have made it a little clear.

                            eve
                            Last edited by evepokua; Dec 3rd, 2009, 05:29 PM.

                            Comment


                            • #15
                              You don't seem to have configured an entityManager bean in your Spring Context.

                              You need to setup your datasource etc and entitymanager in a Spring Context file.

                              Have a look at the spring docs: http://static.springsource.org/sprin...rence/orm.html

                              e.g

                              Code:
                              <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
                                <property name="loadTimeWeaver">
                                  <bean class="org.springframework.instrument.classloading.glassfish.GlassFishLoadTimeWeaver"/>
                                </property>
                              </bean>
                              Using the @PersistenceContext annotation, Spring will automatically take the above EntityManagerFactory and inject an EntityManager into your annotated class.

                              You can even specify what Spring context file to use (where you have defined your entityManager)

                              e.g
                              Code:
                              @ContextConfiguration (locations = { "classpath:applicationContext.xml"})
                              public class test {
                              
                                     private EntityManager em;
                              
                                     @PersistentContext
                                     public void setEntityManager(EntityManager em) {
                              
                                          this.em = em;
                                     }
                              }
                              Search for EntityManager configuration in the docs and have a look.

                              P.S you should put the code quotes around your code and not just click the quote button (look at the menu bar at the top when typing your reply, use the # button to add the code quotes)

                              Comment

                              Working...
                              X