Spring boot Read write Split / Master - Slave / Multiple Databases -


i following link

https://github.com/kwon37xi/replication-datasource

i have implemented code still both service functions using same database(one marked primary)

service class

public class tableservice{      @autowired     private table1repo t1repo;     @transactional(readonly = false)     public void savetable1(table1 t,int a, table1 t2){             try{                 t1repo.save(t2);             }             catch(exception e){                 system.out.println("inside");             }      }      @transactional(readonly = true)     public table1 gettable(int id){         return t1repo.findone(id);     }   } 

then added 2 class(from link)

replicationroutingdatasource

public class replicationroutingdatasource extends abstractroutingdatasource {      @override     protected object determinecurrentlookupkey() {         string datasourcetype = transactionsynchronizationmanager.iscurrenttransactionreadonly() ? "read" : "write";         return datasourcetype;     } } 

withroutingdatasourceconfig

@configuration public class withroutingdatasourceconfig {      /*@bean(destroymethod = "shutdown")*/     @bean     @primary     @configurationproperties(prefix="datasource.primary")     public datasource writedatasource() {        /* embeddeddatabasebuilder builder = new embeddeddatabasebuilder()                 .setname("routingwritedb")                 .settype(embeddeddatabasetype.h2)                 .setscriptencoding("utf-8")                 .addscript("classpath:/writedb.sql");         return builder.build();*/         return datasourcebuilder.create().build();     }  /*    @bean(destroymethod = "shutdown")*/     @bean     @configurationproperties(prefix="datasource.secondary")     public datasource readdatasource() {         /*embeddeddatabasebuilder builder = new embeddeddatabasebuilder()                 .setname("routingreaddb")                 .settype(embeddeddatabasetype.h2)                 .setscriptencoding("utf-8")                 .addscript("classpath:/readdb.sql");         return builder.build();*/         return datasourcebuilder.create().build();     }      /**      * {@link org.springframework.jdbc.datasource.lookup.abstractroutingdatasource}는      * {@link org.springframework.beans.factory.initializingbean}을 구현하므로,      * 명시적으로 afterpropertiesset()메소드를 호출하거나      * 별도 @bean으로 만들어 spring life cycle을 타도록 해야 한다.      */     @bean     public datasource routingdatasource(@qualifier("writedatasource") datasource writedatasource, @qualifier("readdatasource") datasource readdatasource) {         replicationroutingdatasource routingdatasource = new replicationroutingdatasource();          map<object, object> datasourcemap = new hashmap<object, object>();         datasourcemap.put("write", writedatasource);         datasourcemap.put("read", readdatasource);         routingdatasource.settargetdatasources(datasourcemap);         routingdatasource.setdefaulttargetdatasource(writedatasource);          return routingdatasource;     }      /**      * {@link org.springframework.jdbc.datasource.lazyconnectiondatasourceproxy}로 감싸서      * 트랜잭션 동기화가 이루어진 뒤에 실제 커넥션을 확보하도록 해준다.      *      * @param routingdatasource      * @return      */     @bean     public datasource datasource(@qualifier("routingdatasource") datasource routingdatasource) {         return new lazyconnectiondatasourceproxy(routingdatasource);     } } 

application.prop file

server.port=8089  spring.jpa.show-sql = true spring.jpa.properties.hibernate.show_sql=true  # primary datasource configuration datasource.primary.url=jdbc:mysql://127.0.0.1:3306/jpa datasource.primary.username=root datasource.primary.password=root # of other spring supported properties below...  # secondary datasource configuration datasource.secondary.url=jdbc:mysql://127.0.0.1:3306/jpa2 datasource.secondary.username=root datasource.secondary.password=root 

repository

public interface table1repo extends jparepository<table1, integer>{} 

issue both service functions using primary database. missing. have these class. rest have 1 controller

edited have made code work adding class

@configuration @enabletransactionmanagement @enablejparepositories(basepackages="com.example") public class replicationdatasourceapplicationconfig {      @bean     public localcontainerentitymanagerfactorybean entitymanagerfactory(@qualifier("datasource") datasource datasource) {         localcontainerentitymanagerfactorybean emfb = new localcontainerentitymanagerfactorybean();         emfb.setdatasource(datasource);         emfb.setpackagestoscan("com.example");                 hibernatejpavendoradapter jpavendoradapter = new hibernatejpavendoradapter();         emfb.setjpavendoradapter(jpavendoradapter);          return emfb;     }      @bean     public platformtransactionmanager transactionmanager(entitymanagerfactory entitymanagerfactory) {         jpatransactionmanager transactionmanager = new jpatransactionmanager();         transactionmanager.setentitymanagerfactory(entitymanagerfactory);         return transactionmanager;     }      @bean     public persistenceexceptiontranslationpostprocessor exceptiontranslationpostprocessor() {         return new persistenceexceptiontranslationpostprocessor();     } } 

i'm writer of link refered.

which data source use table1repo?

you have inject specific bean "datasource" jdbc call.

i guess @primary "writedatasource" injected repository.

try change @primary "datasource" or find way inject "datasource" repository.


Comments