Spring @Transactional不是以RoutingDataSource开头的

时间:2014-11-28 13:47:54

标签: jpa spring-transactions spring-orm

我正在使用路由数据源,我的创建操作使用@Transactional注释进行注释。但我注意到事务没有开始或提交。以下是我的路由数据源配置。

<bean id="routingDataSource" class="com.test.dataaccess.base.dao.CustomerRoutingDataSource">
    <property name="defaultTargetDataSource" ref="testDataSource" />
     <property name="targetDataSources">
        <map key-type="java.lang.String">
            <entry key="0" value-ref="testDataSource" />
        </map>
    </property>  
</bean>

<bean class="org.springframework.orm.jpa.JpaTransactionManager"
        id="customerTransactionManager">
        <property name="entityManagerFactory" ref="customerEntityManagerFactory" />
</bean>

与我的org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean一起使用的相同数据源。

我在部署时将另一个数据源添加到路由数据源,如下所示。 Spring事务管理不起作用。

@Component
public class CustomerDataSourcePostProcessor implements ApplicationListener {

    @Autowired 
    DatasourcesDAO datasourcesDAO; 

    @Autowired
    @Qualifier("customerEntityManagerFactory")
    private LocalContainerEntityManagerFactoryBean testContentEntityManagerFactory;

    @Autowired
    @Qualifier("routingDataSource")
    private CustomerRoutingDataSource routingDataSource;

    @Autowired
    @Qualifier("customerTransactionManager")
    private JpaTransactionManager  customerTransactionManager;


    private static final Logger LOGGER = LoggerFactory.getLogger(CustomerDataSourcePostProcessor.class);

    public void onApplicationEvent(ApplicationEvent e) {
        if (e instanceof ContextRefreshedEvent) {
            loadCustomerDBConfigForServer();
        }
    }

    private void loadCustomerDBConfigForServer() {

        Map<Object, Object> databaseConfig = loadCustomerDatabaseConfig();
        routingDataSource.setTargetDataSources(databaseConfig);       
        routingDataSource.afterPropertiesSet(); 

        testContentEntityManagerFactory.setDataSource(routingDataSource);  
        testContentEntityManagerFactory.afterPropertiesSet(); 

        EntityManagerFactory emf  =testContentEntityManagerFactory.getObject(); // transaction not begin possible root cause one 
        customerTransactionManager.setEntityManagerFactory(emf);
        customerTransactionManager.afterPropertiesSet(); 
    }
}

0 个答案:

没有答案