How to java-configure separate datasources for spring batch data and business data? Should I even do it?

后端 未结 7 1528
感情败类
感情败类 2020-11-29 22:33

My main job does only read operations and the other one does some writing but on MyISAM engine which ignores transactions, so I wouldn\'t require necessarily tr

相关标签:
7条回答
  • 2020-11-29 23:16

    Assuming you have 2 data sources, one for spring batch metadata such as job details[lets say CONFIGDB] and other for your business data [lets say AppDB]:

    Inject CONFIGDB into jobRepository, like this:

     <bean id="jobRepository"
        class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
        <property name="transactionManager" ref="transactionManager" />
        <property name="dataSource" ref="CONFIGDB" />
        <property name="databaseType" value="db2" />
        <property name="tablePrefix" value="CONFIGDB.BATCH_" />
      </bean>
    

    Now you can inject the AppDB dartasource into your DAO's OR Writers if any like..

       <bean id="DemoItemWriter" class="com.demoItemWriter">
         <property name="dataSource" ref="AppDB" />     
       </bean>
    

    OR

    you can do define a resource and Inject this AppDB with jndi lookup in the class where its needed like:

    public class ExampleDAO {
    
    @Resource(lookup = "java:comp/env/jdbc/AppDB")
    DataSource ds;
    

    }

    0 讨论(0)
提交回复
热议问题