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

  • Transaction Propagations and Hibernate 4

    We had a project that was using Spring 3.0.x and Hibernate 3.5.x, after migrating to Spring 3.1.x and Hibernate 4.0.x we faced a strange (may be not ) problem. we are getting
    Code:
    org.hibernate.HibernateException: No Session found for current thread
    	at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
    	at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:883)
    Only way not to get this error is to use @Transactional annotation with propagation = Propagation.REQUIRED on service methods, but what if we want to use other propagation methods ?

    The config file looks like.
    Code:
    	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    		destroy-method="close" p:driverClassName="${database.driver.classname}"
    		p:url="${database.url}" p:username="${database.username}" p:password="${database.password}"
    		p:poolPreparedStatements="${database.poolstatements}" p:initialSize="${database.initial.size}"
    		p:maxActive="60" p:maxIdle="20" p:maxWait="120000" p:minIdle="10"
    		p:validationQuery="${database.validation.query}" p:testOnBorrow="true"
    		p:testOnReturn="false" p:testWhileIdle="false" />
    
    	<bean id="baseSessionFactory" abstract="true">
    		<property name="dataSource" ref="dataSource" />
    		<property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
    				<prop key="hibernate.connection.useUnicode">true</prop>
    				<prop key="hibernate.connection.characterEncoding">UTF8</prop>
    				<prop key="hibernate.jdbc.batch_size">${hibernate.batch_size}</prop>
    				<prop key="hibernate.bytecode.use_reflection_optimizer">true</prop>
    				<prop key="hibernate.cache.use_query_cache">true</prop>
    				<prop key="hibernate.cache.use_second_level_cache">true</prop>
    				<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
    				</prop>
    				<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
    				<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
    				<prop key="hibernate.use_sql_comments">${hibernate.use_sql_comments}</prop>
    			</props>
    		</property>
    	</bean>
    
    	<bean id="sessionFactory"
    		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
    		parent="baseSessionFactory" p:annotatedPackages-ref="hibernateAnnotatedPackages"
    		p:annotatedClasses-ref="hibernateAnnotatedClasses" />
    
    	<!-- Abstract Dao Definitions -->
    	<bean abstract="true" id="baseSessionFactoryAwareDao"
    		p:sessionFactory-ref="sessionFactory" />
    
    	<bean id="transactionManager"
    		class="org.springframework.orm.hibernate4.HibernateTransactionManager"
    		p:sessionFactory-ref="sessionFactory" />
    
    	<tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager"/>
    The service class
    Code:
    @Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
    @Service("userService")
    public class UserServiceImpl implements UserService {
    
        @Autowired
        @Qualifier("userDao")
        private UserDao userDao = null;
    
        // ~DAO's
    
        @Override    
        public void getUser (final Long user)
        {
        return userDao.getUser(id);
        }
    }
    As you can see the service class has the @Transactional annotation but still the exception occurs on any getUser call (Even if we have @Transactional(readOnly = true, propagation = Propagation.SUPPORTS) on the getUser method).

  • #2
    Propagation.SUPPORTS doesn't start a transaction only REQUIRED or REQUIRES_NEW does that. SUPPORTS means basically I'm fine with or without a transaction (basically I don't care). However if you want spring to get a current session that will only happen when there is a transaction active (else it will not work).

    Comment


    • #3
      Ok, thank you. The problem was that we were using HibernateTemplate and it was opening a new session if there was none.

      Comment

      Working...
      X