import java.math.BigDecimal; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Statement; import java.util.Map.Entry; import java.util.Random; import java.util.TreeMap;
import org.nutz.dao.Chain; import org.nutz.dao.Cnd; import org.nutz.dao.impl.NutDao; import org.nutz.trans.Atom; import org.nutz.trans.Trans;
import cn.gbase.jiangsu.data.transfer.bean.ShopUser;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
public class TestMoney {
NutDao daoOut = new NutDao(); MysqlDataSource dsOut = null; /** * 随机数. */ private static final Random RANDOM = new Random(); private void setOutDataSource() { dsOut = new com.mysql.jdbc.jdbc2.optional.MysqlDataSource();
// 更新连接信息 dsOut.setServerName("localhost"); dsOut.setDatabaseName("shop"); dsOut.setUser("root"); dsOut.setPassword("123456");
// 设置数据源 daoOut.setDataSource(dsOut); }
/** * 随机数字的字符串. * * @param length * 长度 * @return 随机字符串 */ private String getRandomNumString(int length) { String base = "0123456789"; StringBuilder sb = new StringBuilder(); for (int i = 0; i < length; i++) { int number = RANDOM.nextInt(base.length()); sb.append(base.charAt(number)); } return sb.toString(); } private void doWork() throws SQLException, InterruptedException { setOutDataSource();
for(int xx=0;xx<10;xx++){ // 初始为1 daoOut.update(ShopUser.class, Chain.make("accountBalance", 0), Cnd.where("id", "=", 6)); // 100个线程加钱 BigDecimal total = BigDecimal.ZERO; for (int i = 0; i < 100; i++) { final BigDecimal money = new BigDecimal("0." + getRandomNumString(10)); total = total.add(money); new Thread() { public void run() { //updateMoneyNutz1(money); //nutz,转float,不行。 //updateMoneyNutz2(money); //nutz,转string,可以 //updateMoneyJdbc1(money); //jdbc,使用参数,可以 updateMoneyJdbc2(money); //jdbc,转float,拼sql,不行。当然,转string拼sql是可以的。 } }.start(); } Thread.sleep(3000); System.out.println("total money:"+total); BigDecimal db = daoOut.fetch(ShopUser.class, Cnd.where("id", "=", 6)).getAccountBalance(); assert total.equals(db); }
} /** * nutz更新,转成float. * @param money */ private void updateMoneyNutz1(BigDecimal money) { daoOut.update(ShopUser.class, Chain.makeSpecial("accountBalance", "+" + money.floatValue()), Cnd.where("id", "=", 6)); } /** * nutz更新,直接BigDicimal * @param money */ private void updateMoneyNutz2(BigDecimal money) { daoOut.update(ShopUser.class, Chain.makeSpecial("accountBalance", "+" + money.toString()), Cnd.where("id", "=", 6)); } /** * jdbc,使用参数 * @param money */ private void updateMoneyJdbc1(BigDecimal money) {
// 随机延迟 try { Thread.sleep(new Random().nextInt(1000)); } catch (InterruptedException e) { e.printStackTrace(); }
Connection conn = null; // 连接对象 PreparedStatement pstmt = null; // 预编译的SQL语句对象 try {
Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf-8"; conn = DriverManager.getConnection(url, "root", "123456");
String sql = "update shop_users set account_balance = account_balance + ? where id=6"; pstmt = conn.prepareStatement(sql); pstmt.setBigDecimal(1, money); pstmt.execute();
pstmt.close(); conn.close(); } catch (Exception e) {
e.printStackTrace(); } finally { if (pstmt != null) try { pstmt.close(); } catch (Exception ignore) { } if (conn != null) try { conn.close(); } catch (Exception ignore) { } } } /** * jdbc,转成float,拼接sql * @param money */ private void updateMoneyJdbc2(BigDecimal money) {
// 随机延迟 try { Thread.sleep(new Random().nextInt(1000)); } catch (InterruptedException e) { e.printStackTrace(); }
Connection conn = null; // 连接对象 try {
Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf-8"; conn = DriverManager.getConnection(url, "root", "123456");
String sql = "update shop_users set account_balance = account_balance + " + money.floatValue()+ " where id=6"; System.out.println(sql); conn.createStatement().execute(sql);
conn.close(); } catch (Exception e) {
e.printStackTrace(); } finally { if (conn != null) try { conn.close(); } catch (Exception ignore) { } } }
public static void main(String[] args) { TestMoney xx = new TestMoney(); try { try { xx.doWork(); } catch (InterruptedException e) { } } catch (SQLException e) { e.printStackTrace(); } }
}
|