Dylan
Dylan

Reputation: 955

From TransactionProxyFactoryBean to a Declarative transaction management?

I want to migrate from old style of transaction management with TransactionProxyFactoryBean to a Declarative transaction management recommended by spring. So that will be possible to avoid exceptions with transactions that appear from time to time.

This is my configuration xml file:

<beans  xmlns=...>

    <context:annotation-config/>
    <context:component-scan base-package="prof" />

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation">
            <value>WEB-INF/classes/hibernate.cfg.xml</value>
        </property>
    </bean>

    <import resource="prof-dao-spring.xml" />

    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
      <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
        <property name="transactionManager">
          <ref bean="transactionManager"/>
        </property>
        <property name="transactionAttributes">
          <props>
            <prop key="save*">PROPAGATION_REQUIRED</prop>
            ...
            <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
          </props>
        </property>
    </bean>

    <bean id="ProfileService" parent="baseTransactionProxy">
     <property name="target">
      <bean class="tv.clever.hibernate.service.ProfileService"></bean>
     </property>
    </bean>

</beans>

ProfileService looks like:

@Component
public class ProfileService {

@Autowired
@Qualifier("baseDAO")
protected BaseDAO baseDAO;

private static ProfileService profileService;

public ProfileService() {

    setProfileService(this);
}

public void setProfileService(ProfileService ps) {

            profileService = ps;
}

public void save(final Collection transientObjects) {

    baseDAO.save(transientObjects);
}

 ...
}

From where do I need to start?

Upvotes: 1

Views: 1054

Answers (1)

M. Deinum
M. Deinum

Reputation: 124441

Assuming you want to use annotations slap a @Transactional on your service class, add <tx:annotation-driven /> to your configuration and remove the TransactionalProxyFactoryBean declaration and all beans using that as a parent.

Additional pro-tips:

  1. Use @Service for service classes and @Repository for daos
  2. <context:annotation-config /> is implied by <context:component-scan />

Your service

@Service
@Transactional
public class ProfileService { ... }

Configuration

<beans  xmlns=...>

    <context:component-scan base-package="prof" />

    <import resource="prof-dao-spring.xml" />

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation">
            <value>WEB-INF/classes/hibernate.cfg.xml</value>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
      <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <tx:annotation-driven />

</beans>

Restart application.

Upvotes: 3

Related Questions