Search in sources :

Example 11 with Atom

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();
}
Also used : Context(org.nutz.lang.util.Context) El(org.nutz.el.El) Stopwatch(org.nutz.lang.Stopwatch) Atom(org.nutz.trans.Atom) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 12 with Atom

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);
}
Also used : DaoInterceptorChain(org.nutz.dao.DaoInterceptorChain) Transaction(org.nutz.trans.Transaction) DaoStatement(org.nutz.dao.sql.DaoStatement) Atom(org.nutz.trans.Atom)

Example 13 with Atom

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));
                }
            });
        }
    });
}
Also used : Gun(org.nutz.dao.test.meta.Gun) Soldier(org.nutz.dao.test.meta.Soldier) Atom(org.nutz.trans.Atom) Test(org.junit.Test)

Example 14 with Atom

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);
        }
    });
}
Also used : NutDao(org.nutz.dao.impl.NutDao) Atom(org.nutz.trans.Atom) DataSource(javax.sql.DataSource) SimpleDataSource(org.nutz.dao.impl.SimpleDataSource) Test(org.junit.Test)

Example 15 with Atom

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();
        }
    }
}
Also used : NutDao(org.nutz.dao.impl.NutDao) SQLException(java.sql.SQLException) Stopwatch(org.nutz.lang.Stopwatch) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Atom(org.nutz.trans.Atom) SimpleDataSource(org.nutz.dao.impl.SimpleDataSource) List(java.util.List) ArrayList(java.util.ArrayList) Issue1163Pet(org.nutz.dao.test.meta.issue1163.Issue1163Pet) Pet(org.nutz.dao.test.meta.Pet)

Aggregations

Atom (org.nutz.trans.Atom)20 Test (org.junit.Test)15 Pet (org.nutz.dao.test.meta.Pet)9 NutDao (org.nutz.dao.impl.NutDao)4 Stopwatch (org.nutz.lang.Stopwatch)3 Connection (java.sql.Connection)2 ArrayList (java.util.ArrayList)2 SimpleDataSource (org.nutz.dao.impl.SimpleDataSource)2 File (java.io.File)1 PreparedStatement (java.sql.PreparedStatement)1 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 DataSource (javax.sql.DataSource)1 BasicDataSource (org.apache.commons.dbcp.BasicDataSource)1 Ignore (org.junit.Ignore)1 DaoInterceptorChain (org.nutz.dao.DaoInterceptorChain)1 SqlNotFoundException (org.nutz.dao.SqlNotFoundException)1 FileSqlManager (org.nutz.dao.impl.FileSqlManager)1