proxool 多数据源动态切换

  1. package aa; 
  2.  
  3. import java.io.File; 
  4. import java.sql.Connection; 
  5. import java.sql.DriverManager; 
  6. import java.sql.SQLException; 
  7. import java.sql.Statement; 
  8. import java.util.Properties; 
  9.  
  10. import org.apache.log4j.PropertyConfigurator; 
  11. import org.logicalcobwebs.proxool.ProxoolException; 
  12. import org.logicalcobwebs.proxool.ProxoolFacade; 
  13. import org.logicalcobwebs.proxool.configuration.JAXPConfigurator; 
  14.  
  15. import bsh.This; 
  16.  
  17.  
  18. public class Test { 
  19.  
  20.     /**
  21.      * <xmp>
  22.      *
  23.      * </xmp>
  24.      *
  25.      * @param args
  26.      * @author 金鑫 2010-9-27 下午04:45:29
  27.      * @throws SQLException
  28.      * @throws ProxoolException
  29.      * @throws InterruptedException
  30.      */ 
  31.     public static void main(String[] args) throws SQLException, ProxoolException, InterruptedException { 
  32.         // TODO Auto-generated method stub 
  33.         readXmlFile("aa/aa.xml"); 
  34.         readXmlFile("aa/bb.xml"); 
  35.         Connection conn1 = getConnection("aa"); 
  36.         Statement sta1 = conn1.createStatement(); 
  37.         System.out.println(conn1); 
  38.         sta1.executeQuery("select * from wcmuser"); 
  39.         Connection conn2 = getConnection("bb"); 
  40.         System.out.println(conn2); 
  41.         Statement sta2 = conn2.createStatement(); 
  42.         sta2.executeQuery("select * from idsuser"); 
  43.          
  44.         int k =0
  45.         while(k<2){ 
  46.             Thread.sleep(10000); 
  47.             k++; 
  48.         } 
  49.          
  50.         conn1.close(); 
  51.         conn2.close(); 
  52.          
  53.         while(k<4){ 
  54.             Thread.sleep(10000); 
  55.             k++; 
  56.         } 
  57.         ProxoolFacade.removeConnectionPool("aa");//动态删除 
  58.     } 
  59.  
  60.     public static Connection getConnection(String sessionID) { 
  61.         try
  62.             Class.forName("org.logicalcobwebs.proxool.ProxoolDriver"); 
  63.             return DriverManager.getConnection("proxool."+sessionID); 
  64.         } catch (Exception ex) { 
  65.             ex.printStackTrace(); 
  66.         } 
  67.         return null
  68.     } 
  69.  
  70.     public static void readXmlFile(String xmlurl) { 
  71.         String appDir = Thread.currentThread().getContextClassLoader() 
  72.                 .getResource("").toString(); 
  73.         appDir = appDir.substring(6, appDir.length()); 
  74.         Properties properties = new Properties(); 
  75.         File file; 
  76.         try
  77.             file = new File(xmlurl); 
  78.             if (file.isAbsolute()) 
  79.                 JAXPConfigurator.configure(xmlurl, false); 
  80.             else 
  81.                 JAXPConfigurator.configure(appDir + File.separator + xmlurl, 
  82.                         false); 
  83.         } catch (ProxoolException e) { 
  84.             e.printStackTrace(); 
  85.         } 
  86.  
  87.         if (properties.size() > 0) { 
  88.             PropertyConfigurator.configure(properties); 
  89.         } 
  90.          
  91.           
  92.     } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值