Announcement Announcement Module
No announcement yet.
OpenSessionInView / Session is closed / TX settings confusion Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • OpenSessionInView / Session is closed / TX settings confusion


    I'm getting REALLY confused about this stuff, I'd greatly appreciate some clarification.

    Here's how I naively thought it worked - you use the OpenSessionInView filter to wrap each request in a single thread-bound session. This way you can make use of Hibernate lazy initialisation, and make use of objects loaded earlier on in future DAO calls. As long as you're on the same thread and haven't finished the request you should be ok.

    In practice, I found that I had also to wrap my beans in the TransactionFactoryProxyBean, and set all my manager methods to PROPAGATION_NOT_SUPPORTED to get things to work.

    I started off by using PROPAGATION_REQUIRED for store* & remove* methods, and PROAGATION_REQUIRED,readOnly for get* methods.

    However, this would inevitably lead to a "Session is closed" error at some point not far into things. Code such as this would fail on the second line:

    KeywordType type = keywordTypeManager.getKeywordType(typeName);
    Keyword keyword = keywordManager.getKeywordByNameAndType(name, type);
    I'd get:
    org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.SessionException: Session is closed
    I don't understand why the Session would be closed if OpenSessionInView is active, and nothing else has happened between those two manager calls!

    I'm not sure what detail it would be good to post here, here are some snippets:

    From my Spring XML config: all manager beans are wrapped with a parent of:

    	<bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
    		<property name="transactionManager" ref="txManager" />
    		<property name="transactionAttributes">
    				<prop key="store*">PROPAGATION_REQUIRED</prop>
    				<prop key="remove*">PROPAGATION_REQUIRED</prop>
    				<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
    I can see the OpenSessionInViewFilter.doFilterInternal method in the stack trace. The first manager call is passing on to a DAO which does a simple:

    getHibernateTemplate().find("from KeywordType as kt where",name);
    and returns the result, no explicit closing of sessions.

    The excecution of code is obviously moving in and out of the Transaction-wrapped Manager classes, so I'm guessing it's something to do with that - but it doesn't make sense to me. I'm getting a Session is closed error, not a transaction problem: isn't the point of OpenSessionInView to keep the Session open?

    I'm concerned that (a) I don't really understand what's going on, and (b) I seem to have to use a different config from everybody else (all the docs I've seen seem to recommend PROPAGATION_REQUIRED, whereas I can only get it to work with PROPAGATION_NOT_SUPPORTED).

    Can someone shed any light?

    Thanks in advance,