Chinaunix首页 | 论坛 | 博客
  • 博客访问: 518528
  • 博文数量: 135
  • 博客积分: 3568
  • 博客等级: 中校
  • 技术积分: 1942
  • 用 户 组: 普通用户
  • 注册时间: 2006-10-19 17:52
文章分类

全部博文(135)

文章存档

2012年(29)

2011年(41)

2010年(26)

2009年(12)

2008年(9)

2007年(12)

2006年(6)

分类: Java

2011-08-19 14:51:08

  1. package me.test;

  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.text.DecimalFormat;

  8. /**
  9.  * MySQL : Too many connections
  10.  *
  11.  * 该测试例子是用来检测数据库可以允许的最大连接数。
  12.  *
  13.  Windows XP, 物理内存2G,剩余空闲内存1G,my.ini中允许最大10000个连接
  14.  * 测试结果:
  15.  * 内存起始使用量:990M
  16.  * 测试结束时的内存使用量:1760M,
  17.  * 共使用770M内存,共创建1805个连接,平均每个连接使用了0.4M内存。
  18.  *
  19.  * 没有报Too many connections错误,而是报以下错误:
  20.  * ERROR[01806] null, message from server: "Can't create a new thread (errno 12); if you are not out of available memory,
  21.  * you can consult the manual for a possible OS-dependent bug"
  22.  *
  23.  * MySQL:
  24.  * MySQL允许的最大连接数是在my.ini中max_connections设置的。
  25.  * 理论上可以达到 max_connections + 1 个连接,多出的一个是用以在数据库管理员预留的,
  26.  * 防止由于达到最大连接数之后,管理员无法再操作数据库。
  27.  *
  28.  * 但实际上能达到的最大连接数是由所在平台(OS)上线程库的质量、内存大小、每个连接所
  29.  * 使用的数据库内存大小、每个连接上运行的工作量以及期待的响应时间综合决定的。Linux
  30.  * 和Solaris一般都可以支持500~1000个并发连接。如果拥有上G内存且每个连接对响应时间
  31.  * 要求不高时,甚至能支持10000个连接。Windows平台则由于受到 Posix兼容层的限制,最大
  32.  * 连接数为(open tables × 2 + open connections) < 2048。
  33.  *
  34.  * 参考:
  35.  * http://dev.mysql.com/doc/refman/5.5/en/too-many-connections.html
  36.  *
  37.  * @author btpka3@163.com
  38.  * @date 2011/07/29
  39.  */
  40. public class Main extends Thread {

  41.     public static void main(String[] args) {

  42.         Main m;
  43.         for (int i = 1; i <= 10000; i++) {
  44.             m = new Main(i);
  45.             m.start();
  46.             while (m.getSuccess() == null) {
  47.                 try {
  48.                     Thread.sleep(100);
  49.                 } catch (InterruptedException e) {
  50.                     e.printStackTrace();
  51.                 }
  52.             }
  53.             if (m.getSuccess().equals("0")) {
  54.                 System.out.println("Test Over~~~");
  55.                 break;
  56.             }
  57.         }
  58.     }

  59.     private int no = 0;
  60.     private String success = null;
  61.     public Main(int no) {
  62.         this.no = no;
  63.     }

  64.     @Override
  65.     public void run() {
  66.         Connection conn = null;
  67.         String url = "jdbc:mysql://localhost:3306/";
  68.         String dbName = "test";
  69.         String driver = "com.mysql.jdbc.Driver";
  70.         String userName = "root";
  71.         String password = "123456";
  72.         DecimalFormat df = new DecimalFormat("00000");

  73.         try {
  74.             Class.forName(driver).newInstance();
  75.             conn = DriverManager.getConnection(url + dbName, userName, password);
  76.             conn.setAutoCommit(false);

  77.             String sql = "select ? + ?";
  78.             PreparedStatement stmt = conn.prepareStatement(sql);
  79.             stmt.setInt(1, no);
  80.             stmt.setInt(2, no);

  81.             ResultSet rs = stmt.executeQuery();
  82.             int sum = 0;
  83.             while (rs.next()) {
  84.                 sum = rs.getInt(1);
  85.             }
  86.             rs.close();
  87.             stmt.close();
  88.             //conn.commit();
  89.             success = "1";
  90.             System.out.println("conn[" + df.format(no) + "] : "
  91.                     + df.format(sum));

  92.             while (true) {
  93.                 Thread.sleep(10000);
  94.             }
  95.         } catch (Exception e) {
  96.             success = "0";
  97.             System.err.println("ERROR[" + df.format(no) + "] " + e.getMessage());
  98.             try {
  99.                 if (conn != null) {
  100.                     conn.rollback();
  101.                 }
  102.             } catch (SQLException e1) {
  103.                 e1.printStackTrace();
  104.             }
  105.         } finally {
  106.             try {
  107.                 if (conn != null) {
  108.                     conn.commit();
  109.                     conn.close();
  110.                 }
  111.             } catch (SQLException e) {
  112.                 e.printStackTrace();
  113.             }
  114.         }
  115.     }
  116.     public String getSuccess() {
  117.         return success;
  118.     }
  119. }
阅读(1568) | 评论(0) | 转发(0) |
0

上一篇:Weblogic 笔记

下一篇:笔记 - 云产品

给主人留下些什么吧!~~