一、前言
用Java开发企业应用软件, 经常会采用Spring+MyBatis+MySQL搭建数据库框架。如果数据量很大,一个MYSQL库存储数据访问效率很低,往往会采用分库存储管理的方式。本文讲述如何通过Spring+Mybatis构建多数据库访问的架构,并采用多线程提升数据库的访问效率。
二、整体方案
三、开发环境准备
3.1 下载Spring、Mybatis、Mysql组件。
3.2 Eclipse:Java开发IDE。引入如下jar包:
代码结构如下:
四、构建数据库集群
在MYSQL中创建11个数据库(test1/2/3/4/5/6/7/8/9/10/11)创建一个简单的表:
在test1的tbl_Demo表中插入5千万条数据,其它10个数据库的tbl_Demo表中分别插入5百万条数据(用函数)。
在test1的tbl_Demo表中插入5千万条数据,其它10个数据库的tbl_Demo表中分别插入5百万条数据(用函数)。
五、创建Mybatis数据库映射接口
/**
* Mybatis 映射接口
*
*
* @author elon
* @version 1.0, 2015年10月23日
*/
public interface IDemo
{
public void insertDemo(DemoDAO demo);
public List selectGroup();
}
/**
*
* Mybatis 映射服务接口
*
* @author elon
* @version 1.0, 2015年10月23日
*/
public interface IDemoService
{
public void insertDemo(DemoDAO demo);
public List selectGroup();
}
/**
*
* Mybatis 映射服务实现
*
* @author elon
* @version 1.0, 2015年10月23日
*/
public class DemoServiceImpl implements IDemoService
{
private IDemo idemo = null;
public void setIdemo(IDemo idemo) {
this.idemo = idemo;
}
@Override
public void insertDemo(DemoDAO demo)
{
idemo.insertDemo(demo);
}
@Override
public List selectGroup()
{
return idemo.selectGroup();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
六、创建数据库标识管理和动态数据源
/**
*
* 保存数据库标识。每个线程由独立的对象存储
*
* @author elon
* @version 1.0, 2015年10月23日
*/
public class DBIndetifier
{
private static ThreadLocal dbKey = new ThreadLocal();
public static void setDBKey(final String dbKeyPara)
{
dbKey.set(dbKeyPara);
}
public static String getDBKey()
{
return dbKey.get();
}
}
/**
*
* 动态数据源。可根据不同的数据索引连接不同的数据库
*
* @author elon
* @version 1.0, 2015年10月23日
*/
public class DynamicDataSource extends AbstractRoutingDataSource
{
@Override
public Object determineCurrentLookupKey()
{
return DBIndetifier.getDBKey();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
七、创建数据库访问对象
/**
*
* 数据库访问对象。用于插入数据。
*
* @author elon
* @version 1.0, 2015年10月23日
*/
public class DemoDAO
{
private int a;
private String b;
private int c;
public int getA()
{
return a;
}
public void setA(int a)
{
this.a = a;
}
public String getB()
{
return b;
}
public void setB(String b)
{
this.b = b;
}
public int getC()
{
return c;
}
public void setC(int c)
{
this.c = c;
}
}
/**
*
* 映射结果定义
*
* @author elon
* @version 1.0, 2015年10月23日
*/
public class DemoResult implements Serializable
{
/**
* Comment for serialVersionUID
*
*/
private static final long serialVersionUID = -413001138792531448L;
private long sum;
public long getSum()
{
return sum;
}
public void setSum(long sum)
{
this.sum = sum;
}
@Override
public String toString()
{
return String.valueOf(sum);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
八、创建数据库访问任务
/**
* 数据库访问任务定义。将每一个对数据库访问的请求包装为一个任务对象,放到任务管理中,
* 然后等待任务执行完成,取出执行结果。
*
* @author elon
* @version 1.0, 2015年10月23日
*/
public class DBTask implements Runnable
{
private final String dbKey;
private final Object dbAccessObject;
private final String methodName;
private final Object[] paraArray;
@SuppressWarnings("rawtypes")
private final Class[] paraClassArray;
private Object operateResult;
private Exception exception;
private boolean finish;
/**
* 构造函数
* @param dbKey 数据库标识
* @param dbAccessObject 数据库访问对象
* @param methodName 数据库访问方法名称
* @param paraArray 参数列表
*/
public DBTask(final String dbKey, final Object dbAccessObject, final String methodName,
final Object... paraArray)
{
this.dbKey = dbKey;
this.dbAccessObject = dbAccessObject;
this.methodName = methodName;
this.paraArray = paraArray;
finish = false;
exception = null;
paraClassArray = new Class[paraArray.length];
for (int index = 0; index < paraArray.length; ++index)
{
paraClassArray[index] = paraArray[index].getClass();
}
operateResult = null;
}
/**
*
* 任务执行函数
*
*/
@Override
public void run()
{
try
{
DBIndetifier.setDBKey(dbKey);
Method method = dbAccessObject.getClass().getMethod(methodName, paraClassArray);
operateResult = method.invoke(dbAccessObject, paraArray);
}
catch (Exception e)
{
exception = e;
e.printStackTrace();
}
finish = true;
}
/**
*
* 返回操作结果。查询操作返回查询结果; 插入、删除、修改操作返回null
*
* @return 操作结果
*/
public Object getRetValue()
{
return operateResult;
}
/**
* 抛出数据库操作异常
*
* @return 异常
*/
public Exception getException()
{
return exception;
}
/**
*
* 返回任务是否已执行
*
* @return 标记
*/
public boolean isFinish()
{
return finish;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
九、创建数据库任务管理器
/**
* 数据库访问任务管理。将数据库访问任务放到线程池中执行。
*
*
* @author elon
* @version 1.0, 2015年10月23日
*/
public class DBTaskMgr
{
private static class DBTaskMgrInstance
{
public static final DBTaskMgr instance = new DBTaskMgr();
}
public static DBTaskMgr instance()
{
return DBTaskMgrInstance.instance;
}
private ThreadPoolExecutor pool;
public DBTaskMgr()
{
pool = new ThreadPoolExecutor(10, 50, 60, TimeUnit.SECONDS,
new ArrayBlockingQueue(10000),
new ThreadPoolExecutor.CallerRunsPolicy());
}
public void excute(Runnable task)
{
pool.execute(task);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
十、创建MyBatis配置文件
10.1 mybatis.xml
<configuration>
<mappers>
<mapper resource="cfg/demoMapper.xml"/>
mappers>
configuration>
10.2 demoMapper.xml
<mapper namespace="com.elon.IDemo">
<insert id="insertDemo" parameterType="com.elon.DemoDAO">
insert into tbl_demo(a, b, c) values(#{a}, #{b}, #{c});
insert>
<resultMap id="demoResult" type="com.elon.DemoResult">
<id property="sum" column="sumColum"/>
resultMap>
<select id="selectGroup" resultMap="demoResult">
select sum(a) as sumColum from tbl_demo group by c;
select>
mapper>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
十一、创建Spring配置文件
11.1 spring.xml
version="1.0" encoding="UTF-8"?>
"http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
id="dataSource_1" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver">property>
<property name="url" value="jdbc:mysql://10.70.69.69:3306/test1">property>
<property name="username" value="user123">property>
<property name="password" value="user123">property>
<property name="maxActive" value="100">property>
<property name="maxIdle" value="30">property>
<property name="maxWait" value="500">property>
<property name="defaultAutoCommit" value="true">property>
id="dataSource_2" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver">property>
<property name="url" value="jdbc:mysql://10.70.69.69:3306/test2">property>
<property name="username" value="user123">property>
<property name="password" value="user123">property>
<property name="maxActive" value="100">property>
<property name="maxIdle" value="30">property>
<property name="maxWait" value="500">property>
<property name="defaultAutoCommit" value="true">property>
id="dataSource_3" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver">property>
<property name="url" value="jdbc:mysql://10.70.69.69:3306/test3">property>
<property name="username" value="user123">property>
<property name="password" value="user123">property>
<property name="maxActive" value="100">property>
<property name="maxIdle" value="30">property>
<property name="maxWait" value="500">property>
<property name="defaultAutoCommit" value="true">property>
id="dataSource_4" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver">property>
<property name="url" value="jdbc:mysql://10.70.69.69:3306/test4">property>
<property name="username" value="user123">property>
<property name="password" value="user123">property>
<property name="maxActive" value="100">property>
<property name="maxIdle" value="30">property>
<property name="maxWait" value="500">property>
<property name="defaultAutoCommit" value="true">property>
id="dataSource_5" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver">property>
<property name="url" value="jdbc:mysql://10.70.69.69:3306/test5">property>
<property name="username" value="user123">property>
<property name="password" value="user123">property>
<property name="maxActive" value="100">property>
<property name="maxIdle" value="30">property>
<property name="maxWait" value="500">property>
<property name="defaultAutoCommit" value="true">property>
id="dataSource_6" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver">property>
<property name="url" value="jdbc:mysql://10.70.69.69:3306/test6">property>
<property name="username" value="user123">property>
<property name="password" value="user123">property>
<property name="maxActive" value="100">property>
<property name="maxIdle" value="30">property>
<property name="maxWait" value="500">property>
<property name="defaultAutoCommit" value="true">property>
id="dataSource_7" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver">property>
<property name="url" value="jdbc:mysql://10.61.67.246:3306/test7">property>
<property name="username" value="user123">property>
<property name="password" value="user123">property>
<property name="maxActive" value="100">property>
<property name="maxIdle" value="30">property>
<property name="maxWait" value="500">property>
<property name="defaultAutoCommit" value="true">property>
id="dataSource_8" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver">property>
<property name="url" value="jdbc:mysql://10.61.67.246:3306/test8">property>
<property name="username" value="user123">property>
<property name="password" value="user123">property>
<property name="maxActive" value="100">property>
<property name="maxIdle" value="30">property>
<property name="maxWait" value="500">property>
<property name="defaultAutoCommit" value="true">property>
id="dataSource_9" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver">property>
<property name="url" value="jdbc:mysql://10.61.67.246:3306/test9">property>
<property name="username" value="user123">property>
<property name="password" value="user123">property>
<property name="maxActive" value="100">property>
<property name="maxIdle" value="30">property>
<property name="maxWait" value="500">property>
<property name="defaultAutoCommit" value="true">property>
id="dataSource_10" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver">property>
<property name="url" value="jdbc:mysql://10.61.67.246:3306/test10">property>
<property name="username" value="user123">property>
<property name="password" value="user123">property>
<property name="maxActive" value="100">property>
<property name="maxIdle" value="30">property>
<property name="maxWait" value="500">property>
<property name="defaultAutoCommit" value="true">property>
id="dataSource_11" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver">property>
<property name="url" value="jdbc:mysql://10.61.67.246:3306/test11">property>
<property name="username" value="user123">property>
<property name="password" value="user123">property>
<property name="maxActive" value="100">property>
<property name="maxIdle" value="30">property>
<property name="maxWait" value="500">property>
<property name="defaultAutoCommit" value="true">property>
id="dataSource" class="com.elon.DynamicDataSource">
<property name="targetDataSources">
property>
id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:cfg/mybatis.xml">property>
<property name="dataSource" ref="dataSource" />
id="iDemo" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.elon.IDemo">property>
<property name="sqlSessionFactory" ref="sqlSessionFactory">property>
id="iDemoService" class="com.elon.DemoServiceImpl">
<property name="idemo" ref="iDemo">property>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
十二、测试代码
public class TestMain
{
/**
* 测试代码
*
*
* @param args
*/
public static void main(String[] args)
{
@SuppressWarnings("resource")
ApplicationContext context = new ClassPathXmlApplicationContext("cfg/spring.xml");
IDemoService service1 = (IDemoService)context.getBean("iDemoService");
DBTask task1 = new DBTask("test1", service1, "selectGroup");
DBTask task2 = new DBTask("test2", service1, "selectGroup");
DBTask task3 = new DBTask("test3", service1, "selectGroup");
DBTask task4 = new DBTask("test4", service1, "selectGroup");
DBTask task5 = new DBTask("test5", service1, "selectGroup");
DBTask task6 = new DBTask("test6", service1, "selectGroup");
DBTask task7 = new DBTask("test7", service1, "selectGroup");
DBTask task8 = new DBTask("test8", service1, "selectGroup");
DBTask task9 = new DBTask("test9", service1, "selectGroup");
DBTask task10 = new DBTask("test10", service1, "selectGroup");
DBTask task11 = new DBTask("test11", service1, "selectGroup");
DemoDAO demo = new DemoDAO();
demo.setA(10000000);
demo.setB("12121212");
demo.setC(100);
DBTask taskInsert = new DBTask("test2", service1, "insertDemo", demo);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("开始插入数据:" + format.format(new Date()));
DBTaskMgr.instance().excute(taskInsert);
while (true)
{
if (!taskInsert.isFinish())
{
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
else
{
break;
}
}
System.out.println("插入数据结束:" + format.format(new Date()));
System.out.println("开始查询5千万数据表:" + format.format(new Date()));
DBTaskMgr.instance().excute(task1);
while (true)
{
if (!task1.isFinish())
{
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
else
{
break;
}
}
System.out.println(task1.getRetValue());
System.out.println("查询5千万数据表结束:" + format.format(new Date()));
List taskList = new ArrayList();
taskList.add(task2);
taskList.add(task3);
taskList.add(task4);
taskList.add(task5);
taskList.add(task6);
taskList.add(task7);
taskList.add(task8);
taskList.add(task9);
taskList.add(task10);
taskList.add(task11);
System.out.println("开始查询10个5百万数据表:" + format.format(new Date()));
for (DBTask task : taskList)
{
DBTaskMgr.instance().excute(task);
}
while (true)
{
int success = 0;
for (DBTask task : taskList)
{
if (!task.isFinish())
{
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
else
{
++success;
}
}
if (success == 10)
{
break;
}
}
for (DBTask task : taskList)
{
System.out.println(task.getRetValue());;
}
System.out.println("10个5百万数据表查询结束:" +format.format(new Date()));
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
十三、测试结果
直接查询一个5千万条数据的数据库用时:45s。
多线程同步查询10个5百万数据的数据库用时: 22s。
由于10个数据库放在两台服务器上,一个服务器5个数据库。如果将10个数据分别部署到10个服务器,效率将更高。