I'm trying to use delegated Hibernate transactions using Spring in a demo standalone application, using a DAO layer and a Service layer .
I've properly set configuration, and i've unit-tested that usage of @Transactional annotation on DAO methods is working fine, but when i move this annotation to a service layer i get an:
org.hibernate.HibernateException: No Session found for current thread
I provide the most relevant parts of my code, hoping you can throw me a hint to understand what's going on here.
<beans ...>
<context:component-scan base-package="com.genericdao" />
<!-- delegat transactions -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- sessionFactory config -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingDirectoryLocations">
<list>
<value>classpath:com/genericdao/hbm</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SybaseAnywhereDialect</prop>
<!--prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop--><!-- i tried commenting this line -->
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop><!-- i know this is provided by default -->
</props>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
... i provide here configuration
</bean>
</beans>
getSession() just performs sessionFactory.getCurrentSession(); and sessionFactory is autowired in GenericDaoHibernateImpl
@Repository("userLoginDao")
public class UserLoginDaoImpl extends GenericDaoHibernateImpl<UserLogin, Integer> implements UserLoginDao{
@Override
//@Transactional(readOnly=true) // This works when i unit-test!! But I don't want to use @Transactional here!!
public List<UserLogin> findAll() {
boolean active = TransactionSynchronizationManager.isActualTransactionActive(); // always true if i use @Transactional
Query query = getSession().createQuery("from UserLogin");
return (List<UserLogin>) query.list();
}
}
@Service("pruebaService")
public class PruebaServiceImpl implements PruebaService{
private static final ApplicationContext ctx;
static{
ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
}
/**********************************************************
* HERE i want to use @Transactional, but it doesn't work because
* i get the org.hibernate.HibernateException: No Session found for current thread
* when i test invocation of this method...
* NOTE: I want to state, that if i uncomment the Dao @Transactional line
* then this works!!, but as i mentioned before i really don't want to have transactions on DAO methods, just Service Methods!!
*/
@Override
@Transactional(readOnly=true)
public List<UserLogin> obtenerTodasLasCuentas() {
UserLoginDao bean = (UserLoginDao) ctx.getBean("userLoginDao");
List<UserLogin> result = bean.findAll();
return result;
}
}
I really did a serch on this topic but i couldn't find a proper input... hope you can help with this... thanks.
Here's the testing relevant code i'm using
@Test
public void selectTest(){
pruebaService = (PruebaService) ctx.getBean("pruebaService");
Assert.assertNotNull(pruebaService); // This assert is good, so i know service is properly injected
List<UserLogin> result = pruebaService.obtenerTodasLasCuentas();
Assert.assertNotNull(result); // This line is never reached because of the exception i mentioned!!
}
Ok I made this work, I just modified service to this:
@Service("pruebaService")
public class PruebaServiceImpl implements PruebaService{
private @Autowired UserLoginDao userLoginDao; // injected by Spring when I create the ApplicationContext in my unit-testing class
@Override
@Transactional(readOnly=true)
public List<UserLogin> obtenerTodasLasCuentas() {
List<UserLogin> result = userLoginDao.findAll();
return result;
}
}
And it was because of misleading the two times created (one in my service and one on my unit-testing classes):
new ClassPathXmlApplicationContext("applicationContext.xml");
If there's something else you want to point... i'd really appreciate your comments... thank you so much for your help guys
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.