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
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 ?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)
The config file looks like.
The service classCode:<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"/>
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).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); } }


) problem. we are getting
Reply With Quote