Results 1 to 10 of 19

Thread: Auto Flush not working for Spring 3.1 LocalEntityManagerFactoryBean+Hibernate 4+JTA

Hybrid View

  1. #1

    Default Auto Flush not working for Spring 3.1 LocalEntityManagerFactoryBean+Hibernate 4+JTA

    (I am asking the same question at StackOverflow too:
    http://stackoverflow.com/questions/1...bean-hibernate )


    I have did a lot of search on the web and pages like this and this and this mostly suggested for using CMTTransactionFactory. However I am still unable to make it work (I wonder if it is because I am creating a EM using a LocalContainerEntityManagerFactoryBean instead of creating Hibernate session by LocalSessionFactoryBean)

    Here is what I have in my app ctx
    Code:
    <jee:jndi-lookup  id="dataSource" jndi-name="jdbc/fooDs" />
    
    <bean id="transactionManager"
        class="org.springframework.transaction.jta.WebSphereUowTransactionManager" />
    
    <bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect" />
            </bean>
        </property>
        <property name="persistenceUnitName" value="foo" />
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.foo" />
        <!--
          <property name="persistenceXmlLocation" value="classpath:/META-INF/test-persistence.xml" />
         -->
        <property name="jpaPropertyMap">
            <map>
                <entry key="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
                <!--
                  <entry key="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.WebSphereExtendedJTATransactionLookup" />
                  <entry key="hibernate.transaction.factory_class" value="org.hibernate.transaction.CMTTransactionFactory"/>
                 -->
                <entry key="hibernate.transaction.factory_class" value="org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory"/>
                <entry key="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.WebSphereExtendedJtaPlatform"/>
                <entry key="hibernate.transaction.flush_before_completion"  value = "true" />
            </map>
        </property>
    </bean>
    (In jpaPropertyMap, I tried to have the two remarked entries and still the same)

    I am making use of Spring Data JPA and after the entity is retrieved, I updated the content. If I am not flushing the repository (which means session/entityManager) manually, it simply discard my changes.

    Can anyone give me some hints on what I have missed in the setting? Is there anything special to notice for using LocalContainerEntityManagerFactoryBean?

    I am using Spring 3.1.2.RELEASE, Hibernate 4.1.6.Final, Websphere 8
    Last edited by adrianshum; Oct 15th, 2012 at 09:11 PM. Reason: add missing links

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    In the snippet you post there is nothing to drive your transaction... There is only a transaction manager (the what) but no where (tx:annotation-driven or some other transaction demarcation).

    Also as you are using the jta.platform property either don't configure the factory_class or make it JtaTransactionFactory...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3

    Default

    I only extract out suspicious snippet to avoid having a huge xml that no one is going to read I am using annotation-driven tx. It seems working fine if I am manually flushing the entity manager (by Spring Data JPA's JpaRepository#flush() )

    Lemme have a try for factory_class change.

    Just to clarify a bit, originally I have only
    <entry key="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.WebSphereExtended JTATransactionLookup" />
    <entry key="hibernate.transaction.factory_class" value="org.hibernate.transaction.CMTTransactionFac tory"/>
    in the property, and it is not working. However, is it supposed to be fine?

    Thanks

    Quote Originally Posted by Marten Deinum View Post
    In the snippet you post there is nothing to drive your transaction... There is only a transaction manager (the what) but no where (tx:annotation-driven or some other transaction demarcation).

    Also as you are using the jta.platform property either don't configure the factory_class or make it JtaTransactionFactory...

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    Well the transaction stuff is basically deprecated in 4.x and you should have enough at only declaring a jta.platform...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5

    Default

    I am wrong on what I have told:
    Only certain combinations of properties in jpaPropertyMap give me "no-auto-flushing" behavior. Other combination simply tell me there is no transaction in progress when I flush.

    Here is the combinations I have tried in jpaPropertyMap:

    Code:
    hibernate.transaction.manager_lookup_class    org.hibernate.transaction.WebSphereExtendedJTATransactionLookup
    no transaction is in progress




    Code:
    hibernate.transaction.manager_lookup_class    org.hibernate.transaction.WebSphereExtendedJTATransactionLookup
    hibernate.transaction.factory_class           org.hibernate.transaction.CMTTransactionFactory
    transaction active, manual flush works, auto-flush not working




    Code:
    hibernate.transaction.manager_lookup_class    org.hibernate.transaction.WebSphereExtendedJTATransactionLookup
    hibernate.transaction.factory_class           org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory
    transaction active, manual flush works, auto-flush not working




    Code:
    hibernate.transaction.manager_lookup_class    org.hibernate.transaction.WebSphereExtendedJTATransactionLookup
    hibernate.transaction.factory_class           org.hibernate.transaction.JTATransactionFactory
    no transaction is in progress




    Code:
    hibernate.transaction.manager_lookup_class    org.hibernate.transaction.WebSphereExtendedJTATransactionLookup
    hibernate.transaction.factory_class           org.hibernate.engine.transaction.internal.jta.JtaTransactionFactory
    no transaction is in progress





    Code:
    hibernate.transaction.jta.platform            org.hibernate.service.jta.platform.internal.WebSphereExtendedJtaPlatform
    no transaction is in progress





    Code:
    hibernate.transaction.jta.platform            org.hibernate.service.jta.platform.internal.WebSphereExtendedJtaPlatform
    hibernate.transaction.factory_class           org.hibernate.transaction.CMTTransactionFactory
    no transaction is in progress





    Code:
    hibernate.transaction.jta.platform            org.hibernate.service.jta.platform.internal.WebSphereExtendedJtaPlatform
    hibernate.transaction.factory_class           org.hibernate.transaction.JTATransactionFactory
    no transaction is in progress





    In short, no matter I am using jta.platform or manager_lookup_class, JPA/Hibernate seems only be able to pick up the transaction when I am using CMTTransactionFactory. However, auto-flush is still not working.


    My app service look like this:

    Code:
    @Transactional(propagation=Propagation.REQUIRES_NEW)
    public class FooServiceImpl implements FooService {
      @Inject
      private FooRepository fooRepo;
    
      public void doSomething() {
        Foo foo = fooRepo.findBySomething(...);
        foo.setBar(...);
        //fooRepo.flush();
      }
    }
    (the line fooRepo.flush() is the place I modify to try manual flushing and auto flushing)

    Repository is making use of Spring Data JPA


    I really have no clues on what else I can try

    Using Spring 3.1.2, Hibernate 4.1.6, Websphere 8

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    We use the exact same combination without problems and we only have a JtaPlatform configured... (The other properties are deprecated). However we don't use Spring Data but plain repositories.

    The no-transaction indicates to me that your @transactional isn't working, can you post a stacktrace in the case of a non-active transaction.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

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
  •