Mysql 基于 Amoeba 的 水平和垂直 分片


 环境:

Servers

 

Amoeba Server (Linux): 192.168.14.129

Mysql 1 Server  (Linux): 192.168.14.131

Mysql 2 Server  (Linux): 192.168.14.133

 

Clients

 

Mysql GUI Tools (Windows): 192.168.14.28

Java Programs  (Eclipse): 192.168.14.28

 

假设以上程序都已经安装好了。

 

1. Mysql数据库远程访问授权

 

mysql 1 server 和 mysql 2 server 的 test 数据库,允许 amoeba server 访问。用户名:test_user 密码:1234

 

Sql代码   收藏代码
  1. grant all on test.* to test_user@192.168.14.129 identified by '1234';  
 

2.  创建测试表

 

在 mysql 1 server 中:

 

t_user:

Sql代码   收藏代码
  1. mysql> create table test.t_user (  
  2.     -> user_id integer unsigned not null,  
  3.     -> user_name varchar(45),  
  4.     -> user_address varchar(100),  
  5.     -> primary key (user_id)  
  6.     -> )engine=innodb;  
  7. Query OK, 0 rows affected (0.01 sec)  

 

t_blog:

Sql代码   收藏代码
  1. mysql> create table test.t_blog (  
  2.     -> blog_id integer unsigned not null,  
  3.     -> blog_title varchar(45),  
  4.     -> blog_content text,  
  5.     -> user_id integer,  
  6.     -> primary key (blog_id)  
  7.     -> )engine=innodb;  
  8. Query OK, 0 rows affected (0.00 sec)  
 

t_message:

Sql代码   收藏代码
  1. mysql> create table test.t_message (  
  2.     -> message_id integer unsigned not null,  
  3.     -> message_content varchar(255),  
  4.     -> user_id integer  
  5.     -> )engine=innodb;  
  6. Query OK, 0 rows affected (0.01 sec)  
 

在 mysql 2 server  中

 

t_user  同上。

 

t_attention:

Sql代码   收藏代码
  1. mysql> create table test.t_attention (  
  2.     -> attention_id integer unsigned not null,  
  3.     -> user_id integer,  
  4.     -> blog_id integer  
  5.     -> )engine=innodb;  
  6. Query OK, 0 rows affected (0.01 sec)  
 

t_blog_comment:

Sql代码   收藏代码
  1. mysql> create table test.t_blog_comment (  
  2.     -> comment_id integer unsigned not null,  
  3.     -> commnet_content text,  
  4.     -> blog_id integer  
  5.     -> )engine=innodb;  
  6. Query OK, 0 rows affected (0.01 sec)  
 

3. 配置 Amoeba 的切分数据库的规则

 

Amoeba 的详细使用说明请参见:http://docs.hexnova.com/amoeba/

 

dbServers.xml

 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="gbk"?>  
  2.   
  3. <!DOCTYPE amoeba:dbServers SYSTEM "dbserver.dtd">  
  4. <amoeba:dbServers xmlns:amoeba="http://amoeba.meidusa.com/">  
  5.   
  6.                 <!--  
  7.                         Each dbServer needs to be configured into a Pool,  
  8.                         If you need to configure multiple dbServer with load balancing that can be simplified by the following configuration:  
  9.                          add attribute with name virtual = "true" in dbServer, but the configuration does not allow the element with name factoryConfig  
  10.                          such as 'multiPool' dbServer  
  11.                 -->  
  12.   
  13.         <dbServer name="abstractServer" abstractive="true">  
  14.                 <factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory">  
  15.                         <property name="manager">${defaultManager}</property>  
  16.                         <property name="sendBufferSize">64</property>  
  17.                         <property name="receiveBufferSize">128</property>  
  18.   
  19.                         <!-- mysql port -->  
  20.                         <property name="port">3306</property>  
  21.   
  22.                         <!-- mysql schema -->  
  23.                         <property name="schema">test</property>  
  24.   
  25.                         <!-- mysql user -->  
  26.                         <property name="user">test_user</property>  
  27.   
  28.                         <!--  mysql password -->  
  29.                         <property name="password">1234</property>  
  30.   
  31.                 </factoryConfig>  
  32.   
  33.                 <poolConfig class="com.meidusa.amoeba.net.poolable.PoolableObjectPool">  
  34.                         <property name="maxActive">500</property>  
  35.                         <property name="maxIdle">500</property>  
  36.                         <property name="minIdle">10</property>  
  37.                         <property name="minEvictableIdleTimeMillis">600000</property>  
  38.                         <property name="timeBetweenEvictionRunsMillis">600000</property>  
  39.                         <property name="testOnBorrow">true</property>  
  40.                         <property name="testWhileIdle">true</property>  
  41.                 </poolConfig>  
  42.         </dbServer>  
  43.   
  44.         <dbServer name="server1"  parent="abstractServer">  
  45.                 <factoryConfig>  
  46.                         <!-- mysql ip -->  
  47.                         <property name="ipAddress">192.168.14.131</property>  
  48.                 </factoryConfig>  
  49.         </dbServer>  
  50.   
  51.         <dbServer name="server2"  parent="abstractServer">  
  52.                 <factoryConfig>  
  53.                         <!-- mysql ip -->  
  54.                         <property name="ipAddress">192.168.14.133</property>  
  55.                 </factoryConfig>  
  56.         </dbServer>  
  57.   
  58. </amoeba:dbServers>  

 

amoeba.xml

 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="gbk"?>  
  2.   
  3. <!DOCTYPE amoeba:configuration SYSTEM "amoeba.dtd">  
  4. <amoeba:configuration xmlns:amoeba="http://amoeba.meidusa.com/">  
  5.   
  6.         <proxy>  
  7.   
  8.                 <!-- service class must implements com.meidusa.amoeba.service.Service -->  
  9.                 <service name="Amoeba for Mysql" class="com.meidusa.amoeba.net.ServerableConnectionManager">  
  10.                         <!-- port -->  
  11.                         <property name="port">8066</property>  
  12.   
  13.                         <!-- bind ipAddress -->  
  14.                         <!-- 
  15.                         <property name="ipAddress">127.0.0.1</property> 
  16.                          -->  
  17.   
  18.                         <property name="manager">${clientConnectioneManager}</property>  
  19.   
  20.                         <property name="connectionFactory">  
  21.                                 <bean class="com.meidusa.amoeba.mysql.net.MysqlClientConnectionFactory">  
  22.                                         <property name="sendBufferSize">128</property>  
  23.                                         <property name="receiveBufferSize">64</property>  
  24.                                 </bean>  
  25.                         </property>  
  26.   
  27.                         <property name="authenticator">  
  28.                                 <bean class="com.meidusa.amoeba.mysql.server.MysqlClientAuthenticator">  
  29.   
  30.                                         <property name="user">root</property>  
  31.   
  32.                                         <property name="password">root</property>  
  33.   
  34.                                         <property name="filter">  
  35.                                                 <bean class="com.meidusa.amoeba.server.IPAccessController">  
  36.                                                         <property name="ipFile">${amoeba.home}/conf/access_list.conf</property>  
  37.                                                 </bean>  
  38.                                         </property>  
  39.                                 </bean>  
  40.                         </property>  
  41.   
  42.                 </service>  
  43.   
  44.                 <!-- server class must implements com.meidusa.amoeba.service.Service -->  
  45.                 <service name="Amoeba Monitor Server" class="com.meidusa.amoeba.monitor.MonitorServer">  
  46.                         <!-- port -->  
  47.                         <!--  default value: random number  
  48.                         <property name="port">9066</property>  
  49.                         -->  
  50.                         <!-- bind ipAddress -->  
  51.                         <property name="ipAddress">127.0.0.1</property>  
  52.                         <property name="daemon">true</property>  
  53.                         <property name="manager">${clientConnectioneManager}</property>  
  54.                         <property name="connectionFactory">  
  55.                                 <bean class="com.meidusa.amoeba.monitor.net.MonitorClientConnectionFactory"></bean>  
  56.                         </property>  
  57.   
  58.                 </service>  
  59.   
  60.                 <runtime class="com.meidusa.amoeba.mysql.context.MysqlRuntimeContext">  
  61.                         <!-- proxy server net IO Read thread size -->  
  62.                         <property name="readThreadPoolSize">20</property>  
  63.   
  64.                         <!-- proxy server client process thread size -->  
  65.                         <property name="clientSideThreadPoolSize">30</property>  
  66.   
  67.                         <!-- mysql server data packet process thread size -->  
  68.                         <property name="serverSideThreadPoolSize">30</property>  
  69.   
  70.                         <!-- per connection cache prepared statement size  -->  
  71.                         <property name="statementCacheSize">500</property>  
  72.   
  73.                         <!-- query timeout( default: 60 second , TimeUnit:second) -->  
  74.                         <property name="queryTimeout">60</property>  
  75.                 </runtime>  
  76.   
  77.         </proxy>  
  78.         <!--  
  79.                 Each ConnectionManager will start as thread  
  80.                 manager responsible for the Connection IO read , Death Detection  
  81.         -->  
  82.         <connectionManagerList>  
  83.                 <connectionManager name="clientConnectioneManager" class="com.meidusa.amoeba.net.MultiConnectionManagerWrapper">  
  84.                         <property name="subManagerClassName">com.meidusa.amoeba.net.ConnectionManager</property>  
  85.                         <!--  
  86.                           default value is avaliable Processors  
  87.                         <property name="processors">5</property>  
  88.                          -->  
  89.                 </connectionManager>  
  90.                 <connectionManager name="defaultManager" class="com.meidusa.amoeba.net.MultiConnectionManagerWrapper">  
  91.                         <property name="subManagerClassName">com.meidusa.amoeba.net.AuthingableConnectionManager</property>  
  92.   
  93.                         <!--  
  94.                           default value is avaliable Processors  
  95.                         <property name="processors">5</property>  
  96.                          -->  
  97.                 </connectionManager>  
  98.         </connectionManagerList>  
  99.   
  100.                 <!-- default using file loader -->  
  101.         <dbServerLoader class="com.meidusa.amoeba.context.DBServerConfigFileLoader">  
  102.                 <property name="configFile">${amoeba.home}/conf/dbServers.xml</property>  
  103.         </dbServerLoader>  
  104.   
  105.         <queryRouter class="com.meidusa.amoeba.mysql.parser.MysqlQueryRouter">  
  106.                 <property name="ruleLoader">  
  107.                         <bean class="com.meidusa.amoeba.route.TableRuleFileLoader">  
  108.                                 <property name="ruleFile">${amoeba.home}/conf/rule.xml</property>  
  109.                                 <property name="functionFile">${amoeba.home}/conf/ruleFunctionMap.xml</property>  
  110.                         </bean>  
  111.                 </property>  
  112.                 <property name="sqlFunctionFile">${amoeba.home}/conf/functionMap.xml</property>  
  113.                 <property name="LRUMapSize">1500</property>  
  114.                 <property name="defaultPool">server1</property><!-- 默认数据库,即主数据库 -->  
  115.   
  116.                 <!--  
  117.                 <property name="writePool">server1</property>  
  118.                 <property name="readPool">server1</property>  
  119.                 -->  
  120.                 <property name="needParse">true</property>  
  121.         </queryRouter>  
  122. </amoeba:configuration>  
 

rule.xml

 

t_user 表 根据 user_id 字段的奇偶性 水平切分 , 偶数分到 server1 , 奇数分到 server2。其中 server1,server2 是在 dbServers.xml 中定义的。

 

t_attention 表垂直切分到 server2

 

主数据库是 server1 , 在 amoeba.xml 中定义

Xml代码   收藏代码
  1. <property name="defaultPool">server1</property>  

 

主数据库说明:连接到 Amoeba 代理的时候,主数据库的所有表均可访问。 但是其他的数据库的表,需要在分片规则中有涉及的表,才能访问,其他表不能访问。

 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="gbk"?>  
  2. <!DOCTYPE amoeba:rule SYSTEM "rule.dtd">  
  3.   
  4. <amoeba:rule xmlns:amoeba="http://amoeba.meidusa.com/">  
  5.         <tableRule name="t_user" schema="test" defaultPools="server1,server2">  
  6.                 <rule name="rule1">  
  7.                         <parameters>user_id</parameters>  
  8.                         <expression><![CDATA[ user_id % 2 == 0 ]]></expression>  
  9.                         <defaultPools>server1</defaultPools>  
  10.                 </rule>  
  11.                 <rule name="rule2">  
  12.                         <parameters>user_id</parameters>  
  13.                         <expression><![CDATA[ user_id % 2 == 1 ]]></expression>  
  14.                         <defaultPools>server2</defaultPools>  
  15.                 </rule>  
  16.         </tableRule>  
  17.         <tableRule name="t_attention" schema="test" defaultPools="server2" />  
  18.         <tableRule name="t_blog_comment" schema="test" defaultPools="server2" />  
  19.  </amoeba:rule>  

 

启动 msyql1, mysql2, amoeba 切分完成。

 

4. 测试

 

访问的时候,需要通过 Amoeba 代理来访问。不能直接访问实体数据库,不然切分无效。

 

使用 Mysql GUI Tools 连接 Amoeba

 

用户名:root   密码: root  端口号:8066

 

以上信息在 amoeba.xml 中定义

 

Xml代码   收藏代码
  1. <service name="Amoeba for Mysql" class="com.meidusa.amoeba.net.ServerableConnectionManager">  
  2.                         <!-- port 连接端口号-->  
  3.                         <property name="port">8066</property>  
  4.   
  5.                         <!-- bind ipAddress -->  
  6.                         <!-- 
  7.                         <property name="ipAddress">127.0.0.1</property> 
  8.                          -->  
  9.   
  10.                         <property name="manager">${clientConnectioneManager}</property>  
  11.   
  12.                         <property name="connectionFactory">  
  13.                                 <bean class="com.meidusa.amoeba.mysql.net.MysqlClientConnectionFactory">  
  14.                                         <property name="sendBufferSize">128</property>  
  15.                                         <property name="receiveBufferSize">64</property>  
  16.                                 </bean>  
  17.                         </property>  
  18.   
  19.                         <property name="authenticator">  
  20.                                 <bean class="com.meidusa.amoeba.mysql.server.MysqlClientAuthenticator">  
  21.                                         <!--用户名,密码 -->  
  22.                                         <property name="user">root</property>  
  23.   
  24.                                         <property name="password">root</property>  
  25.   
  26.                                         <property name="filter">  
  27.                                                 <bean class="com.meidusa.amoeba.server.IPAccessController">  
  28.                                                         <property name="ipFile">${amoeba.home}/conf/access_list.conf</property>  
  29.                                                 </bean>  
  30.                                         </property>  
  31.                                 </bean>  
  32.                         </property>  
  33.   
  34.                 </service>  

 



 

 

可以发现,在GUI 工具中,能看到的表 只有在 server1 中,创建的表。 上面已经说明了,因为 server1 中主数据库。

 

验证垂直切分的 t_attention 表

 

为了验证,垂直切分出去的 t_attention 表。  直接执行 select * from test.t_attention;


 

可以看到,查询成功。 说明的确可以访问,切分出去的 t_attention 表。

 

验证水平切分的 t_user 表

 

t_user 的水平切分规则是 根据 user_id 的奇偶性进行切分。

 


上图的左下角可以看到:2 rows affected by the last command , 说明刚才那个插入语句,作用到了两条记录

 

查询看一下插入结果:


发现有两条一样的数据。到网上找了一下原因。

 

后来知道 amoeba 是根据 sql 解析来进行 水平切分的, 需要把切分的关键字段(这里是user_id),加入到 sql 中。否则 切分规则无效。无效后,会在 server1, server2 均都插入数据。

 

即变为:insert into t_user(user_id, user_name, user_address) values(1, 'n1', 'a1')

 

分别检查一下,两个数据库


 

两个数据库均插入了一条相同的数据。  同时也了解到,amoeba 的查询会将水平切分的表在两个数据库的结果合并。

 

修改 sql 语句后再执行一次。


可以看到左下角:只作用到一条记录

 

看下查询结果



 
 

 

因为 user_id 为 偶数, 所有分派到 server 1 了。

 

水平切分的排序与分页


排序结果:分别查询的 server1 和 server2 然后简单合并。 所以 排序不正确。


分页结果:同样是分别执行的 server1 和 server2 然后合并。 每个 server 取两条记录,记录也变为了 4 条。分页不正确

 

 

验证连接查询

 

在 t_message , t_blog, t_attention 表分别插入两个数据

分别执行以上查询语句。

 

发现只有 第二 和 第四 可以执行成功。

 

结果:

1. 如果 表1 被水平分片,连接查询时,需要连接的表,在 表1 分片的所有数据库中均存在(即相同的分片规则),才可以连接查询。

2. 如果 表1 被垂直分片,连接查询时,需要连接的表也同样分片到同一个数据库中(即相同的分片规则)。

3. 综上,只有在连接的表,在同一个数据库中均存在时,才能连接操作。即 不支持跨数据库的连接。

 

验证事务管理

 

使用程序验证。

 

1. 验证没有分片的表的事务操作(无回滚)。

 

程序片段:

Java代码   收藏代码
  1. package prx.dao;  
  2.   
  3. import java.util.List;  
  4. import java.util.Map;  
  5.   
  6. import prx.core.string.StringUtil;  
  7. import prx.dao.core.IPrxDao;  
  8. import prx.dao.core.impl.MySqlDao;  
  9.   
  10. public class Test {  
  11.   
  12.     public static void main(String[] args) throws Exception {  
  13.         IPrxDao dao = new MySqlDao();  
  14.           
  15.         //开启事务,插入几条数据  
  16.         dao.startTransaction();  
  17.         for(int i=3; i<10; i++) {  
  18.             dao.executeUpdate("insert into t_message(message_id, message_content, user_id) values(?, ?, ?)"new Object[]{i, "c" + i, i});  
  19.         }  
  20.         dao.commitTransaction();  
  21.           
  22.         //查看结果  
  23.         List<Map<String, Object>> list = dao.queryForMapList("select * from t_message"new Object[]{});  
  24.           
  25.         for(Map<String, Object> map : list) {  
  26.             System.out.println(StringUtil.valueOfMap(map));  
  27.         }  
  28.     }  
  29. }  

 

执行结果:

Cmd代码   收藏代码
  1. 2011/08/12 10:17:33 [INFO] MLog clients using log4j logging.  
  2. 2011/08/12 10:17:34 [INFO] Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]  
  3. 2011/08/12 10:17:34 [INFO] Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> 1hge3oc8h1g3olvycduja2|7a78d3, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1hge3oc8h1g3olvycduja2|7a78d3, idleConnectionTestPeriod -> 0, initialPoolSize -> 3, jdbcUrl -> jdbc:mysql://192.168.14.129:8066/test?autoReconnect=true&characterEncoding=UTF-8, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 15, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 3, numHelperThreads -> 3, numThreadsAwaitingCheckoutDefaultUser -> 0, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false ]  
  4. 2011/08/12 10:17:36 [INFO] [开始事务]  
  5. 2011/08/12 10:17:36 [INFO] insert into t_message(message_id, message_content, user_id) values(?, ?, ?)  
  6. 2011/08/12 10:17:36 [INFO] values:[3,c3,3]  
  7. 2011/08/12 10:17:36 [INFO] insert into t_message(message_id, message_content, user_id) values(?, ?, ?)  
  8. 2011/08/12 10:17:36 [INFO] values:[4,c4,4]  
  9. 2011/08/12 10:17:36 [INFO] insert into t_message(message_id, message_content, user_id) values(?, ?, ?)  
  10. 2011/08/12 10:17:36 [INFO] values:[5,c5,5]  
  11. 2011/08/12 10:17:36 [INFO] insert into t_message(message_id, message_content, user_id) values(?, ?, ?)  
  12. 2011/08/12 10:17:36 [INFO] values:[6,c6,6]  
  13. 2011/08/12 10:17:36 [INFO] insert into t_message(message_id, message_content, user_id) values(?, ?, ?)  
  14. 2011/08/12 10:17:36 [INFO] values:[7,c7,7]  
  15. 2011/08/12 10:17:36 [INFO] insert into t_message(message_id, message_content, user_id) values(?, ?, ?)  
  16. 2011/08/12 10:17:36 [INFO] values:[8,c8,8]  
  17. 2011/08/12 10:17:36 [INFO] insert into t_message(message_id, message_content, user_id) values(?, ?, ?)  
  18. 2011/08/12 10:17:36 [INFO] values:[9,c9,9]  
  19. 2011/08/12 10:17:36 [INFO] [事务提交]  
  20. 2011/08/12 10:17:36 [INFO] select * from t_message  
  21. [message_id:1, message_content:c1, user_id:1]  
  22. [message_id:2, message_content:c2, user_id:2]  
  23. [message_id:3, message_content:c3, user_id:3]  
  24. [message_id:4, message_content:c4, user_id:4]  
  25. [message_id:5, message_content:c5, user_id:5]  
  26. [message_id:6, message_content:c6, user_id:6]  
  27. [message_id:7, message_content:c7, user_id:7]  
  28. [message_id:8, message_content:c8, user_id:8]  
  29. [message_id:9, message_content:c9, user_id:9]  
 

执行正确无误。

 

2. 验证没有分片的表的操作(有回滚)。

程序片段:

 
Java代码   收藏代码
  1. public static void main(String[] args) throws Exception {  
  2.     IPrxDao dao = new MySqlDao();  
  3.       
  4.     //开启事务,插入几条数据  
  5.     dao.startTransaction();  
  6.     for(int i=10; i<20; i++) {  
  7.         dao.executeUpdate("insert into t_message(message_id, message_content, user_id) values(?, ?, ?)"new Object[]{i, "c" + i, i});  
  8.     }  
  9.     //主键冲突,出错,回滚。  
  10.     dao.executeUpdate("insert into t_user(user_id) values(?)"new Object[]{1});  
  11.     dao.commitTransaction();  
  12.       
  13.       
  14.     //查看结果  
  15.     List<Map<String, Object>> list = dao.queryForMapList("select * from t_message"new Object[]{});  
  16.       
  17.     for(Map<String, Object> map : list) {  
  18.         System.out.println(StringUtil.valueOfMap(map));  
  19.     }  
  20. }  

 执行结果:

Cmd代码   收藏代码
  1. 2011/08/12 10:24:37 [INFO] MLog clients using log4j logging.  
  2. 2011/08/12 10:24:37 [INFO] Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]  
  3. 2011/08/12 10:24:37 [INFO] Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> 1hge3oc8h1g3xpaud8zfzf|b169f8, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1hge3oc8h1g3xpaud8zfzf|b169f8, idleConnectionTestPeriod -> 0, initialPoolSize -> 3, jdbcUrl -> jdbc:mysql://192.168.14.129:8066/test?autoReconnect=true&characterEncoding=UTF-8, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 15, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 3, numHelperThreads -> 3, numThreadsAwaitingCheckoutDefaultUser -> 0, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false ]  
  4. 2011/08/12 10:24:38 [INFO] [开始事务]  
  5. 2011/08/12 10:24:38 [INFO] insert into t_message(message_id, message_content, user_id) values(?, ?, ?)  
  6. 2011/08/12 10:24:38 [INFO] values:[10,c10,10]  
  7. 2011/08/12 10:24:38 [INFO] insert into t_message(message_id, message_content, user_id) values(?, ?, ?)  
  8. 2011/08/12 10:24:38 [INFO] values:[11,c11,11]  
  9. 2011/08/12 10:24:38 [INFO] insert into t_message(message_id, message_content, user_id) values(?, ?, ?)  
  10. 2011/08/12 10:24:38 [INFO] values:[12,c12,12]  
  11. 2011/08/12 10:24:38 [INFO] insert into t_message(message_id, message_content, user_id) values(?, ?, ?)  
  12. 2011/08/12 10:24:38 [INFO] values:[13,c13,13]  
  13. 2011/08/12 10:24:38 [INFO] insert into t_message(message_id, message_content, user_id) values(?, ?, ?)  
  14. 2011/08/12 10:24:38 [INFO] values:[14,c14,14]  
  15. 2011/08/12 10:24:38 [INFO] insert into t_message(message_id, message_content, user_id) values(?, ?, ?)  
  16. 2011/08/12 10:24:38 [INFO] values:[15,c15,15]  
  17. 2011/08/12 10:24:38 [INFO] insert into t_message(message_id, message_content, user_id) values(?, ?, ?)  
  18. 2011/08/12 10:24:38 [INFO] values:[16,c16,16]  
  19. 2011/08/12 10:24:38 [INFO] insert into t_message(message_id, message_content, user_id) values(?, ?, ?)  
  20. 2011/08/12 10:24:38 [INFO] values:[17,c17,17]  
  21. 2011/08/12 10:24:38 [INFO] insert into t_message(message_id, message_content, user_id) values(?, ?, ?)  
  22. 2011/08/12 10:24:38 [INFO] values:[18,c18,18]  
  23. 2011/08/12 10:24:38 [INFO] insert into t_message(message_id, message_content, user_id) values(?, ?, ?)  
  24. 2011/08/12 10:24:38 [INFO] values:[19,c19,19]  
  25. 2011/08/12 10:24:38 [INFO] insert into t_user(user_id) values(?)  
  26. 2011/08/12 10:24:38 [INFO] values:[1]  
  27. 2011/08/12 10:24:38 [ERROR] Duplicate entry '1' for key 'PRIMARY'  
  28. com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Duplicate entry '1' for key 'PRIMARY'  
  29.     at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:931)  
  30.     at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2870)  
  31.     at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1573)  
  32.     at com.mysql.jdbc.ServerPreparedStatement.serverExecute(ServerPreparedStatement.java:1169)  
  33.     at com.mysql.jdbc.ServerPreparedStatement.executeInternal(ServerPreparedStatement.java:693)  
  34.     at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1404)  
  35.     at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1318)  
  36.     at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1303)  
  37.     at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:105)  
  38.     at prx.dao.core.AbstractPrxDao.executeUpdate(AbstractPrxDao.java:215)  
  39.     at prx.dao.Test.main(Test.java:21)  
  40. 2011/08/12 10:24:38 [ERROR] [事务失败,回滚]  
  41. 2011/08/12 10:24:38 [INFO] select * from t_message  
  42. [message_id:1, message_content:c1, user_id:1]  
  43. [message_id:2, message_content:c2, user_id:2]  
  44. [message_id:3, message_content:c3, user_id:3]  
  45. [message_id:4, message_content:c4, user_id:4]  
  46. [message_id:5, message_content:c5, user_id:5]  
  47. [message_id:6, message_content:c6, user_id:6]  
  48. [message_id:7, message_content:c7, user_id:7]  
  49. [message_id:8, message_content:c8, user_id:8]  
  50. [message_id:9, message_content:c9, user_id:9]  

 执行正确无误。

 

3. 验证垂直分片的表的事务(无回滚)

Java代码   收藏代码
  1. public static void main(String[] args) throws Exception {  
  2.     IPrxDao dao = new MySqlDao();  
  3.       
  4.     //开启事务,插入几条数据  
  5.     dao.startTransaction();  
  6.     for(int i=3; i<10; i++) {  
  7.         dao.executeUpdate("insert into t_attention(attention_id, user_id, blog_id) values(?, ?, ?)"new Object[]{i, i, i});  
  8.     }  
  9.     //主键冲突,出错,回滚。  
  10.     //dao.executeUpdate("insert into t_user(user_id) values(?)", new Object[]{1});  
  11.     dao.commitTransaction();  
  12.       
  13.       
  14.     //查看结果  
  15.     List<Map<String, Object>> list = dao.queryForMapList("select * from t_attention"new Object[]{});  
  16.       
  17.     for(Map<String, Object> map : list) {  
  18.         System.out.println(StringUtil.valueOfMap(map));  
  19.     }  
  20. }  

 执行结果:

Cmd代码   收藏代码
  1. 2011/08/12 10:30:49 [INFO] MLog clients using log4j logging.  
  2. 2011/08/12 10:30:50 [INFO] Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]  
  3. 2011/08/12 10:30:50 [INFO] Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> 1hge3oc8h1g45oipe8gwx3|b169f8, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1hge3oc8h1g45oipe8gwx3|b169f8, idleConnectionTestPeriod -> 0, initialPoolSize -> 3, jdbcUrl -> jdbc:mysql://192.168.14.129:8066/test?autoReconnect=true&characterEncoding=UTF-8, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 15, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 3, numHelperThreads -> 3, numThreadsAwaitingCheckoutDefaultUser -> 0, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false ]  
  4. 2011/08/12 10:30:50 [INFO] [开始事务]  
  5. 2011/08/12 10:30:50 [INFO] insert into t_attention(attention_id, user_id, blog_id) values(?, ?, ?)  
  6. 2011/08/12 10:30:50 [INFO] values:[3,3,3]  
  7. 2011/08/12 10:30:50 [INFO] insert into t_attention(attention_id, user_id, blog_id) values(?, ?, ?)  
  8. 2011/08/12 10:30:50 [INFO] values:[4,4,4]  
  9. 2011/08/12 10:30:50 [INFO] insert into t_attention(attention_id, user_id, blog_id) values(?, ?, ?)  
  10. 2011/08/12 10:30:50 [INFO] values:[5,5,5]  
  11. 2011/08/12 10:30:50 [INFO] insert into t_attention(attention_id, user_id, blog_id) values(?, ?, ?)  
  12. 2011/08/12 10:30:50 [INFO] values:[6,6,6]  
  13. 2011/08/12 10:30:50 [INFO] insert into t_attention(attention_id, user_id, blog_id) values(?, ?, ?)  
  14. 2011/08/12 10:30:50 [INFO] values:[7,7,7]  
  15. 2011/08/12 10:30:50 [INFO] insert into t_attention(attention_id, user_id, blog_id) values(?, ?, ?)  
  16. 2011/08/12 10:30:50 [INFO] values:[8,8,8]  
  17. 2011/08/12 10:30:50 [INFO] insert into t_attention(attention_id, user_id, blog_id) values(?, ?, ?)  
  18. 2011/08/12 10:30:50 [INFO] values:[9,9,9]  
  19. 2011/08/12 10:30:50 [INFO] [事务提交]  
  20. 2011/08/12 10:30:50 [INFO] select * from t_attention  
  21. [attention_id:1, user_id:1, blog_id:1]  
  22. [attention_id:2, user_id:2, blog_id:2]  
  23. [attention_id:2, user_id:2, blog_id:2]  
  24. [attention_id:3, user_id:3, blog_id:3]  
  25. [attention_id:4, user_id:4, blog_id:4]  
  26. [attention_id:5, user_id:5, blog_id:5]  
  27. [attention_id:6, user_id:6, blog_id:6]  
  28. [attention_id:7, user_id:7, blog_id:7]  
  29. [attention_id:8, user_id:8, blog_id:8]  
  30. [attention_id:9, user_id:9, blog_id:9]  

 执行成功。

 

4.  验证垂直分片的表(有回滚)

Java代码   收藏代码
  1. public static void main(String[] args) throws Exception {  
  2.     IPrxDao dao = new MySqlDao();  
  3.       
  4.     //开启事务,插入几条数据  
  5.     dao.startTransaction();  
  6.     for(int i=10; i<20; i++) {  
  7.         dao.executeUpdate("insert into t_attention(attention_id, user_id, blog_id) values(?, ?, ?)"new Object[]{i, i, i});  
  8.     }  
  9.     //主键冲突,出错,回滚。  
  10.     dao.executeUpdate("insert into t_user(user_id) values(?)"new Object[]{1});  
  11.     dao.commitTransaction();  
  12.       
  13.       
  14.     //查看结果  
  15.     List<Map<String, Object>> list = dao.queryForMapList("select * from t_attention"new Object[]{});  
  16.       
  17.     for(Map<String, Object> map : list) {  
  18.         System.out.println(StringUtil.valueOfMap(map));  
  19.     }  
  20. }  

 执行结果:

Cmd代码   收藏代码
  1. 2011/08/12 10:32:46 [INFO] MLog clients using log4j logging.  
  2. 2011/08/12 10:32:46 [INFO] Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]  
  3. 2011/08/12 10:32:46 [INFO] Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> 1hge3oc8h1g486lb1n4c9c4|b169f8, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1hge3oc8h1g486lb1n4c9c4|b169f8, idleConnectionTestPeriod -> 0, initialPoolSize -> 3, jdbcUrl -> jdbc:mysql://192.168.14.129:8066/test?autoReconnect=true&characterEncoding=UTF-8, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 15, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 3, numHelperThreads -> 3, numThreadsAwaitingCheckoutDefaultUser -> 0, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false ]  
  4. 2011/08/12 10:32:47 [INFO] [开始事务]  
  5. 2011/08/12 10:32:47 [INFO] insert into t_attention(attention_id, user_id, blog_id) values(?, ?, ?)  
  6. 2011/08/12 10:32:47 [INFO] values:[10,10,10]  
  7. 2011/08/12 10:32:47 [INFO] insert into t_attention(attention_id, user_id, blog_id) values(?, ?, ?)  
  8. 2011/08/12 10:32:47 [INFO] values:[11,11,11]  
  9. 2011/08/12 10:32:47 [INFO] insert into t_attention(attention_id, user_id, blog_id) values(?, ?, ?)  
  10. 2011/08/12 10:32:47 [INFO] values:[12,12,12]  
  11. 2011/08/12 10:32:47 [INFO] insert into t_attention(attention_id, user_id, blog_id) values(?, ?, ?)  
  12. 2011/08/12 10:32:47 [INFO] values:[13,13,13]  
  13. 2011/08/12 10:32:47 [INFO] insert into t_attention(attention_id, user_id, blog_id) values(?, ?, ?)  
  14. 2011/08/12 10:32:47 [INFO] values:[14,14,14]  
  15. 2011/08/12 10:32:47 [INFO] insert into t_attention(attention_id, user_id, blog_id) values(?, ?, ?)  
  16. 2011/08/12 10:32:47 [INFO] values:[15,15,15]  
  17. 2011/08/12 10:32:47 [INFO] insert into t_attention(attention_id, user_id, blog_id) values(?, ?, ?)  
  18. 2011/08/12 10:32:47 [INFO] values:[16,16,16]  
  19. 2011/08/12 10:32:47 [INFO] insert into t_attention(attention_id, user_id, blog_id) values(?, ?, ?)  
  20. 2011/08/12 10:32:47 [INFO] values:[17,17,17]  
  21. 2011/08/12 10:32:47 [INFO] insert into t_attention(attention_id, user_id, blog_id) values(?, ?, ?)  
  22. 2011/08/12 10:32:47 [INFO] values:[18,18,18]  
  23. 2011/08/12 10:32:47 [INFO] insert into t_attention(attention_id, user_id, blog_id) values(?, ?, ?)  
  24. 2011/08/12 10:32:47 [INFO] values:[19,19,19]  
  25. 2011/08/12 10:32:47 [INFO] insert into t_user(user_id) values(?)  
  26. 2011/08/12 10:32:47 [INFO] values:[1]  
  27. 2011/08/12 10:32:47 [ERROR] Duplicate entry '1' for key 'PRIMARY'  
  28. com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Duplicate entry '1' for key 'PRIMARY'  
  29.     at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:931)  
  30.     at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2870)  
  31.     at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1573)  
  32.     at com.mysql.jdbc.ServerPreparedStatement.serverExecute(ServerPreparedStatement.java:1169)  
  33.     at com.mysql.jdbc.ServerPreparedStatement.executeInternal(ServerPreparedStatement.java:693)  
  34.     at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1404)  
  35.     at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1318)  
  36.     at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1303)  
  37.     at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:105)  
  38.     at prx.dao.core.AbstractPrxDao.executeUpdate(AbstractPrxDao.java:215)  
  39.     at prx.dao.Test.main(Test.java:21)  
  40. 2011/08/12 10:32:47 [ERROR] [事务失败,回滚]  
  41. 2011/08/12 10:32:47 [INFO] select * from t_attention  
  42. [attention_id:1, user_id:1, blog_id:1]  
  43. [attention_id:2, user_id:2, blog_id:2]  
  44. [attention_id:2, user_id:2, blog_id:2]  
  45. [attention_id:3, user_id:3, blog_id:3]  
  46. [attention_id:4, user_id:4, blog_id:4]  
  47. [attention_id:5, user_id:5, blog_id:5]  
  48. [attention_id:6, user_id:6, blog_id:6]  
  49. [attention_id:7, user_id:7, blog_id:7]  
  50. [attention_id:8, user_id:8, blog_id:8]  
  51. [attention_id:9, user_id:9, blog_id:9]  
  52. [attention_id:10, user_id:10, blog_id:10]  
  53. [attention_id:11, user_id:11, blog_id:11]  
  54. [attention_id:12, user_id:12, blog_id:12]  
  55. [attention_id:13, user_id:13, blog_id:13]  
  56. [attention_id:14, user_id:14, blog_id:14]  
  57. [attention_id:15, user_id:15, blog_id:15]  
  58. [attention_id:16, user_id:16, blog_id:16]  
  59. [attention_id:17, user_id:17, blog_id:17]  
  60. [attention_id:18, user_id:18, blog_id:18]  
  61. [attention_id:19, user_id:19, blog_id:19]  

 执行错误, 事务回滚失败

 

5. 验证水平分片的表的事务(无回滚)

Java代码   收藏代码
  1. public static void main(String[] args) throws Exception {  
  2.     IPrxDao dao = new MySqlDao();  
  3.       
  4.     //开启事务,插入几条数据  
  5.     dao.startTransaction();  
  6.     for(int i=10; i<20; i++) {  
  7.         dao.executeUpdate("insert into t_user(user_id, user_name, user_address) values(?, ?, ?)"new Object[]{i, "n"+i, "a"+i});  
  8.     }  
  9.     //主键冲突,出错,回滚。  
  10.     //dao.executeUpdate("insert into t_user(user_id) values(?)", new Object[]{1});  
  11.     dao.commitTransaction();  
  12.       
  13.       
  14.     //查看结果  
  15.     List<Map<String, Object>> list = dao.queryForMapList("select * from t_user"new Object[]{});  
  16.       
  17.     for(Map<String, Object> map : list) {  
  18.         System.out.println(StringUtil.valueOfMap(map));  
  19.     }  
  20. }  

 执行结果:

Cmd代码   收藏代码
  1. 2011/08/12 10:41:20 [INFO] MLog clients using log4j logging.  
  2. 2011/08/12 10:41:23 [INFO] Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]  
  3. 2011/08/12 10:41:24 [INFO] Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> 1hge3oc8h1g4j7pb1170bmp|929206, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1hge3oc8h1g4j7pb1170bmp|929206, idleConnectionTestPeriod -> 0, initialPoolSize -> 3, jdbcUrl -> jdbc:mysql://192.168.14.129:8066/test?autoReconnect=true&characterEncoding=UTF-8, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 15, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 3, numHelperThreads -> 3, numThreadsAwaitingCheckoutDefaultUser -> 0, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false ]  
  4. 2011/08/12 10:41:27 [INFO] [开始事务]  
  5. 2011/08/12 10:41:27 [INFO] insert into t_user(user_id, user_name, user_address) values(?, ?, ?)  
  6. 2011/08/12 10:41:27 [INFO] values:[10,n10,a10]  
  7. 2011/08/12 10:41:27 [INFO] insert into t_user(user_id, user_name, user_address) values(?, ?, ?)  
  8. 2011/08/12 10:41:27 [INFO] values:[11,n11,a11]  
  9. 2011/08/12 10:41:27 [INFO] insert into t_user(user_id, user_name, user_address) values(?, ?, ?)  
  10. 2011/08/12 10:41:27 [INFO] values:[12,n12,a12]  
  11. 2011/08/12 10:41:27 [INFO] insert into t_user(user_id, user_name, user_address) values(?, ?, ?)  
  12. 2011/08/12 10:41:27 [INFO] values:[13,n13,a13]  
  13. 2011/08/12 10:41:27 [INFO] insert into t_user(user_id, user_name, user_address) values(?, ?, ?)  
  14. 2011/08/12 10:41:27 [INFO] values:[14,n14,a14]  
  15. 2011/08/12 10:41:27 [INFO] insert into t_user(user_id, user_name, user_address) values(?, ?, ?)  
  16. 2011/08/12 10:41:27 [INFO] values:[15,n15,a15]  
  17. 2011/08/12 10:41:28 [INFO] insert into t_user(user_id, user_name, user_address) values(?, ?, ?)  
  18. 2011/08/12 10:41:28 [INFO] values:[16,n16,a16]  
  19. 2011/08/12 10:41:28 [INFO] insert into t_user(user_id, user_name, user_address) values(?, ?, ?)  
  20. 2011/08/12 10:41:28 [INFO] values:[17,n17,a17]  
  21. 2011/08/12 10:41:28 [INFO] insert into t_user(user_id, user_name, user_address) values(?, ?, ?)  
  22. 2011/08/12 10:41:28 [INFO] values:[18,n18,a18]  
  23. 2011/08/12 10:41:28 [INFO] insert into t_user(user_id, user_name, user_address) values(?, ?, ?)  
  24. 2011/08/12 10:41:28 [INFO] values:[19,n19,a19]  
  25. 2011/08/12 10:41:28 [INFO] [事务提交]  
  26. 2011/08/12 10:41:28 [INFO] select * from t_user  
  27. [user_id:2, user_name:n2, user_address:a2]  
  28. [user_id:4, user_name:n4, user_address:a4]  
  29. [user_id:6, user_name:n6, user_address:a6]  
  30. [user_id:10, user_name:n10, user_address:a10]  
  31. [user_id:12, user_name:n12, user_address:a12]  
  32. [user_id:14, user_name:n14, user_address:a14]  
  33. [user_id:16, user_name:n16, user_address:a16]  
  34. [user_id:18, user_name:n18, user_address:a18]  
  35. [user_id:1, user_name:n1, user_address:a1]  
  36. [user_id:3, user_name:n3, user_address:a3]  
  37. [user_id:5, user_name:n5, user_address:a5]  
  38. [user_id:11, user_name:n11, user_address:a11]  
  39. [user_id:13, user_name:n13, user_address:a13]  
  40. [user_id:15, user_name:n15, user_address:a15]  
  41. [user_id:17, user_name:n17, user_address:a17]  
  42. [user_id:19, user_name:n19, user_address:a19]  

 执行成功。

 

6. 验证水平分片的表的事务(有回滚)

Java代码   收藏代码
  1. public static void main(String[] args) throws Exception {  
  2.     IPrxDao dao = new MySqlDao();  
  3.       
  4.     //开启事务,插入几条数据  
  5.     dao.startTransaction();  
  6.     for(int i=20; i<30; i++) {  
  7.         dao.executeUpdate("insert into t_user(user_id, user_name, user_address) values(?, ?, ?)"new Object[]{i, "n"+i, "a"+i});  
  8.     }  
  9.     //主键冲突,出错,回滚。  
  10.     dao.executeUpdate("insert into t_user(user_id) values(?)"new Object[]{1});  
  11.     dao.commitTransaction();  
  12.       
  13.       
  14.     //查看结果  
  15.     List<Map<String, Object>> list = dao.queryForMapList("select * from t_user"new Object[]{});  
  16.       
  17.     for(Map<String, Object> map : list) {  
  18.         System.out.println(StringUtil.valueOfMap(map));  
  19.     }  
  20. }  
 
Cmd代码   收藏代码
  1. 2011/08/12 10:44:05 [INFO] MLog clients using log4j logging.  
  2. 2011/08/12 10:44:05 [INFO] Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]  
  3. 2011/08/12 10:44:05 [INFO] Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> 1hge3oc8h1g4mqbxeevnxo|b169f8, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1hge3oc8h1g4mqbxeevnxo|b169f8, idleConnectionTestPeriod -> 0, initialPoolSize -> 3, jdbcUrl -> jdbc:mysql://192.168.14.129:8066/test?autoReconnect=true&characterEncoding=UTF-8, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 15, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 3, numHelperThreads -> 3, numThreadsAwaitingCheckoutDefaultUser -> 0, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false ]  
  4. 2011/08/12 10:44:05 [INFO] [开始事务]  
  5. 2011/08/12 10:44:05 [INFO] insert into t_user(user_id, user_name, user_address) values(?, ?, ?)  
  6. 2011/08/12 10:44:05 [INFO] values:[20,n20,a20]  
  7. 2011/08/12 10:44:06 [INFO] insert into t_user(user_id, user_name, user_address) values(?, ?, ?)  
  8. 2011/08/12 10:44:06 [INFO] values:[21,n21,a21]  
  9. 2011/08/12 10:44:06 [INFO] insert into t_user(user_id, user_name, user_address) values(?, ?, ?)  
  10. 2011/08/12 10:44:06 [INFO] values:[22,n22,a22]  
  11. 2011/08/12 10:44:06 [INFO] insert into t_user(user_id, user_name, user_address) values(?, ?, ?)  
  12. 2011/08/12 10:44:06 [INFO] values:[23,n23,a23]  
  13. 2011/08/12 10:44:06 [INFO] insert into t_user(user_id, user_name, user_address) values(?, ?, ?)  
  14. 2011/08/12 10:44:06 [INFO] values:[24,n24,a24]  
  15. 2011/08/12 10:44:06 [INFO] insert into t_user(user_id, user_name, user_address) values(?, ?, ?)  
  16. 2011/08/12 10:44:06 [INFO] values:[25,n25,a25]  
  17. 2011/08/12 10:44:06 [INFO] insert into t_user(user_id, user_name, user_address) values(?, ?, ?)  
  18. 2011/08/12 10:44:06 [INFO] values:[26,n26,a26]  
  19. 2011/08/12 10:44:06 [INFO] insert into t_user(user_id, user_name, user_address) values(?, ?, ?)  
  20. 2011/08/12 10:44:06 [INFO] values:[27,n27,a27]  
  21. 2011/08/12 10:44:06 [INFO] insert into t_user(user_id, user_name, user_address) values(?, ?, ?)  
  22. 2011/08/12 10:44:06 [INFO] values:[28,n28,a28]  
  23. 2011/08/12 10:44:06 [INFO] insert into t_user(user_id, user_name, user_address) values(?, ?, ?)  
  24. 2011/08/12 10:44:06 [INFO] values:[29,n29,a29]  
  25. 2011/08/12 10:44:06 [INFO] insert into t_user(user_id) values(?)  
  26. 2011/08/12 10:44:06 [INFO] values:[1]  
  27. 2011/08/12 10:44:06 [ERROR] Duplicate entry '1' for key 'PRIMARY'  
  28. com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Duplicate entry '1' for key 'PRIMARY'  
  29.     at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:931)  
  30.     at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2870)  
  31.     at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1573)  
  32.     at com.mysql.jdbc.ServerPreparedStatement.serverExecute(ServerPreparedStatement.java:1169)  
  33.     at com.mysql.jdbc.ServerPreparedStatement.executeInternal(ServerPreparedStatement.java:693)  
  34.     at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1404)  
  35.     at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1318)  
  36.     at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1303)  
  37.     at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:105)  
  38.     at prx.dao.core.AbstractPrxDao.executeUpdate(AbstractPrxDao.java:215)  
  39.     at prx.dao.Test.main(Test.java:21)  
  40. 2011/08/12 10:44:06 [ERROR] [事务失败,回滚]  
  41. 2011/08/12 10:44:06 [INFO] select * from t_user  
  42. [user_id:1, user_name:n1, user_address:a1]  
  43. [user_id:3, user_name:n3, user_address:a3]  
  44. [user_id:5, user_name:n5, user_address:a5]  
  45. [user_id:11, user_name:n11, user_address:a11]  
  46. [user_id:13, user_name:n13, user_address:a13]  
  47. [user_id:15, user_name:n15, user_address:a15]  
  48. [user_id:17, user_name:n17, user_address:a17]  
  49. [user_id:19, user_name:n19, user_address:a19]  
  50. [user_id:21, user_name:n21, user_address:a21]  
  51. [user_id:23, user_name:n23, user_address:a23]  
  52. [user_id:25, user_name:n25, user_address:a25]  
  53. [user_id:27, user_name:n27, user_address:a27]  
  54. [user_id:29, user_name:n29, user_address:a29]  
  55. [user_id:2, user_name:n2, user_address:a2]  
  56. [user_id:4, user_name:n4, user_address:a4]  
  57. [user_id:6, user_name:n6, user_address:a6]  
  58. [user_id:10, user_name:n10, user_address:a10]  
  59. [user_id:12, user_name:n12, user_address:a12]  
  60. [user_id:14, user_name:n14, user_address:a14]  
  61. [user_id:16, user_name:n16, user_address:a16]  
  62. [user_id:18, user_name:n18, user_address:a18]  

结果:

user_id 为偶数的部分都没有插入数据,回滚成功。 user_id 为偶数 分片到 server1 为主数据库

user_id 为奇数的部分都已经插入数据,回滚失败。 user_id 为奇数 分片到 server2

 

综上: 所有分片的表,都不能支持事务。

 

验证存储过程

 

这个就不写验证过程了。 直接写结果好了。

 

结果:

1. 只可以调用 server1 主数据库 的 procedure , 不能调用 server2 的。 

2. server1 的 procedure 只能操作 server1 中的表, 不支持水平和垂直分片。水平切分的 t_user, 不管 user_id 奇偶 均插入到 server1。垂直切分的t_attention 表不能访问。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值