use of org.nutz.trans.Atom in project nutz by nutzam.
the class SimpleSpeedTest method test_speed.
@Ignore
@Test
public void test_speed() throws SecurityException, NoSuchMethodException {
final SimpleSpeedTest z = new SimpleSpeedTest();
final String elstr = "num + (i - 1 + 2 - 3 + 4 - 5 + 6 - 7)-z.abc(i)";
final Context context = Lang.context("{num:0}");
context.set("z", z);
System.out.println("\n" + Strings.dup('=', 100));
Stopwatch sw = Stopwatch.run(new Atom() {
public void run() {
int num = 0;
for (int i = 0; i < max; i++) num = num + (i - 1 + 2 - 3 + 4 - 5 + 6 - 7) - z.abc(i);
//System.out.println("Num: " + num);
}
});
System.out.println("\n" + Strings.dup('=', 100));
Stopwatch sw3 = Stopwatch.run(new Atom() {
public void run() {
try {
context.set("num", 0);
for (int i = 0; i < max; i++) context.set("num", El.eval(context.set("i", i), elstr));
//System.out.println("Num: " + context.getInt("num"));
} catch (Exception e) {
throw Lang.wrapThrow(e);
}
}
});
System.out.println("\n" + Strings.dup('=', 100));
Stopwatch sw4 = Stopwatch.run(new Atom() {
public void run() {
try {
El el2pre = new El(elstr);
context.set("num", 0);
context.set("z", z);
for (int i = 0; i < max; i++) context.set("num", el2pre.eval(context.set("i", i)));
//System.out.println("Num: " + context.getInt("num"));
} catch (Exception e) {
throw Lang.wrapThrow(e);
}
}
});
System.out.println("\n" + Strings.dup('=', 100));
Stopwatch sw5 = Stopwatch.run(new Atom() {
public void run() {
try {
El el2pre = new El(elstr);
context.set("num", 0);
context.set("z", z);
for (int i = 0; i < max; i++) context.set("num", el2pre.eval(context.set("i", i)));
//System.out.println("Num: " + context.getInt("num"));
} catch (Exception e) {
throw Lang.wrapThrow(e);
}
}
});
System.out.println("\n" + Strings.dup('=', 100));
System.out.printf("\n%20s : %s", "Invoke", sw.toString());
System.out.printf("\n%20s : %s", "Reflect", sw3.toString());
System.out.printf("\n%20s : %s", "Reflect", sw4.toString());
System.out.printf("\n%20s : %s", "Reflect", sw5.toString());
System.out.println();
}
use of org.nutz.trans.Atom in project nutz by nutzam.
the class NutDaoRunner method run.
public void run(final DataSource dataSource, final ConnCallback callback) {
if (callback instanceof DaoInterceptorChain) {
// 看看是不是应该强制使用事务
DaoStatement[] sts = ((DaoInterceptorChain) callback).getDaoStatements();
boolean useTrans = false;
boolean isAllSelect = true;
for (DaoStatement st : sts) {
if (!st.isSelect() && !st.isForceExecQuery()) {
isAllSelect = false;
break;
}
}
switch(meta.getType()) {
case PSQL:
// PSQL必须带事务,不然Clob和Blob操作必死
useTrans = true;
break;
case SQLITE:
// SQLITE仅支持2种事务级别
Transaction t = Trans.get();
if (t == null) {
if (isAllSelect)
useTrans = false;
else {
((DaoInterceptorChain) callback).setAutoTransLevel(Connection.TRANSACTION_READ_UNCOMMITTED);
useTrans = true;
}
} else if (t.getLevel() != Connection.TRANSACTION_SERIALIZABLE && t.getLevel() != Connection.TRANSACTION_READ_UNCOMMITTED) {
t.setLevel(Connection.TRANSACTION_READ_UNCOMMITTED);
useTrans = true;
}
break;
default:
useTrans = !(Trans.isTransactionNone() && (sts.length == 1 || isAllSelect));
break;
}
// 看来需要开启事务了
if (useTrans) {
Trans.exec(((DaoInterceptorChain) callback).getAutoTransLevel(), new Atom() {
public void run() {
_run(dataSource, callback);
}
});
return;
}
}
// 不需要额外加事务,直接通过
_run(dataSource, callback);
}
use of org.nutz.trans.Atom in project nutz by nutzam.
the class DynamicManyTest method delete_links_partly.
@Test
public void delete_links_partly() {
TableName.run(platoon, new Atom() {
public void run() {
TableName.run(platoon, new Atom() {
public void run() {
Soldier s = dao.fetchLinks(dao.fetch(Soldier.class, "ZZH"), "guns");
s.getGuns()[1] = null;
dao.deleteLinks(s, "guns");
assertEquals(9, dao.count(Gun.class));
}
});
}
});
}
use of org.nutz.trans.Atom in project nutz by nutzam.
the class SimpleDaoTest method new_nutdao_inside_trans.
@Test
public void new_nutdao_inside_trans() {
// 这纯粹是重现bug的代码,不要学
final DataSource ds = ioc.get(DataSource.class);
Trans.exec(new Atom() {
public void run() {
new NutDao(ds);
}
});
}
use of org.nutz.trans.Atom in project nutz by nutzam.
the class SimpleDaoTest method test_fastinsert_speed.
// @Test
// public void test_map_blob() throws FileNotFoundException {
// if (dao.exists("t_test_map_blob")) {
// dao.drop("t_test_map_blob");
// Lang.quiteSleep(1000);
// }
// dao.execute(Sqls.create("create table t_test_map_blob(id
// VARCHAR(60),filecontent blob)"));
//
// NutMap map = new NutMap().setv(".table", "t_test_map_blob");
// map.put("id", R.UU32());
// map.put("filecontent", new
// FileInputStream("W:\\usb3.0_intel_1.0.10.255_w7.zip"));
//
// dao.insert(map);
//
// Record re = dao.fetch("t_test_map_blob", Cnd.NEW());
// assertNotNull(re);
// System.out.println(re.get("filecontent").getClass());
// System.out.println(new String((byte[])re.get("filecontent")));
//
//// assertEquals("你好", new String((byte[])re.get("filecontent")));
// }
//@Test
public void test_fastinsert_speed() {
SimpleDataSource ds = new SimpleDataSource();
ds.setJdbcUrl("jdbc:mysql://localhost/nutztest");
ds.setUsername("root");
ds.setPassword("root");
dao = new NutDao(ds);
// 删表重建
dao.create(Pet.class, true);
Lang.sleep(1000);
Stopwatch sw = Stopwatch.begin();
// 生成10*2000个对象
List<List<Pet>> list = new ArrayList<List<Pet>>();
for (int i = 0; i < 10; i++) {
List<Pet> pets = new ArrayList<Pet>();
for (int j = 0; j < 2000; j++) {
Pet pet = Pet.create(R.UU32());
pets.add(pet);
}
list.add(pets);
}
sw.stop();
System.out.println("生成对象的耗时: " + sw);
for (final List<Pet> tmp : list) {
sw = Stopwatch.begin();
Trans.exec(new Atom() {
public void run() {
dao.fastInsert(tmp);
}
});
sw.stop();
System.out.println("fastInsert插入2000个对象的耗时" + sw);
}
dao.create(Pet.class, false);
for (int i = 0; i < 10; i++) {
try {
final int t = i;
Connection conn = ds.getConnection();
conn.setAutoCommit(false);
sw = Stopwatch.begin();
System.out.println(System.currentTimeMillis());
PreparedStatement ps = conn.prepareStatement("INSERT INTO t_pet(name,alias) VALUES(?,?)");
System.out.println(System.currentTimeMillis());
for (int j = 0; j < 2000; j++) {
ps.setString(1, "pet_" + t + "_" + j);
ps.setString(2, "");
// ps.setInt(3, 30);
// ps.setInt(4, 0);
// ps.setDate(5, null);
// ps.setFloat(6, 0);
ps.addBatch();
}
System.out.println(System.currentTimeMillis());
ps.executeBatch();
conn.commit();
sw.stop();
System.out.println(sw);
ps.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Aggregations