Announcement Announcement Module
Collapse
No announcement yet.
JPA + Managed enviroment Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • JPA + Managed enviroment

    Hi,

    I have a couple of DAOs that use JPA for CRUD. They get their EntityManager injected by Spring.

    The xml sets up a LocalContainerEntityManager and uses PersistenceAnnotationBeanPostProcessor to inject the EM.

    I want to run this in a managed EJB enviroment, where my CMT EJBs call the DAOs. When the DAOs are called I want them to run in the transaction specified by the EJB. How do I configure this?
    From what I've understood (I'm using Websphere) I have to set up a JTATransactionManager using the WebSphereTransactionManagerFactoryBEan as input param.

    What I don't understand is how I get the DAOs to hook into the ejb tx when they are called.
    Last edited by magott; Nov 14th, 2006, 06:35 AM. Reason: specified that the ejbs where cmt

  • #2
    You can either use Spring JtaTM (the WebSphereTMFB) along with your DAOs or you can switch from LocalContainerEntityManager to LocalEntityManagerFactoryBean and configure your persistence unit to use a JTA transaction type.

    Comment


    • #3
      Originally posted by Costin Leau View Post
      You can either use Spring JtaTM (the WebSphereTMFB) along with your DAOs or you can switch from LocalContainerEntityManager to LocalEntityManagerFactoryBean and configure your persistence unit to use a JTA transaction type.
      All I have to do is set up the WebSphereTMFB in the same context as the LocalContainerEntityManager, and everything will be handled? Can't find a way to inject the tm anywhere..

      Comment


      • #4
        When using Jpa, the transaction manager is not injected into an entityManagerFactory rather, the other way around - see the reference documentation for more details.
        When dealing with JTA, the transaction manager is already configured in the server and has its resources enlisted and inside Spring you just have to specify it without hooking to the JPA provider (this is taken care of inside the persistence.xml).

        Comment


        • #5
          Originally posted by Costin Leau View Post
          When using Jpa, the transaction manager is not injected into an entityManagerFactory rather, the other way around - see the reference documentation for more details.
          When dealing with JTA, the transaction manager is already configured in the server and has its resources enlisted and inside Spring you just have to specify it without hooking to the JPA provider (this is taken care of inside the persistence.xml).
          Code:
          <!-- Business POJO-->
          <bean id="personServicePojo" class="com.test.service.PersonPojo">
          
          <!--DAO-->
          <bean id="personDAO" class="com.test.dao.Person"/>
          
          <!-- JPA EntityManagerFactory -->
          <bean id="entityManagerFactory"
          class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBea
          n">
          	<property name="persistenceUnitName" value="pu1"/>
          	<property name="dataSource" ref="dataSource"/>
          	<property name="loadTimeWeaver">
          		<bean
          class="org.springframework.instrument.classloading.InstrumentationLoadTi
          meWeaver"/>
          	</property>
          	<property name="jpaVendorAdapter">
          		<bean
          	
          class="org.springframework.orm.jpa.vendor.TopLinkJpaVendorAdapter">
          			<property name="showSql" value="true" />
          			<property name="generateDdl" value="true" />
          			<property name="databasePlatform"
          	
          value="${jpaVendorAdapterDatabasePlattform}" />
          		</bean>
          	</property>
          </bean>   
          
          <!-- PostProcessors to perform resource injection according to the JPA
          specification (@PersistenceContext, @PersistenceUnit). -->		
          <bean
          class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPost
          Processor"/>
          	
          <!-- Configurer that replaces ${...} placeholders with values from a properties file -->
          <!-- (in this case, JDBC-related settings for the dataSource definition
          above) -->
          <bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfi
          gurer">
          	<property name="location" value="enviroment.properties"/> </bean>
          
          <!--Websphere tx manager factory-->
          <bean id="wsJtaTm"
          class="org.springframework.transaction.jta.WebSphereTransactionManagerFa
          ctoryBean"/>
          
          <!--Transaction manager-->
          <bean id="transactionManager"
          class="org.springframework.transaction.jta.JtaTransactionManager">
          	<property name="transactionManager" ref="wsJtaTm" /> 
          </bean>
          Persistence.xml only contains a persistence unit. No properties.

          The EJB is CMT (all methods REQUIRES), but when I call updating operations on the dao, they are not persisted in the database.
          Queries are executed ok.

          When doing a entityManager.flush() I get an exception stating that no transaction is active.

          I'm almost there, I just can't figure out why a tx isn't started. I'm guessing it has something to do with my configuration. Doesn't seem like spring is able to hook into the tx running on the container?

          Comment


          • #6
            How do you define your dataSource?

            Comment


            • #7
              as a jndiobject, configured on the server.
              should probably also mention that the server is WebSphere 6.1, which doesn't have EJB 3.0 support. The ejb is a ejb 2.1 bean.

              Comment


              • #8
                Did you try setting the jta-datasource in persistence.xml?

                Comment


                • #9
                  I'm reading your post from my mobile. I'll try it once I'm at my pc.

                  Comment


                  • #10
                    Putting the datasource into the persistence.xml, and removing it from the LocalContainerEntityManagerFactoryBean gave me an error that no datasource was found, and no default was set.

                    Having it in both persistence.xml AND the LocalContainerEntityManagerFactoryBean gave me the same exception as earlier, stating that no active transaction present (to that effect, can't remember the exact wording)

                    It's really frustrating, I feel that I'm really close. I just can't get Spring and JPA to hook into the cmt...

                    Desperate for help! Is what I'm trying to accomplish at all possible? (EJB 2.1 CMT running on a AppServer with J2EE 1.4 calling JPA Daos?)

                    Comment


                    • #11
                      Almost out of ideas here...

                      Are you 100% sure CMT is really working? Did you try setting transaction type to "jta" in persistence.xml?

                      Comment


                      • #12
                        HAven't set the transaction type explicitly, JTA is default. I guess I can try, as I think I have tried just about everything else...

                        Comment


                        • #13
                          setting jta in persistence.xml produced the same exception

                          Comment


                          • #14
                            I'd say you should turn on Spring transaction and/or jpa debugging and see what really happens under the hood.

                            Comment


                            • #15
                              Originally posted by dejanp View Post
                              I'd say you should turn on Spring transaction and/or jpa debugging and see what really happens under the hood.
                              Tried setting up spring tx (JpaTransactionManager), using <tx:annotation-driven/>, I got an error. The error said that my dao was injected in raw format, due to some circular bean dependencies. Problem is I can't find them....I'm trying to figure it all out now.

                              At the moment, it seems JPA and J2EE 1.4 is a no-go...

                              Looking at the Spring reference documentation, there seems to be missing good examples on how to set up jpa w/spring using transactions (either regular jta or spring)
                              Last edited by magott; Nov 30th, 2006, 10:23 AM.

                              Comment

                              Working...
                              X