Results 1 to 10 of 10

Thread: Spring Hibernate Transactions not flushed

Hybrid View

  1. #1
    Join Date
    Jul 2009
    Posts
    12

    Default Spring Hibernate Transactions not flushed

    Hello,

    I am using spring 2.5.6, hibernate 3.3 and tomcat 6, MySQL 5. Below is my application.xml showing database config. The problem I have is that changes that I have made to hibernate objects attached to the hibernate session only get committed to the database provided I call session.flush() or an explicit hibernate save/update method at the end of the transaction. How can I set things up so that the the changes are committed without having to do either of these approaches. I have lots of methods and it would be a real pain to have to do explicit save calls or a session.flush() at the end of each transaction method. Below is the relevant part of my applicationContext.xml.
    Many thanks. Help would be REALLY appreciated

    <context:annotation-config/>
    <bean id="myDataSource" class="org.springframework.jndi.JndiObjectFactoryB ean">
    <property name="jndiName" value="java:comp/env/jdbc/filmSource"/>
    </bean>
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean">
    <property name="dataSource" ref="myDataSource"/>
    <property name="mappingResources">
    <list>
    <value>film/hibernate/Commission.hbm.xml</value>
    </list>
    </property>
    <property name="hibernateProperties">

    <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQ LDialect</prop>
    <prop key="hibernate.cache.provider_class">org.hibernate .cache.EhCacheProvider</prop>
    <prop key="hibernate.cache.use_second_level_cache">true</prop>
    <prop key="hibernate.show_sql">true</prop>
    </props>

    </property>
    </bean>

    <aop:config>
    <aopointcut id="defaultServiceOperation"
    expression="execution(* *Helper.*(..))"/>
    <aop:advisor pointcut-ref="defaultServiceOperation" advice-ref="defaultTxAdvice"/>
    </aop:config>

    <tx:advice id="defaultTxAdvice">
    <tx:attributes>
    <tx:method name="*" propagation="REQUIRED" isolation="READ_COMMITTED" />
    </tx:attributes>
    </tx:advice>


    <bean id="transactionManager" class="org.springframework.orm.hibernate3.Hibernat eTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean>

  2. #2
    Join Date
    Nov 2007
    Posts
    420

    Default

    try adding this to your hibernate properties
    Code:
    <prop key="hibernate.transaction.flush_before_completion">true</prop>

  3. #3
    Join Date
    Jul 2009
    Posts
    12

    Default

    Quote Originally Posted by bdangubic View Post
    try adding this to your hibernate properties
    Code:
    <prop key="hibernate.transaction.flush_before_completion">true</prop>
    Thanks for the suggestion and I was very hopeful it would work after reading the description in the hibernate doc. Unfortunately adding the property hasnt made any difference. An explicit session.flush() is still required at the end of the method.

  4. #4
    Join Date
    Apr 2008
    Location
    Seville, Spain
    Posts
    132

    Default

    Configurations seems ok.

    Can you post a log between tx manager start and end trasaction?. Log the session flushMode seession.getFlushMode().

    cheers

  5. #5
    Join Date
    Jul 2009
    Posts
    12

    Default

    chelu I have done as you suggested and if I call session.getFlushMode() within the method body of the transaction it returns the value NEVER. Is that the problem?

  6. #6
    Join Date
    Apr 2008
    Location
    Seville, Spain
    Posts
    132

    Default

    Sure, flush mode Never, must be used only in read-only transactions and HibernateTrasactionManager uses modes AUTO or COMMIT flush modes on non read-only transactions.

    Why your transactions are read-only?,
    Are you using annotations?
    Are you sure that services methods are matched by the pointcut?

    I can see this on log, (if you post the log)

    You can enforce the flush with earlyFlushBeforeCommit property of HibernateTrasactionManager set to true, but you really don't need it for get a non read-only trasaction flush

    cheers.

  7. #7
    Join Date
    Jul 2009
    Posts
    12

    Default

    chelu,

    As far as I am aware the transactions are not read-only as you can see from the earlier config. Also if I do session.flush() at the end of the method things do get saved to the database. I am not using annotations. As far as I can tell my services methods are matched by the pointcut. The example I am referring to for instance is in a class called DrContractRightsHelper

    Below are 2 snippets from the log showing the start and end of the transaction I am looking at. Any further help would be greatly appreciated.

    2010-02-11 16:42:53,546 DEBUG [http-8080-2] (OpenSessionInViewFilter.java:239) - Using SessionFactory 'sessionFactory' for OpenSessionInViewFilter
    2010-02-11 16:42:53,546 DEBUG [http-8080-2] (AbstractBeanFactory.java:214) - Returning cached instance of singleton bean 'sessionFactory'
    2010-02-11 16:42:53,546 DEBUG [http-8080-2] (OpenSessionInViewFilter.java:181) - Opening single Hibernate Session in OpenSessionInViewFilter
    2010-02-11 16:42:53,546 DEBUG [http-8080-2] (SessionFactoryUtils.java:315) - Opening Hibernate Session
    2010-02-11 16:42:53,546 DEBUG [http-8080-2] (SessionImpl.java:220) - opened session at timestamp: 5185153325244416
    2010-02-11 16:42:53,546 DEBUG [http-8080-2] (SessionImpl.java:1289) - setting flush mode to: NEVER
    2010-02-11 16:42:53,546 DEBUG [http-8080-2] (TransactionSynchronizationManager.java:168) - Bound value [org.springframework.orm.hibernate3.SessionHolder@1 c47dd5] for key [org.hibernate.impl.SessionFactoryImpl@b635d7] to thread [http-8080-2]
    2010-02-11 16:42:53,546 DEBUG [http-8080-2] (ModuleUtils.java:171) - Get module name for path /EditDrContractRights.do
    2010-02-11 16:42:53,546 DEBUG [http-8080-2] (ModuleUtils.java:171) - Get module name for path /EditDrContractRights.do

    -----------------------------------------------------------------------------------------------------
    -----------------------------------------------------------------------------------------------------

    2010-02-11 16:42:54,390 DEBUG [http-8080-2] (TwoPhaseLoad.java:206) - done materializing entity [film.hibernate.DrContract#111]
    2010-02-11 16:42:54,390 DEBUG [http-8080-2] (StatefulPersistenceContext.java:790) - initializing non-lazy collections
    2010-02-11 16:42:54,390 DEBUG [http-8080-2] (Loader.java:1883) - done entity load
    2010-02-11 16:42:54,390 DEBUG [http-8080-2] (JDBCContext.java:233) - after autocommit
    2010-02-11 16:42:54,390 DEBUG [http-8080-2] (ConnectionManager.java:302) - transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!
    2010-02-11 16:42:54,390 DEBUG [http-8080-2] (SessionImpl.java:422) - after transaction completion
    2010-02-11 16:42:54,390 DEBUG [http-8080-2] (SessionImpl.java:832) - initializing proxy: [film.hibernate.PrContract#32]
    2010-02-11 16:42:54,390 DEBUG [http-8080-2] (DefaultLoadEventListener.java:332) - attempting to resolve: [film.hibernate.PrContract#32]
    2010-02-11 16:42:54,390 DEBUG [http-8080-2] (DefaultLoadEventListener.java:369) - object not resolved in any cache: [film.hibernate.PrContract#32]
    2010-02-11 16:42:54,390 DEBUG [http-8080-2] (AbstractEntityPersister.java:3037) - Fetching entity: [film.hibernate.PrContract#32]
    2010-02-11 16:42:54,390 DEBUG [http-8080-2] (Loader.java:1852) - loading entity: [film.hibernate.PrContract#32]

Posting Permissions

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