Java代码nutz很好的实现了表切换 <="" body="">
根据它的思路写了个数据库切换,测试可行!
Java代码
  1. public class MutiDataSource implements DataSource {
  2. private static ConcurrentHashMap dataSourceMap = new ConcurrentHashMap();
  3. protected HashMap dsdbMap;
  4. protected String driverClassName;
  5. protected String url;
  6. protected String username;
  7. protected String password;
  8. protected int maxActive;
  9. public MutiDataSource() {
  10. }
  11. public void init() throws SQLException {
  12. Iterator it = dsdbMap.keySet().iterator();
  13. String dsname, dbname;
  14. while (it.hasNext()) {
  15. dsname = it.next();
  16. dbname = dsdbMap.get(dsname);
  17. DruidDataSource ds = createDataSource(dbname);
  18. dataSourceMap.put(dsname, ds);
  19. }
  20. }
  21. public void close() {
  22. Iterator it = dataSourceMap.values().iterator();
  23. while (it.hasNext()) {
  24. it.next().close();
  25. }
  26. }
  27. protected DruidDataSource createDataSource(String dbname) {
  28. DruidDataSource ds = new DruidDataSource();
  29. ds.setDriverClassName(driverClassName);
  30. ds.setUsername(username);
  31. ds.setPassword(password);
  32. ds.setUrl(url + dbname);
  33. return ds;
  34. }
  35. public DataSource getDs() {
  36. return dataSourceMap.get(DataSourceNames.get());
  37. }
  38. public void setDriverClassName(String driverClassName) {
  39. this.driverClassName = driverClassName;
  40. }
  41. public void setUrl(String url) {
  42. this.url = url;
  43. }
  44. public void setUsername(String username) {
  45. this.username = username;
  46. }
  47. public void setPassword(String password) {
  48. this.password = password;
  49. }
  50. public void setMaxActive(int maxActive) {
  51. this.maxActive = maxActive;
  52. }
  53. @Override
  54. public PrintWriter getLogWriter() throws SQLException {
  55. return getDs().getLogWriter();
  56. }
  57. @Override
  58. public int getLoginTimeout() throws SQLException {
  59. return getDs().getLoginTimeout();
  60. }
  61. @Override
  62. public void setLogWriter(PrintWriter out) throws SQLException {
  63. getDs().setLogWriter(out);
  64. }
  65. @Override
  66. public void setLoginTimeout(int seconds) throws SQLException {
  67. getDs().setLoginTimeout(seconds);
  68. }
  69. @Override
  70. public boolean isWrapperFor(Class iface) throws SQLException {
  71. return getDs().isWrapperFor(iface);
  72. }
  73. @Override
  74. public T unwrap(Class iface) throws SQLException {
  75. return getDs().unwrap(iface);
  76. }
  77. @Override
  78. public Connection getConnection() throws SQLException {
  79. return getDs().getConnection();
  80. }
  81. @Override
  82. public Connection getConnection(String username, String password)
  83. throws SQLException {
  84. return getDs().getConnection(username, password);
  85. }
  86. }

Java代码
  1. public class DataSourceNames {
  2. private static final ThreadLocal ds = new ThreadLocal();
  3. public static String get() {
  4. return ds.get();
  5. }
  6. public static String set(String obj) {
  7. String re = get();
  8. ds.set(obj);
  9. return re;
  10. }
  11. public static void clear() {
  12. set(null);
  13. }
  14. }