Guest User

Untitled

a guest
Jun 3rd, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. (For simplicity sake,
  2. domain : localhost:8080 ---> hem1 DB
  3. domain : 127.0.0.1:8080 ---> hem2 DB
  4. )
  5.  
  6. public class MyRoutingDataSource extends AbstractRoutingDataSource{
  7. @Override
  8. protected Object determineCurrentLookupKey() {
  9.  
  10. /*
  11. * this is derived from threadlocal set by filter for each web
  12. * request
  13. */
  14. return SessionUtil.getDB();
  15. }
  16. }
  17.  
  18. package com.hemant.basic.dataSource;
  19.  
  20. import java.beans.PropertyVetoException;
  21. import java.util.HashMap;
  22. import java.util.Map;
  23.  
  24. import javax.naming.ConfigurationException;
  25. import javax.sql.DataSource;
  26.  
  27. import org.springframework.context.annotation.Bean;
  28. import org.springframework.context.annotation.Configuration;
  29.  
  30. import com.mchange.v2.c3p0.ComboPooledDataSource;
  31.  
  32. @Configuration
  33. public class DBConfig {
  34.  
  35. @Bean(name = "dataSource")
  36. public DataSource dataSource() throws PropertyVetoException,
  37. ConfigurationException {
  38. MyRoutingDataSource routingDB = new MyRoutingDataSource();
  39. Map<Object, Object> targetDataSources = datasourceList();
  40.  
  41. // hem1 is the default target DB
  42. routingDB.setDefaultTargetDataSource(targetDataSources.get(1));
  43. routingDB.setTargetDataSources(targetDataSources);
  44. routingDB.afterPropertiesSet();
  45. return routingDB;
  46. }
  47.  
  48. private Map<Object, Object> datasourceList() throws PropertyVetoException,
  49. ConfigurationException {
  50. final Map<Object, Object> datasources = new HashMap<Object, Object>();
  51. ComboPooledDataSource datasource = null;
  52. for (int id = 1; id <= 2; id++) {
  53. datasource = getDatasource(id);
  54. datasources.put(id, datasource);
  55. }
  56. return datasources;
  57. }
  58.  
  59. private ComboPooledDataSource getDatasource(int id)
  60. throws PropertyVetoException, ConfigurationException {
  61. ComboPooledDataSource datasource = new ComboPooledDataSource();
  62.  
  63. // set the connection pool properties
  64. datasource.setJdbcUrl("jdbc:postgresql://localhost/hem" + id);
  65. datasource.setUser("hemant");
  66. datasource.setPassword("");
  67. datasource.setDriverClass("org.postgresql.Driver");
  68. datasource.setMaxPoolSize(30);
  69. datasource.setInitialPoolSize(10);
  70. datasource.setMinPoolSize(10);
  71.  
  72. return datasource;
  73. }
  74. }
  75.  
  76. # Hibernate ddl auto (create, create-drop, update)
  77. spring.jpa.hibernate.ddl-auto = update
  78.  
  79. [main] org.hibernate.tool.hbm2ddl.SchemaUpdate : HHH000228: Running hbm2ddl schema update
  80. [main] org.hibernate.tool.hbm2ddl.SchemaUpdate : HHH000102: Fetching database metadata
  81. [main] org.hibernate.tool.hbm2ddl.SchemaUpdate : HHH000396: Updating schema
  82. [main] java.sql.DatabaseMetaData : HHH000262: Table not found: users
  83. [main] java.sql.DatabaseMetaData : HHH000262: Table not found: users
  84. [main] java.sql.DatabaseMetaData : HHH000262: Table not found: users
  85. [main] org.hibernate.tool.hbm2ddl.SchemaUpdate : HHH000232: Schema update complete`enter code here`
Add Comment
Please, Sign In to add comment