Announcement Announcement Module
Collapse
No announcement yet.
Can't get declarative transactions to work :( Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Can't get declarative transactions to work :(

    I'm having a hard time figuring this one out. I'm using Hibernate for persistence and I have no problem with using programmatic transactions but declaratively I'm stuck Here's my configuration:

    Code:
    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
          <property name="jndiName">
    		<value>java&#58;comp/env/jdbc/mutwiki</value></property>
    </bean>
    
    <!-- Hibernate transaction manager -->
    <bean id="transactionManagerHibernate"
    class="org.springframework.orm.hibernate.HibernateTransactionManager">
            <property name="sessionFactory"><ref local="sessionFactory"/></property>
    </bean>
    
    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
            <property name="dataSource"><ref local="dataSource"/></property>
            <property name="mappingResources">
              <list>
                <value>hibernate/org/kuato/mutwiki/domain/GalleryAlbum.hbm.xml</value>
                <value>hibernate/org/kuato/mutwiki/domain/GalleryPhoto.hbm.xml</value>
                <value>hibernate/org/kuato/mutwiki/domain/GalleryComment.hbm.xml</value>
              </list>
            </property>
            <property name="hibernateProperties">
                <props>
                    <prop
    				key="hibernate.dialect">net.sf.hibernate.dialect.PostgreSQLDialect</prop>
                    <prop key="hibernate.show_sql">true</prop>
                    <prop key="hibernate.max_fetch_depth">4</prop>
                    <prop key="hibernate.autocommit">false</prop>
                    <prop key="hibernate.cache.use_query_cache">true</prop>
                    <prop key="hibernate.cache.provider_class">net.sf.hibernate.cache.EhCacheProvider</prop>
                </props>
            </property>
    </bean>
    
    <bean id="galleryManagerHibernate"
    class="org.kuato.mutwiki.business.GalleryManagerHibernate">
            <property name="sessionFactory"><ref bean="sessionFactory"/></property>
            <!-- transactionManager for programmatic transactions, removed when using declarative -->
            <property name="transactionManager"><ref bean="transactionManagerHibernate"/></property>
    </bean>
    
    <bean id="gallery"
    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
            <property name="transactionManager"><ref
    bean="transactionManagerHibernate"/></property>
            <property name="target"><ref local="galleryManagerHibernate"/></property>
            <property name="transactionAttributes">
                <props>
                	<prop key="select*">PROPAGATION_REQUIRED,readOnly</prop>
                	<prop key="insert*">PROPAGATION_REQUIRED</prop>
                	<prop key="update*">PROPAGATION_REQUIRED</prop>
                    <prop key="delete*">PROPAGATION_REQUIRED</prop>
                </props>
            </property>
    </bean>
    For now I'm not using DAOs, my hibernate version of galleryManger just implements a GalleryManagerFacade to keep things simple. In my galleryManager if I try this using programmatic transation I have no problem:

    Code:
    public class GalleryManagerHibernate extends HibernateDaoSupport implements GalleryManagerFacade
    &#123;
        .... // many other select/insert/update/delete methods here
    
        public void insertPhoto&#40;GalleryPhoto photo&#41;
        &#123;
    	   TransactionStatus transaction=this.transactionManager.
            	getTransaction&#40;new DefaultTransactionDefinition&#40;&#41;&#41;;
    	  
    	   photo.setCreated&#40;new java.util.Date&#40;&#41;&#41;;
    	   getHibernateTemplate&#40;&#41;.saveOrUpdate&#40;photo&#41;;
    	    
    	   this.transactionManager.commit&#40;transaction&#41;;
        &#125;
    &#125;
    However, if I remove that programmatic transaction and try to rely on declarative transaction using the following code:
    Code:
        public void insertPhoto&#40;GalleryPhoto photo&#41;
        &#123;
    	   photo.setCreated&#40;new java.util.Date&#40;&#41;&#41;;
    	   getHibernateTemplate&#40;&#41;.saveOrUpdate&#40;photo&#41;;
        &#125;
    Then I get the following error when I try to call insertPhoto:

    javax.servlet.ServletException: Write operations are not allowed in read-only mode (FlushMode.NEVER) - turn your Session into FlushMode.AUTO respectively remove 'readOnly' marker from transaction definition
    net.sf.acegisecurity.intercept.web.SecurityEnforce mentFilter.doFilter(SecurityEnforcementFilter.java :207)
    net.sf.acegisecurity.util.FilterToBeanProxy.doFilt er(FilterToBeanProxy.java:105)
    net.sf.acegisecurity.ui.AbstractIntegrationFilter. doFilter(AbstractIntegrationFilter.java:170)
    net.sf.acegisecurity.util.FilterToBeanProxy.doFilt er(FilterToBeanProxy.java:105)
    net.sf.acegisecurity.ui.AbstractProcessingFilter.d oFilter(AbstractProcessingFilter.java:368)
    net.sf.acegisecurity.util.FilterToBeanProxy.doFilt er(FilterToBeanProxy.java:105)
    org.springframework.orm.hibernate.support.OpenSess ionInViewFilter.doFilterInternal(OpenSessionInView Filter.java:171)
    org.springframework.web.filter.OncePerRequestFilte r.doFilter(OncePerRequestFilter.java:76)

    root cause

    org.springframework.dao.InvalidDataAccessApiUsageE xception: Write operations are not allowed in read-only mode (FlushMode.NEVER) - turn your Session into FlushMode.AUTO respectively remove 'readOnly' marker from transaction definition
    org.springframework.orm.hibernate.HibernateTemplat e.checkWriteOperationAllowed(HibernateTemplate.jav a:872)
    org.springframework.orm.hibernate.HibernateTemplat e$14.doInHibernate(HibernateTemplate.java:404)
    org.springframework.orm.hibernate.HibernateTemplat e.execute(HibernateTemplate.java:243)
    org.springframework.orm.hibernate.HibernateTemplat e.saveOrUpdate(HibernateTemplate.java:402)
    org.kuato.mutwiki.business.GalleryManagerHibernate .insertPhoto(GalleryManagerHibernate.java:280)
    org.kuato.mutwiki.web.GalleryPhotoFormController.o nSubmit(GalleryPhotoFormController.java:141)
    org.springframework.web.servlet.mvc.SimpleFormCont roller.processFormSubmission(SimpleFormController. java:224)
    org.springframework.web.servlet.mvc.AbstractFormCo ntroller.handleRequestInternal(AbstractFormControl ler.java:230)
    org.springframework.web.servlet.mvc.AbstractContro ller.handleRequest(AbstractController.java:128)
    org.springframework.web.servlet.mvc.SimpleControll erHandlerAdapter.handle(SimpleControllerHandlerAda pter.java:44)
    org.springframework.web.servlet.DispatcherServlet. doService(DispatcherServlet.java:584)
    org.springframework.web.servlet.FrameworkServlet.s erviceWrapper(FrameworkServlet.java:368)
    org.springframework.web.servlet.FrameworkServlet.d oPost(FrameworkServlet.java:328)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:709)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:802)
    net.sf.acegisecurity.intercept.web.FilterSecurityI nterceptor.proceedWithObject(FilterSecurityInterce ptor.java:87)
    net.sf.acegisecurity.intercept.AbstractSecurityInt erceptor.interceptor(AbstractSecurityInterceptor.j ava:318)
    net.sf.acegisecurity.intercept.web.FilterSecurityI nterceptor.invoke(FilterSecurityInterceptor.java:7 8)
    net.sf.acegisecurity.intercept.web.SecurityEnforce mentFilter.doFilter(SecurityEnforcementFilter.java :165)
    net.sf.acegisecurity.util.FilterToBeanProxy.doFilt er(FilterToBeanProxy.java:105)
    net.sf.acegisecurity.ui.AbstractIntegrationFilter. doFilter(AbstractIntegrationFilter.java:170)
    net.sf.acegisecurity.util.FilterToBeanProxy.doFilt er(FilterToBeanProxy.java:105)
    net.sf.acegisecurity.ui.AbstractProcessingFilter.d oFilter(AbstractProcessingFilter.java:368)
    net.sf.acegisecurity.util.FilterToBeanProxy.doFilt er(FilterToBeanProxy.java:105)
    org.springframework.orm.hibernate.support.OpenSess ionInViewFilter.doFilterInternal(OpenSessionInView Filter.java:171)
    org.springframework.web.filter.OncePerRequestFilte r.doFilter(OncePerRequestFilter.java:76)

    It's as if it's totally ignoring my TransactionFactoryProxyBean that I created.

    Thanks for any help you can offer.

  • #2
    Try removing

    <prop key="hibernate.autocommit">false</prop>

    Comment


    • #3
      Thanks, but that doesn't help either. I really wanted to be sure that autocommit is not turned on so I can rollback so I thought specifying false in there would be some extra insurance Well anyway, I'll keep playing around to figure this out.

      A few other things I forgot to mention in my original post:

      Spring version: 1.1.3
      Database: Postgres 7.4.6
      Linux 2.4.19 (lol I haven't recompiled it in 2 years )

      Comment


      • #4
        It's as if it's totally ignoring my TransactionFactoryProxyBean that I created.
        I think this is the solution to this issue!!! You need to inject bean gallery into your controller instead of galleryManagerHibernate.
        HTH

        Comment


        • #5
          Omar, you are my hero!!!!

          You were absolutely right, I was still passing my galleryManagerHibernate bean to my galleryController. I fixed it as you suggested to inject the gallery bean into the galleryController and now it's running like a charm. Great call especially since I forgot to even post the galleryController configuration

          I can finally let out a huge sigh of relief now.

          Thank you very very much!

          Comment

          Working...
          X