ITPub博客

首页 > 应用开发 > Java > 同步写两个数据库--多线程

同步写两个数据库--多线程

原创 Java 作者:壹頁書 时间:2016-06-12 23:15:45 0 删除 编辑
接前文
http://blog.itpub.net/29254281/viewspace-2119080/

两个数据库假设在不同的服务器,不同的实例
  1. drop database db1;  
  2. drop database db2;  
  3. create database db1;  
  4. use db1;  
  5. create table t(translog int primary key,ts timestamp default current_timestamp);  
  6.   
  7. create database db2;  
  8. use db2;  
  9. create table t(translog int primary key,ts timestamp default current_timestamp);

如果像前文那样同步写两个库,
那么第一个库的连接和锁持有时间,都会翻倍.
可以尝试多线程处理

  1. import java.sql.Connection;  
  2. import java.sql.DriverManager;  
  3. import java.sql.PreparedStatement;  
  4. import java.sql.SQLException;  
  5. import java.util.concurrent.CountDownLatch;  
  6. import java.util.concurrent.ExecutorService;  
  7. import java.util.concurrent.Executors;  
  8.   
  9. public class Test {  
  10.   
  11.     private static ExecutorService threadpool = Executors.newFixedThreadPool(10);  
  12.   
  13.     public void shutdown() {  
  14.         threadpool.shutdown();  
  15.     }  
  16.   
  17.     private synchronized static Connection getConnection(String dbName) {  
  18.         String URL = "jdbc:mysql://127.0.0.1:3306/" + dbName;  
  19.         String USERNAME = "xx";  
  20.         String PWD = "xx";  
  21.         Connection con = null;  
  22.         try {  
  23.             Class.forName("com.mysql.jdbc.Driver");  
  24.             con = DriverManager.getConnection(URL, USERNAME, PWD);  
  25.             con.setAutoCommit(false);  
  26.         } catch (SQLException e) {  
  27.         } catch (ClassNotFoundException e) {  
  28.         }  
  29.         return con;  
  30.     }  
  31.   
  32.     public void insertTranslogid(int translogid) {  
  33.         Connection db1 = getConnection("db1");  
  34.         Connection db2 = getConnection("db2");  
  35.         CountDownLatch latch = new CountDownLatch(2);  
  36.   
  37.         Worker worker1 = new Worker(db1, translogid, latch);  
  38.         Worker worker2 = new Worker(db2, translogid, latch);  
  39.         threadpool.submit(worker1);  
  40.         threadpool.submit(worker2);  
  41.   
  42.         try {  
  43.             latch.await();  
  44.         } catch (InterruptedException e1) {  
  45.             e1.printStackTrace();  
  46.         }  
  47.   
  48.         try {  
  49.             if (worker1.isSuccessed() && worker2.isSuccessed()) {  
  50.                 db1.commit();  
  51.                 db2.commit();  
  52.             } else {  
  53.                 db1.rollback();  
  54.                 db2.rollback();  
  55.             }  
  56.             db1.close();  
  57.             db2.close();  
  58.         } catch (SQLException e) {  
  59.             e.printStackTrace();  
  60.         }  
  61.     }  
  62.   
  63.     public static void main(String[] args) throws ClassNotFoundException, SQLException {  
  64.         long start=System.currentTimeMillis();  
  65.         Test t = new Test();  
  66.         for (int i = 0; i <= 500; i++) {  
  67.             t.insertTranslogid(i);  
  68.         }  
  69.         t.shutdown();  
  70.         long end=System.currentTimeMillis();  
  71.         System.out.println((end-start)/1000);  
  72.     }  
  73. }  
  74.   
  75. class Worker implements Runnable {  
  76.     public Worker(Connection con, int translogid, CountDownLatch latch) {  
  77.         this.con = con;  
  78.         this.translogid = translogid;  
  79.         this.latch = latch;  
  80.     }  
  81.   
  82.     private CountDownLatch latch;  
  83.     private Connection con;  
  84.     private int translogid;  
  85.     private boolean successed=false;  
  86.   
  87.     public boolean isSuccessed() {  
  88.         return successed;  
  89.     }  
  90.   
  91.     @Override  
  92.     public void run() {  
  93.         PreparedStatement pt;  
  94.         try {  
  95.             pt = con.prepareStatement("insert into t(translog) values(?)");  
  96.             pt.setInt(1, translogid);  
  97.             pt.execute();  
  98.             successed = true;  
  99.         } catch (SQLException e) {  
  100.             e.printStackTrace();  
  101.             successed = false;  
  102.         } finally {  
  103.             latch.countDown();  
  104.         }  
  105.     }  
  106.   
  107. }  

501个Translog,用时29s
前文的处理时间为32s...似乎提高并不多...

这程序其实放生产,自己也没有什么把握.
有时候感觉压力很大.新单位已经2年了.
数据库还是一知半解.JAVA程序也忘的差不多了.其实原来JAVA也是一知半解.
希望2016年自己还能有一些突破吧.

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29254281/viewspace-2119274/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2013-10-19

  • 博文量
    620
  • 访问量
    6339948