package Ġcom . example . spring boot . mult idat as ource Ċ Ċ import Ġcom . example . spring boot . mult idat as ource . datasource A . Transactional A requires New Ċ import Ġcom . example . spring boot . mult idat as ource . datasource A . mapper . DataSource A Mapper Ċ import Ġcom . example . spring boot . mult idat as ource . datasource B . Transactional B requires New Ċ import Ġcom . example . spring boot . mult idat as ource . datasource B . mapper . DataSource B Mapper Ċ import Ġcom . example . spring boot . mult idat as ource . model . Test Model Ċ import Ġorg . junit . Assert Ċ import Ġorg . junit . Test Ċ import Ġorg . junit . runner . RunWith Ċ import Ġorg . springframework . beans . factory . annotation . Autowired Ċ import Ġorg . springframework . boot . test . context . SpringBootTest Ċ import Ġorg . springframework . stereotype . Component Ċ import Ġorg . springframework . test . annotation . Rollback Ċ import Ġorg . springframework . test . context . junit 4 . Spring Runner Ċ import Ġorg . springframework . transaction . interceptor . Transaction Aspect Support Ċ Ċ @ RunWith ( Spring Runner :: class ) Ċ @ SpringBootTest Ċ class ĠMultiple DataSource Access Test Ġ{ ĊĊĠĠĠ Ġ@ Autowired ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġmanip ulator A : ĠMan ip ulator A ĊĠĠĠ Ġ@ Autowired ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġmanip ulator B : ĠMan ip ulator B ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġ// ĠBecause Ġthis Ġtest Ġrequires Ġtransactions Ġto Ġboth ĠA Ġand ĠB , ĠI Ġdon 't Ġadd Ġ@ Transactional Ġannotation Ġhere . ĊĠĠĠ Ġ// ĠBut ĠI Ġdefined Ġtwo ĠComponent Ġand Ġeach Ġhas Ġ@ Transactional Ġannotation . ĊĠĠĠ Ġ// ĠEach Ġcomponent Ġestablish Ġtransaction Ġfor Ġeach Ġdatabase . ĊĠĠĠ Ġ// ĊĠĠĠ Ġ// ĠIn Ġother Ġway , Ġyou Ġcan Ġuse ĠTransaction Template Ġto Ġexplicitly Ġestablish Ġtransactions Ġto Ġtwo Ġdatabases . ĊĠĠĠ Ġfun Ġtest () Ġ{ ĊĠĠĠĠĠĠĠ Ġmanip ulator A . action Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠAssert . assertEquals ( 1 , Ġmanip ulator A . mapper . selectAll (). size ) Ġ// ĠInsert ed Ġby ĠMan ip ulator A ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġmanip ulator B . action Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠAssert . assertEquals ( 1 , Ġmanip ulator B . mapper . selectAll (). size ) Ġ// ĠInsert ed Ġby ĠMan ip ulator B ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} Ċ } Ċ Ċ @ Component Ċ class ĠMan ip ulator A ( val Ġmapper : ĠDataSource A Mapper ) Ġ{ ĊĊĠĠĠ Ġ@ Transactional A requires New ĊĠĠĠ Ġfun Ġaction ( callback : Ġ() Ġ-> ĠUnit ) Ġ{ ĊĠĠĠĠĠĠĠ Ġmapper . createTable () ĊĠĠĠĠĠĠĠ ĠAssert . assertEquals ( 0 , Ġmapper . selectAll (). size ) ĊĠĠĠĠĠĠĠ Ġmapper . insert ( Test Model ( 1 2 3 , Ġ" test Ġrecord ")) ĊĠĠĠĠĠĠĠ ĠAssert . assertEquals ( 1 , Ġmapper . selectAll (). size ) ĊĊĠĠĠĠĠĠĠ Ġ// ĠExecute Ġgiven Ġcallback Ġwithin Ġtransaction Ġof ĠA ĊĠĠĠĠĠĠĠ Ġcallback () ĊĊĠĠĠĠĠĠĠ ĠTransaction Aspect Support . current Transaction Status (). set Rollback Only () ĊĠĠĠ Ġ} Ċ } Ċ Ċ @ Component Ċ class ĠMan ip ulator B ( val Ġmapper : ĠDataSource B Mapper ) Ġ{ ĊĊĠĠĠ Ġ@ Transactional B requires New ĊĠĠĠ Ġ@ Rollback ĊĠĠĠ Ġfun Ġaction ( callback : Ġ() Ġ-> ĠUnit ) Ġ{ ĊĠĠĠĠĠĠĠ Ġmapper . createTable () ĊĠĠĠĠĠĠĠ ĠAssert . assertEquals ( 0 , Ġmapper . selectAll (). size ) Ġ// ĠShould Ġbe Ġzero . ĠDifferent Ġdatabase Ġwith ĠMan ip ulator B ĊĠĠĠĠĠĠĠ Ġmapper . insert ( Test Model ( 1 2 3 , Ġ" test Ġrecord ")) ĊĠĠĠĠĠĠĠ ĠAssert . assertEquals ( 1 , Ġmapper . selectAll (). size ) ĊĊĠĠĠĠĠĠĠ Ġcallback () ĊĊĠĠĠĠĠĠĠ ĠTransaction Aspect Support . current Transaction Status (). set Rollback Only () ĊĠĠĠ Ġ} Ċ }