Last active
November 2, 2018 00:03
-
-
Save kariyayo/69bd554d1dccb4c16b11 to your computer and use it in GitHub Desktop.
Springで自前でDataSource欲しいとき
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Configuration | |
public class DatabaseConfig { | |
@Bean | |
public DataSource ds1() { | |
org.apache.tomcat.jdbc.pool.DataSource ds = new org.apache.tomcat.jdbc.pool.DataSource(); | |
ds.setDriverClassName("driverName"); | |
ds.setUrl("url"); | |
ds.setUsername("username"); | |
ds.setPassword("password"); | |
ds.setDefaultAutoCommit(false); | |
return ds; | |
} | |
@Bean | |
public DataSource ds2() { | |
org.apache.tomcat.jdbc.pool.DataSource ds = new org.apache.tomcat.jdbc.pool.DataSource(); | |
ds.setDriverClassName("driverName"); | |
ds.setUrl("url"); | |
ds.setUsername("username"); | |
ds.setPassword("password"); | |
ds.setDefaultAutoCommit(false); | |
return ds; | |
} | |
@Bean | |
@DependsOn({"ds1", "ds2"}) | |
public DataSource routingDataSource() { | |
AbstractRoutingDataSource routingDataSource = ・・・ | |
Map<Object, Object> ds = new HashMap<>(); | |
ds.put("a", ds1()); | |
ds.put("b", ds2()); | |
routingDataSource.setTargetDataSources(ds); | |
return routingDataSource; | |
} | |
@Bean | |
@Primary | |
@DependsOn({"routingDataSource"}) | |
public DataSource dataSource() { | |
return new TransactionAwareDataSourceProxy(routingDataSource()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment