Announcement Announcement Module
Collapse
No announcement yet.
Transactional init-method problems Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Transactional init-method problems

    Greetings, guys!

    I've noticed very strange behaviour when added @Transactional(propagation=Propagation.REQUIRED) Spring annotation to the init method of the Spring bean. This bean is supposed to populate my DB with some basic data needed for the startup of the main application.

    Primarily, I did the transaction management by hand, i.e. called beginTransaction() and commit() inside the init method, and the data were put to db quite smoothly. But after switching to spring transaction management, I've noticed that some data is not saved. Specifically, I've noticed that cascade saves did't worked.

    My transaction config look like

    Code:
        <!-- Transaction Management -->
        <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="dataSource" ref="dataSourceProxy"/>
            <property name="sessionFactory" ref="sessionFactory"/>
        </bean>
    
        <bean id="txInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
            <property name="transactionManager" ref="transactionManager"/>
            <property name="transactionAttributeSource">
                <bean class="org.springframework.transaction.annotation.AnnotationTransactionAttributeSource"/>
            </property>
        </bean>
    
        <bean class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">
            <property name="transactionInterceptor" ref="txInterceptor"/>
        </bean>
    My first assumption was that the transaction management system just didn't have time to startup correctly, so I put a depends-on="transactionManager" into my database populating bean, with no effect.

    Can someone advise me, why such configuration of transaction management system might brake the cascades?

    Thanks in advance!

  • #2
    I don't think the init method is proxied hence it won't be providing a transaction if you using @Transactional.

    Comment


    • #3
      Ok, sounds reasonable. How would you solve this issue? What if I really need to make initialization of the database transactional (for instance, in order to use this method in unit tests)?

      Thanks in advance!

      Comment


      • #4
        Let the init method call another method, which is Transactional. For instance some method in a service.

        Comment


        • #5
          Or you could use TransactionTemplate.

          Comment

          Working...
          X