本文主要是介绍Mysql 基于 Amoeba 的 水平和垂直 分片(下),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
验证事务管理
使用程序验证。
1. 验证没有分片的表的事务操作(无回滚)。
程序片段:
Java代码
package prx.dao;
import java.util.List;
import java.util.Map;
import prx.core.string.StringUtil;
import prx.dao.core.IPrxDao;
import prx.dao.core.impl.MySqlDao;
public class Test {
public static void main(String[] args) throws Exception {
IPrxDao dao = new MySqlDao();
//开启事务,插入几条数据
dao.startTransaction();
for(int i=3; i<10; i++) {
dao.executeUpdate("insert into t_message(message_id, message_content, user_id) values(?, ?, ?)", new Object[]{i, "c" + i, i});
}
dao.commitTransaction();
//查看结果
List<Map<String, Object>> list = dao.queryForMapList("select * from t_message", new Object[]{});
for(Map<String, Object> map : list) {
System.out.println(StringUtil.valueOfMap(map));
}
}
}
执行结果:
Cmd代码
2011/08/12 10:17:33 [INFO] MLog clients using log4j logging.
2011/08/12 10:17:34 [INFO] Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]
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 ]
2011/08/12 10:17:36 [INFO] [开始事务]
2011/08/12 10:17:36 [INFO] insert into t_message(message_id, message_content, user_id) values(?, ?, ?)
2011/08/12 10:17:36 [INFO] values:[3,c3,3]
2011/08/12 10:17:36 [INFO] insert into t_message(message_id, message_content, user_id) values(?, ?, ?)
2011/08/12 10:17:36 [INFO] values:[4,c4,4]
2011/08/12 10:17:36 [INFO] insert into t_message(message_id, message_content, user_id) values(?, ?, ?)
2011/08/12 10:17:36 [INFO] values:[5,c5,5]
2011/08/12 10:17:36 [INFO] insert into t_message(message_id, message_content, user_id) values(?, ?, ?)
2011/08/12 10:17:36 [INFO] values:[6,c6,6]
2011/08/12 10:17:36 [INFO] insert into t_message(message_id, message_content, user_id) values(?, ?, ?)
2011/08/12 10:17:36 [INFO] values:[7,c7,7]
2011/08/12 10:17:36 [INFO] insert into t_message(message_id, message_content, user_id) values(?, ?, ?)
2011/08/12 10:17:36 [INFO] values:[8,c8,8]
2011/08/12 10:17:36 [INFO] insert into t_message(message_id, message_content, user_id) values(?, ?, ?)
2011/08/12 10:17:36 [INFO] values:[9,c9,9]
2011/08/12 10:17:36 [INFO] [事务提交]
2011/08/12 10:17:36 [INFO] select * from t_message
[message_id:1, message_content:c1, user_id:1]
[message_id:2, message_content:c2, user_id:2]
[message_id:3, message_content:c3, user_id:3]
[message_id:4, message_content:c4, user_id:4]
[message_id:5, message_content:c5, user_id:5]
[message_id:6, message_content:c6, user_id:6]
[message_id:7, message_content:c7, user_id:7]
[message_id:8, message_content:c8, user_id:8]
[message_id:9, message_content:c9, user_id:9]
执行正确无误。
2. 验证没有分片的表的操作(有回滚)。
程序片段:
Java代码
public static void main(String[] args) throws Exception {
IPrxDao dao = new MySqlDao();
//开启事务,插入几条数据
dao.startTransaction();
for(int i=10; i<20; i++) {
dao.executeUpdate("insert into t_message(message_id, message_content, user_id) values(?, ?, ?)", new Object[]{i, "c" + i, i});
}
//主键冲突,出错,回滚。
dao.executeUpdate("insert into t_user(user_id) values(?)", new Object[]{1});
dao.commitTransaction();
//查看结果
List<Map<String, Object>> list = dao.queryForMapList("select * from t_message", new Object[]{});
for(Map<String, Object> map : list) {
System.out.println(StringUtil.valueOfMap(map));
}
}
执行结果:
Cmd代码
2011/08/12 10:24:37 [INFO] MLog clients using log4j logging.
2011/08/12 10:24:37 [INFO] Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]
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 ]
2011/08/12 10:24:38 [INFO] [开始事务]
2011/08/12 10:24:38 [INFO] insert into t_message(message_id, message_content, user_id) values(?, ?, ?)
2011/08/12 10:24:38 [INFO] values:[10,c10,10]
2011/08/12 10:24:38 [INFO] insert into t_message(message_id, message_content, user_id) values(?, ?, ?)
2011/08/12 10:24:38 [INFO] values:[11,c11,11]
2011/08/12 10:24:38 [INFO] insert into t_message(message_id, message_content, user_id) values(?, ?, ?)
2011/08/12 10:24:38 [INFO] values:[12,c12,12]
2011/08/12 10:24:38 [INFO] insert into t_message(message_id, message_content, user_id) values(?, ?, ?)
2011/08/12 10:24:38 [INFO] values:[13,c13,13]
2011/08/12 10:24:38 [INFO] insert into t_message(message_id, message_content, user_id) values(?, ?, ?)
2011/08/12 10:24:38 [INFO] values:[14,c14,14]
2011/08/12 10:24:38 [INFO] insert into t_message(message_id, message_content, user_id) values(?, ?, ?)
2011/08/12 10:24:38 [INFO] values:[15,c15,15]
2011/08/12 10:24:38 [INFO] insert into t_message(message_id, message_content, user_id) values(?, ?, ?)
2011/08/12 10:24:38 [INFO] values:[16,c16,16]
2011/08/12 10:24:38 [INFO] insert into t_message(message_id, message_content, user_id) values(?, ?, ?)
2011/08/12 10:24:38 [INFO] values:[17,c17,17]
2011/08/12 10:24:38 [INFO] insert into t_message(message_id, message_content, user_id) values(?, ?, ?)
2011/08/12 10:24:38 [INFO] values:[18,c18,18]
2011/08/12 10:24:38 [INFO] insert into t_message(message_id, message_content, user_id) values(?, ?, ?)
2011/08/12 10:24:38 [INFO] values:[19,c19,19]
2011/08/12 10:24:38 [INFO] insert into t_user(user_id) values(?)
2011/08/12 10:24:38 [INFO] values:[1]
2011/08/12 10:24:38 [ERROR] Duplicate entry '1' for key 'PRIMARY'
com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Duplicate entry '1' for key 'PRIMARY'
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:931)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2870)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1573)
at com.mysql.jdbc.ServerPreparedStatement.serverExecute(ServerPreparedStatement.java:1169)
at com.mysql.jdbc.ServerPreparedStatement.executeInternal(ServerPreparedStatement.java:693)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1404)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1318)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1303)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:105)
at prx.dao.core.AbstractPrxDao.executeUpdate(AbstractPrxDao.java:215)
at prx.dao.Test.main(Test.java:21)
2011/08/12 10:24:38 [ERROR] [事务失败,回滚]
2011/08/12 10:24:38 [INFO] select * from t_message
[message_id:1, message_content:c1, user_id:1]
[message_id:2, message_content:c2, user_id:2]
[message_id:3, message_content:c3, user_id:3]
[message_id:4, message_content:c4, user_id:4]
[message_id:5, message_content:c5, user_id:5]
[message_id:6, message_content:c6, user_id:6]
[message_id:7, message_content:c7, user_id:7]
[message_id:8, message_content:c8, user_id:8]
[message_id:9, message_content:c9, user_id:9]
执行正确无误。
3. 验证垂直分片的表的事务(无回滚)
Java代码
public static void main(String[] args) throws Exception {
IPrxDao dao = new MySqlDao();
//开启事务,插入几条数据
dao.startTransaction();
for(int i=3; i<10; i++) {
dao.executeUpdate("insert into t_attention(attention_id, user_id, blog_id) values(?, ?, ?)", new Object[]{i, i, i});
}
//主键冲突,出错,回滚。
//dao.executeUpdate("insert into t_user(user_id) values(?)", new Object[]{1});
dao.commitTransaction();
//查看结果
List<Map<String, Object>> list = dao.queryForMapList("select * from t_attention", new Object[]{});
for(Map<String, Object> map : list) {
System.out.println(StringUtil.valueOfMap(map));
}
}
执行结果:
Cmd代码
2011/08/12 10:30:49 [INFO] MLog clients using log4j logging.
2011/08/12 10:30:50 [INFO] Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]
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 ]
2011/08/12 10:30:50 [INFO] [开始事务]
2011/08/12 10:30:50 [INFO] insert into t_attention(attention_id, user_id, blog_id) values(?, ?, ?)
2011/08/12 10:30:50 [INFO] values:[3,3,3]
2011/08/12 10:30:50 [INFO] insert into t_attention(attention_id, user_id, blog_id) values(?, ?, ?)
2011/08/12 10:30:50 [INFO] values:[4,4,4]
2011/08/12 10:30:50 [INFO] insert into t_attention(attention_id, user_id, blog_id) values(?, ?, ?)
2011/08/12 10:30:50 [INFO] values:[5,5,5]
2011/08/12 10:30:50 [INFO] insert into t_attention(attention_id, user_id, blog_id) values(?, ?, ?)
2011/08/12 10:30:50 [INFO] values:[6,6,6]
2011/08/12 10:30:50 [INFO] insert into t_attention(attention_id, user_id, blog_id) values(?, ?, ?)
2011/08/12 10:30:50 [INFO] values:[7,7,7]
2011/08/12 10:30:50 [INFO] insert into t_attention(attention_id, user_id, blog_id) values(?, ?, ?)
2011/08/12 10:30:50 [INFO] values:[8,8,8]
2011/08/12 10:30:50 [INFO] insert into t_attention(attention_id, user_id, blog_id) values(?, ?, ?)
2011/08/12 10:30:50 [INFO] values:[9,9,9]
2011/08/12 10:30:50 [INFO] [事务提交]
2011/08/12 10:30:50 [INFO] select * from t_attention
[attention_id:1, user_id:1, blog_id:1]
[attention_id:2, user_id:2, blog_id:2]
[attention_id:2, user_id:2, blog_id:2]
[attention_id:3, user_id:3, blog_id:3]
[attention_id:4, user_id:4, blog_id:4]
[attention_id:5, user_id:5, blog_id:5]
[attention_id:6, user_id:6, blog_id:6]
[attention_id:7, user_id:7, blog_id:7]
[attention_id:8, user_id:8, blog_id:8]
[attention_id:9, user_id:9, blog_id:9]
执行成功。
4. 验证垂直分片的表(有回滚)
Java代码
public static void main(String[] args) throws Exception {
IPrxDao dao = new MySqlDao();
//开启事务,插入几条数据
dao.startTransaction();
for(int i=10; i<20; i++) {
dao.executeUpdate("insert into t_attention(attention_id, user_id, blog_id) values(?, ?, ?)", new Object[]{i, i, i});
}
//主键冲突,出错,回滚。
dao.executeUpdate("insert into t_user(user_id) values(?)", new Object[]{1});
dao.commitTransaction();
//查看结果
List<Map<String, Object>> list = dao.queryForMapList("select * from t_attention", new Object[]{});
for(Map<String, Object> map : list) {
System.out.println(StringUtil.valueOfMap(map));
}
}
执行结果:
Cmd代码
2011/08/12 10:32:46 [INFO] MLog clients using log4j logging.
2011/08/12 10:32:46 [INFO] Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]
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 ]
2011/08/12 10:32:47 [INFO] [开始事务]
2011/08/12 10:32:47 [INFO] insert into t_attention(attention_id, user_id, blog_id) values(?, ?, ?)
2011/08/12 10:32:47 [INFO] values:[10,10,10]
2011/08/12 10:32:47 [INFO] insert into t_attention(attention_id, user_id, blog_id) values(?, ?, ?)
2011/08/12 10:32:47 [INFO] values:[11,11,11]
2011/08/12 10:32:47 [INFO] insert into t_attention(attention_id, user_id, blog_id) values(?, ?, ?)
2011/08/12 10:32:47 [INFO] values:[12,12,12]
2011/08/12 10:32:47 [INFO] insert into t_attention(attention_id, user_id, blog_id) values(?, ?, ?)
2011/08/12 10:32:47 [INFO] values:[13,13,13]
2011/08/12 10:32:47 [INFO] insert into t_attention(attention_id, user_id, blog_id) values(?, ?, ?)
2011/08/12 10:32:47 [INFO] values:[14,14,14]
2011/08/12 10:32:47 [INFO] insert into t_attention(attention_id, user_id, blog_id) values(?, ?, ?)
2011/08/12 10:32:47 [INFO] values:[15,15,15]
2011/08/12 10:32:47 [INFO] insert into t_attention(attention_id, user_id, blog_id) values(?, ?, ?)
2011/08/12 10:32:47 [INFO] values:[16,16,16]
2011/08/12 10:32:47 [INFO] insert into t_attention(attention_id, user_id, blog_id) values(?, ?, ?)
2011/08/12 10:32:47 [INFO] values:[17,17,17]
2011/08/12 10:32:47 [INFO] insert into t_attention(attention_id, user_id, blog_id) values(?, ?, ?)
2011/08/12 10:32:47 [INFO] values:[18,18,18]
2011/08/12 10:32:47 [INFO] insert into t_attention(attention_id, user_id, blog_id) values(?, ?, ?)
2011/08/12 10:32:47 [INFO] values:[19,19,19]
2011/08/12 10:32:47 [INFO] insert into t_user(user_id) values(?)
2011/08/12 10:32:47 [INFO] values:[1]
2011/08/12 10:32:47 [ERROR] Duplicate entry '1' for key 'PRIMARY'
com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Duplicate entry '1' for key 'PRIMARY'
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:931)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2870)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1573)
at com.mysql.jdbc.ServerPreparedStatement.serverExecute(ServerPreparedStatement.java:1169)
at com.mysql.jdbc.ServerPreparedStatement.executeInternal(ServerPreparedStatement.java:693)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1404)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1318)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1303)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:105)
at prx.dao.core.AbstractPrxDao.executeUpdate(AbstractPrxDao.java:215)
at prx.dao.Test.main(Test.java:21)
2011/08/12 10:32:47 [ERROR] [事务失败,回滚]
2011/08/12 10:32:47 [INFO] select * from t_attention
[attention_id:1, user_id:1, blog_id:1]
[attention_id:2, user_id:2, blog_id:2]
[attention_id:2, user_id:2, blog_id:2]
[attention_id:3, user_id:3, blog_id:3]
[attention_id:4, user_id:4, blog_id:4]
[attention_id:5, user_id:5, blog_id:5]
[attention_id:6, user_id:6, blog_id:6]
[attention_id:7, user_id:7, blog_id:7]
[attention_id:8, user_id:8, blog_id:8]
[attention_id:9, user_id:9, blog_id:9]
[attention_id:10, user_id:10, blog_id:10]
[attention_id:11, user_id:11, blog_id:11]
[attention_id:12, user_id:12, blog_id:12]
[attention_id:13, user_id:13, blog_id:13]
[attention_id:14, user_id:14, blog_id:14]
[attention_id:15, user_id:15, blog_id:15]
[attention_id:16, user_id:16, blog_id:16]
[attention_id:17, user_id:17, blog_id:17]
[attention_id:18, user_id:18, blog_id:18]
[attention_id:19, user_id:19, blog_id:19]
执行错误, 事务回滚失败
5. 验证水平分片的表的事务(无回滚)
Java代码
public static void main(String[] args) throws Exception {
IPrxDao dao = new MySqlDao();
//开启事务,插入几条数据
dao.startTransaction();
for(int i=10; i<20; i++) {
dao.executeUpdate("insert into t_user(user_id, user_name, user_address) values(?, ?, ?)", new Object[]{i, "n"+i, "a"+i});
}
//主键冲突,出错,回滚。
//dao.executeUpdate("insert into t_user(user_id) values(?)", new Object[]{1});
dao.commitTransaction();
//查看结果
List<Map<String, Object>> list = dao.queryForMapList("select * from t_user", new Object[]{});
for(Map<String, Object> map : list) {
System.out.println(StringUtil.valueOfMap(map));
}
}
执行结果:
Cmd代码
2011/08/12 10:41:20 [INFO] MLog clients using log4j logging.
2011/08/12 10:41:23 [INFO] Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]
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 ]
2011/08/12 10:41:27 [INFO] [开始事务]
2011/08/12 10:41:27 [INFO] insert into t_user(user_id, user_name, user_address) values(?, ?, ?)
2011/08/12 10:41:27 [INFO] values:[10,n10,a10]
2011/08/12 10:41:27 [INFO] insert into t_user(user_id, user_name, user_address) values(?, ?, ?)
2011/08/12 10:41:27 [INFO] values:[11,n11,a11]
2011/08/12 10:41:27 [INFO] insert into t_user(user_id, user_name, user_address) values(?, ?, ?)
2011/08/12 10:41:27 [INFO] values:[12,n12,a12]
2011/08/12 10:41:27 [INFO] insert into t_user(user_id, user_name, user_address) values(?, ?, ?)
2011/08/12 10:41:27 [INFO] values:[13,n13,a13]
2011/08/12 10:41:27 [INFO] insert into t_user(user_id, user_name, user_address) values(?, ?, ?)
2011/08/12 10:41:27 [INFO] values:[14,n14,a14]
2011/08/12 10:41:27 [INFO] insert into t_user(user_id, user_name, user_address) values(?, ?, ?)
2011/08/12 10:41:27 [INFO] values:[15,n15,a15]
2011/08/12 10:41:28 [INFO] insert into t_user(user_id, user_name, user_address) values(?, ?, ?)
2011/08/12 10:41:28 [INFO] values:[16,n16,a16]
2011/08/12 10:41:28 [INFO] insert into t_user(user_id, user_name, user_address) values(?, ?, ?)
2011/08/12 10:41:28 [INFO] values:[17,n17,a17]
2011/08/12 10:41:28 [INFO] insert into t_user(user_id, user_name, user_address) values(?, ?, ?)
2011/08/12 10:41:28 [INFO] values:[18,n18,a18]
2011/08/12 10:41:28 [INFO] insert into t_user(user_id, user_name, user_address) values(?, ?, ?)
2011/08/12 10:41:28 [INFO] values:[19,n19,a19]
2011/08/12 10:41:28 [INFO] [事务提交]
2011/08/12 10:41:28 [INFO] select * from t_user
[user_id:2, user_name:n2, user_address:a2]
[user_id:4, user_name:n4, user_address:a4]
[user_id:6, user_name:n6, user_address:a6]
[user_id:10, user_name:n10, user_address:a10]
[user_id:12, user_name:n12, user_address:a12]
[user_id:14, user_name:n14, user_address:a14]
[user_id:16, user_name:n16, user_address:a16]
[user_id:18, user_name:n18, user_address:a18]
[user_id:1, user_name:n1, user_address:a1]
[user_id:3, user_name:n3, user_address:a3]
[user_id:5, user_name:n5, user_address:a5]
[user_id:11, user_name:n11, user_address:a11]
[user_id:13, user_name:n13, user_address:a13]
[user_id:15, user_name:n15, user_address:a15]
[user_id:17, user_name:n17, user_address:a17]
[user_id:19, user_name:n19, user_address:a19]
执行成功。
6. 验证水平分片的表的事务(有回滚)
Java代码
public static void main(String[] args) throws Exception {
IPrxDao dao = new MySqlDao();
//开启事务,插入几条数据
dao.startTransaction();
for(int i=20; i<30; i++) {
dao.executeUpdate("insert into t_user(user_id, user_name, user_address) values(?, ?, ?)", new Object[]{i, "n"+i, "a"+i});
}
//主键冲突,出错,回滚。
dao.executeUpdate("insert into t_user(user_id) values(?)", new Object[]{1});
dao.commitTransaction();
//查看结果
List<Map<String, Object>> list = dao.queryForMapList("select * from t_user", new Object[]{});
for(Map<String, Object> map : list) {
System.out.println(StringUtil.valueOfMap(map));
}
}
Cmd代码
2011/08/12 10:44:05 [INFO] MLog clients using log4j logging.
2011/08/12 10:44:05 [INFO] Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]
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 ]
2011/08/12 10:44:05 [INFO] [开始事务]
2011/08/12 10:44:05 [INFO] insert into t_user(user_id, user_name, user_address) values(?, ?, ?)
2011/08/12 10:44:05 [INFO] values:[20,n20,a20]
2011/08/12 10:44:06 [INFO] insert into t_user(user_id, user_name, user_address) values(?, ?, ?)
2011/08/12 10:44:06 [INFO] values:[21,n21,a21]
2011/08/12 10:44:06 [INFO] insert into t_user(user_id, user_name, user_address) values(?, ?, ?)
2011/08/12 10:44:06 [INFO] values:[22,n22,a22]
2011/08/12 10:44:06 [INFO] insert into t_user(user_id, user_name, user_address) values(?, ?, ?)
2011/08/12 10:44:06 [INFO] values:[23,n23,a23]
2011/08/12 10:44:06 [INFO] insert into t_user(user_id, user_name, user_address) values(?, ?, ?)
2011/08/12 10:44:06 [INFO] values:[24,n24,a24]
2011/08/12 10:44:06 [INFO] insert into t_user(user_id, user_name, user_address) values(?, ?, ?)
2011/08/12 10:44:06 [INFO] values:[25,n25,a25]
2011/08/12 10:44:06 [INFO] insert into t_user(user_id, user_name, user_address) values(?, ?, ?)
2011/08/12 10:44:06 [INFO] values:[26,n26,a26]
2011/08/12 10:44:06 [INFO] insert into t_user(user_id, user_name, user_address) values(?, ?, ?)
2011/08/12 10:44:06 [INFO] values:[27,n27,a27]
2011/08/12 10:44:06 [INFO] insert into t_user(user_id, user_name, user_address) values(?, ?, ?)
2011/08/12 10:44:06 [INFO] values:[28,n28,a28]
2011/08/12 10:44:06 [INFO] insert into t_user(user_id, user_name, user_address) values(?, ?, ?)
2011/08/12 10:44:06 [INFO] values:[29,n29,a29]
2011/08/12 10:44:06 [INFO] insert into t_user(user_id) values(?)
2011/08/12 10:44:06 [INFO] values:[1]
2011/08/12 10:44:06 [ERROR] Duplicate entry '1' for key 'PRIMARY'
com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Duplicate entry '1' for key 'PRIMARY'
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:931)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2870)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1573)
at com.mysql.jdbc.ServerPreparedStatement.serverExecute(ServerPreparedStatement.java:1169)
at com.mysql.jdbc.ServerPreparedStatement.executeInternal(ServerPreparedStatement.java:693)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1404)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1318)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1303)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:105)
at prx.dao.core.AbstractPrxDao.executeUpdate(AbstractPrxDao.java:215)
at prx.dao.Test.main(Test.java:21)
2011/08/12 10:44:06 [ERROR] [事务失败,回滚]
2011/08/12 10:44:06 [INFO] select * from t_user
[user_id:1, user_name:n1, user_address:a1]
[user_id:3, user_name:n3, user_address:a3]
[user_id:5, user_name:n5, user_address:a5]
[user_id:11, user_name:n11, user_address:a11]
[user_id:13, user_name:n13, user_address:a13]
[user_id:15, user_name:n15, user_address:a15]
[user_id:17, user_name:n17, user_address:a17]
[user_id:19, user_name:n19, user_address:a19]
[user_id:21, user_name:n21, user_address:a21]
[user_id:23, user_name:n23, user_address:a23]
[user_id:25, user_name:n25, user_address:a25]
[user_id:27, user_name:n27, user_address:a27]
[user_id:29, user_name:n29, user_address:a29]
[user_id:2, user_name:n2, user_address:a2]
[user_id:4, user_name:n4, user_address:a4]
[user_id:6, user_name:n6, user_address:a6]
[user_id:10, user_name:n10, user_address:a10]
[user_id:12, user_name:n12, user_address:a12]
[user_id:14, user_name:n14, user_address:a14]
[user_id:16, user_name:n16, user_address:a16]
[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 表不能访问。
这篇关于Mysql 基于 Amoeba 的 水平和垂直 分片(下)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!