Dear friends!
For some reason transaction doesn't start when transactional service method is called from @PostConstruct method.
Following are the context and the components:
Code:<?xml version="1.0" encoding="UTF-8"?> <beans ...> <context:component-scan base-package="com.bla"/> <bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">...</bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="c3p0DataSource"/> <property name="cacheProvider" ref="cacheProvider"/> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop> </props> </property> <property name="annotatedPackages"> <list>...</list> </property> </bean> </beans>Code:@Component public class Bootstraper { @Autowired private HeatCacheService heatCacheService; @PostConstruct public void bootstrap() { heatCacheService.heatCache(); } }Code:@Service @Transactional public class HeatCacheService { @Autowired private HeatCacheDAO heatCacheDAO; @Transactional(readOnly = true) public void heatCache() { this.heatCacheDAO.heatCache(); } }That's all, simple as that.Code:@Repository public class HeatCacheDAO extends AbstractDAO { public void heatCache() { loadEntity(SomeEntity.class); ... } private void loadEntity(Class clazz) { Session session = sessionFactory.getCurrentSession(); session.createCriteria(clazz).list(); } }
The exception I get is "No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here", which means that no transaction exists when calling sessionFactory.getCurrentSession().
If I open the transaction manully (using TransactionTemplate in the @PostConstruct method) it works like charm, but I want to stay with declarative transactions as much as possible.
Thank you in advance.
Baruch.


Reply With Quote