Guest User

Untitled

a guest
Jul 17th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
  2.  
  3. public class MultitenantDataSource extends AbstractRoutingDataSource {
  4. @Override
  5. protected Object determineCurrentLookupKey() {
  6. return TenantContext.getCurrentTenant();
  7.  
  8. }
  9. }
  10.  
  11. public class TenantContext {
  12. private static ThreadLocal<Object> currentTenant = ThreadLocal.withInitial(() -> "TenantTwo") ;
  13.  
  14. public static void setCurrentTenant(Object tenant) {
  15. currentTenant.set(tenant);
  16. }
  17.  
  18. public static Object getCurrentTenant() {
  19. return currentTenant.get();
  20. }
  21. }
  22.  
  23. import com.zaxxer.hikari.HikariDataSource;
  24. import javax.sql.DataSource;
  25. import org.springframework.beans.factory.annotation.Autowired;
  26. import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
  27. import org.springframework.boot.context.properties.ConfigurationProperties;
  28. import org.springframework.context.annotation.Bean;
  29. import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
  30. import org.springframework.context.annotation.Configuration;
  31. import ebi.zmap.MultitenantDataSource; // здесь импортируется ваш класс из первого файла
  32. import java.io.*;
  33. import java.nio.file.Paths;
  34. import java.util.*;
  35. import java.util.Properties;
  36.  
  37. @Configuration
  38. public class MultitenantConfiguration {
  39.  
  40. @Autowired
  41. private DataSourceProperties properties;
  42.  
  43. /**
  44. * Defines the data source for the application
  45. * @return
  46. */
  47. @Bean
  48. @ConfigurationProperties(
  49. prefix = "spring.datasource"
  50. )
  51. public DataSource dataSource() {
  52. AbstractRoutingDataSource dataSource = new MultitenantDataSource();
  53.  
  54. Map<Object,Object> targetDataSources = new HashMap<>();
  55.  
  56. targetDataSources.put("TenantOne", tenantOne());
  57. targetDataSources.put("TenantTwo", tenantTwo());
  58. targetDataSources.put(null, tenantOne());
  59.  
  60. dataSource.setTargetDataSources(targetDataSources);
  61.  
  62. dataSource.afterPropertiesSet();
  63.  
  64. return dataSource;
  65. }
  66.  
  67. public DataSource tenantOne() {
  68.  
  69. HikariDataSource dataSource = new HikariDataSource();
  70.  
  71. dataSource.setInitializationFailTimeout(0);
  72. dataSource.setMaximumPoolSize(5);
  73. dataSource.setDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource");
  74. dataSource.addDataSourceProperty("url", "jdbc:mysql://127.0.0.1:3306/annotrack");
  75. dataSource.addDataSourceProperty("user", "root");
  76. dataSource.addDataSourceProperty("password", "");
  77.  
  78. return dataSource;
  79. }
  80.  
  81. public DataSource tenantTwo() {
  82.  
  83. HikariDataSource dataSource = new HikariDataSource();
  84.  
  85. dataSource.setInitializationFailTimeout(0);
  86. dataSource.setMaximumPoolSize(5);
  87. dataSource.setDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource");
  88. dataSource.addDataSourceProperty("url", "jdbc:mysql://127.0.0.1:3306/annotrackm");
  89. dataSource.addDataSourceProperty("user", "root");
  90. dataSource.addDataSourceProperty("password", "");
  91.  
  92. return dataSource;
  93. }
  94.  
  95. /**
  96. * Creates the default data source for the application
  97. * @return
  98. */
  99. private DataSource defaultDataSource() {
  100. HikariDataSource dataSource = new HikariDataSource();
  101.  
  102. dataSource.setInitializationFailTimeout(0);
  103. dataSource.setMaximumPoolSize(5);
  104. dataSource.setDataSourceClassName("com.mysql.cj.jdbc.Driver");
  105. dataSource.addDataSourceProperty("url", "jdbc:mysql://193.62.52.185:3306/annotrack");
  106. dataSource.addDataSourceProperty("user", "admin");
  107. dataSource.addDataSourceProperty("password", "interestingtimes");
  108.  
  109. return dataSource;
  110. }
  111. }
  112.  
  113. TenantContext.setCurrentTenant(db);
Add Comment
Please, Sign In to add comment