Announcement Announcement Module
No announcement yet.
Transaction Management with CXF and Hibernate Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Transaction Management with CXF and Hibernate

    Dear All,

    Although I had used Spring before, I am not experienced in transaction management and I think I am having a challenging issue.

    For data persistence, I am using Hibernate (with MySQL) and for presentation I am using Web Services with CXF. (over tomcat 7.0) Lazy initialization is needed in my project, so it is active. I am using Spring version 3.5.

    In terms of transaction management, what I basically need is something like the following: a transaction/session is started once an HTTP request is made. The data is retrieved to instantiate the objects, as normal, and these objects are serialized to XML to do soap response, by CXF. Once the serialization is completed, the transaction/session needs to be terminated.

    Although it sounds fairly, simple; I couldn't manage to do that and I think I am running out of options.

    Note: I am using XML configuration instead of annotations.

    At first, I tried to configure HibernateTransactionManager using "tx:advice" tags and specified the propagation methods. However, I had LazyInitializationException, and I suppose this is because JAXB is performing object serialization after I close the transaction.

    Then, I heard about OpenSessionInViewFilter and configured this. In fact, I am a little bit confused about it. Its flushing mode is NEVER by default, which is obviously problematic for me, so I tried to change it to AUTO. However, when I monitor user connections through MySQL Admin interface, the connection begins before I make the request (strangely) and it is not disposed, even if the request is completed. This is bad because after some time, I receive "broken pipe" errors and as far as I understood this is because session remains opened for a long time.

    Below, I will provide related piece of codes that could be helpful for those who would like to help me.

    cxf.xml (a.k.a application context)
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		<property name="configLocation" value="classpath:com/algoritix/elections/persistence/hibernate/resources/hibernate.cfg.xml" />

    From web.xml:
    For my prior approach using HibernateTransactionManager, I had these in my application context file: ( this was before I was using filtering)

     <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        	<property name="sessionFactory" ref="sessionFactory" />
    	    <aop:pointcut id="serviceMethods" expression="execution(* com.algoritix.elections.webservices.presentation.*ServiceImpl.*(..))"/>
    	    <aop:advisor advice-ref="txRetrievalAdvice" pointcut-ref="serviceMethods"/>
    	<tx:advice id="txRetrievalAdvice" transaction-manager="transactionManager">
    			<tx:method name="get*" propagation="REQUIRES_NEW"/>

    Interesting thing here is even if I get LazyInitializationException, the opened session seems to remain in sleep.

    And here is a sample code from my repository class for object retrieval:
    	public Election getByName(String name) 
    		Criteria crit = sessionFactory.getCurrentSession().createCriteria(Election.class);
    		crit.add(Restrictions.eq("name", name));
    		return (Election) crit.list().get(0);

    I am having very difficult time adjusting all these... I would be really grateful if you could help me.

    Thanks in advance.

  • #2
    I don't understand how your application work :
    (for instance, where is the class that describe your web-service? When a request is made, what is the flow to the DAO...)

    It seems easy for me :
    You declare a DAO
    You declare a 'business' service that use this DAO (on which you set the transaction)
    You declare a service that will be used as webservice.

    The 'business' service is injected inside the 'web service'. The web service will just call a transactional method of your business service.


    • #3
      The fact that you use a filter doesn't mean you don't need transactions anymore! OpenSessionInView != Transaction (I suggest a read of the javadocs and that pattern in general). Post your hibernate configuration (you left out the hibernate.cfg.xml)