Spring中具有多个事务管理器的多个DataSource

时间:2014-04-13 01:55:57

标签: java spring transactions datasource

我在Junit应用程序上下文文件中定义了三个DataSource(JDBC)。其中两个需要进行交易管理;使用这两个数据源时,我不必链接任何方法(它们完全相互独立)。

当我使用dataSource2的单个事务管理器时,我没有遇到任何问题,即使dataSource3被使用但未在相应的方法中进行管理。尽管如此,在需要同时管理来自仅使用dataSource3的各种DAO类的方法时,我添加了第二个事务 - txManager2。上下文文件包含以下内容:

<context:component-scan base-package="my.pkg" />

<bean id="dataSource1"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="oracle.jdbc.OracleDriver" />
    <property name="url" value="jdbc:oracle:thin:@host1:1521:dbsid1" />
<property name="username" value="username1" />
<property name="password" value="password1" />
</bean>

<bean id="dataSource2"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="oracle.jdbc.OracleDriver" />
    <property name="url" value="jdbc:oracle:thin:@host2:1521:dbsid2" />
<property name="username" value="username2" />
<property name="password" value="password2" />
</bean>

<bean id="dataSource3"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="oracle.jdbc.OracleDriver" />
    <property name="url" value="jdbc:oracle:thin:@host3:1521:dbsid3" />
<property name="username" value="username3" />
<property name="password" value="password3" />
</bean>

<tx:annotation-driven/>

<bean id="txManager1" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <qualifier value="txManager1"/>
<property name="dataSource2" ref="dataSource2"/>
</bean>

<bean id="txManager2" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <qualifier value="txManager2"/>
<property name="dataSource3" ref="dataSource3"/>
</bean>

由于正在定义多个事务管理器,因此我限定它们具有自己的值。如您所见,dataSource3正在使用setter方法注入:

package my.pkg;

@Component
public class MyDAO {

    private DataSource dataSource3;

    // Read only from datasource
    @Autowired
    @Qualifier("dataSource")
    public void setDataSource(DataSource ds) {
        template = new NamedParameterJdbcTemplate(ds);
    }

    // Performs reads/updates/inserts from datasource2
    @Autowired
    @Qualifier("dataSource2")
    public void setDataSource2(DataSource ds) {
        iTemplate = new NamedParameterJdbcTemplate(ds);
    }

    // Performs reads/updates/inserts from datasource3
    @Autowired
    @Qualifier("dataSource3")
    public void setDataSource3(DataSource ds) {
        dataSource3 = ds;
        uTemplate = new NamedParameterJdbcTemplate(ds);
    }

    @Transactional("txManager1")    
    public String insertProcs() { }

    @Transactional("txManager2")    
    public String updateProcs() { }
}

但是,在运行 Junit单元测试后,我会得到以下结果:

java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:308)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'txManager2' defined in class path resource [test-context.xml]: 

Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'dataSource3' of bean class 

[org.springframework.jdbc.datasource.DataSourceTransactionManager]: Bean property 'dataSource3' is not writable or has an invalid setter method. Does the parameter 

type of the setter match the return type of the getter?
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1363)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1085)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:516)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:455)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:192)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
        at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:84)
        at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:1)
        at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:280)
        at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:304)
        ... 24 more
    Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'dataSource3' of bean class 

    [org.springframework.jdbc.datasource.DataSourceTransactionManager]: Bean property 'dataSource3' is not writable or has an invalid setter method. Does the parameter 

    type of the setter match the return type of the getter?
        at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:1052)
        at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:921)
        at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:76)
        at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:58)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1360)

知道问题的根源在哪里?

更新

根据接受的答案,进行了以下更改以获得预期的功能:

<tx:annotation-driven transaction-manager="txManager1"/>

<bean id="txManager1" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <qualifier value="txManager1"/>
    <property name="dataSource" ref="dataSource2"/>
</bean>

<bean id="txManager2" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <qualifier value="txManager2"/>
    <property name="dataSource" ref="dataSource3"/>
</bean>

@Qualifier("dataSource")应该已经更正了(我的错字):

    // Read only from datasource
    @Autowired
    @Qualifier("dataSource1")
    public void setDataSource1(DataSource ds) {
        template = new NamedParameterJdbcTemplate(ds);
    }

1 个答案:

答案 0 :(得分:10)

您的配置存在一些问题:

<property name="dataSource2" ref="dataSource2"/>
<property name="dataSource3" ref="dataSource3"/>

bean名称不同,但属性名称不是。您应该将其更改为:

<property name="dataSource" ref="dataSource2"/>
<property name="dataSource" ref="dataSource3"/>

例如:分别在txManager1和txManager2 bean的setter属性中。

第二:@Qualifier("dataSource2")@Qualifier("dataSource3")是错误的,因为您正在验证TX经理bean,而不是数据源bean。

您可以改为使用@Resource("dataSource2")@Resource("dataSource3"),也可以限定数据源bean。

第三:@Qualifier("dataSource")。请注意,使用限定符和bean的名称不会投诉(注意:它的 dataSource1

最后,您已定义tx:annotation-driven。它需要一个bean名称为 transactionManager 的事务管理器。你也没有给它定义bean的定义,所以这也会失败。