Results 1 to 10 of 39

Thread: No transaction in transactional service called from @PostConstruct

Hybrid View

  1. #1

    Default No transaction in transactional service called from @PostConstruct

    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();
        }
    }
    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();
        }
    }
    That's all, simple as that.
    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.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    In the @PostConstruct (as with the afterPropertiesSet from the InitializingBean interface) there is no way to ensure that all the post processing is already done, so (indeed) there can be no Transactions. The only way to ensure that that is working is by using a TransactionTemplate.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3

    Default

    That doesn't sound good, actually. Isn't "afterPropertiesSet" means that the bean is fully configured and ready to fly?

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    No. It means that your bean is created and all the properties have been injected, it doesn't mean that all the other BeanPostProcessor already did their respective jobs.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5

    Default

    So, how can I be sure that when my business logic kicks in (without @PostConstruct, new user request, for example), the BeanPostProcessors already did their respective jobs?

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    If the ApplicationContext is finished and your application is up and running everything happend. All the Bean(Factory)PostProcessors have done their respective jobs. It is/can only be an issue from within @PostConstruct methods.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •