Search in sources :

Example 1 with Atom

use of org.nutz.trans.Atom in project nutz by nutzam.

the class PsTest method main.

/**
	 * 测试新的Dao实现在postgresql下的表现
	 */
public static void main(String[] args) throws Throwable {
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("org.postgresql.Driver");
    ds.setUsername("postgres");
    ds.setPassword("123456");
    ds.setUrl("jdbc:postgresql:test");
    ds.setDefaultAutoCommit(false);
    final NutDao dao = new NutDao(ds);
    TableName.run(1, new Atom() {

        public void run() {
            Trans.exec(new Atom() {

                public void run() {
                    dao.create(Country.class, true);
                    System.out.println("------------???");
                    dao.insert(Country.make("A"));
                    try {
                        System.out.println("---------------------");
                        dao.insert(Country.make("A"));
                        System.out.println("+++++++++++++++++++++++++++++++++++");
                    } catch (DaoException e) {
                    }
                    try {
                        dao.insert(Country.make("C"));
                    } catch (Throwable e) {
                        System.out.println("天啊,还是报错!!");
                    }
                }
            });
        }
    });
    Connection conn = ds.getConnection();
    conn.prepareStatement("insert into dao_country(name,detail) values('ABC','CC')").execute();
    try {
        conn.prepareStatement("insert into dao_country(name,detail) values('ABC','CC')").execute();
    } catch (Exception e) {
    }
    conn.prepareStatement("insert into dao_country(name,detail) values('CC','CC')").execute();
    System.out.println("竟然能通过??!!");
    conn.commit();
}
Also used : NutDao(org.nutz.dao.impl.NutDao) Connection(java.sql.Connection) BasicDataSource(org.apache.commons.dbcp.BasicDataSource) Atom(org.nutz.trans.Atom)

Example 2 with Atom

use of org.nutz.trans.Atom in project nutz by nutzam.

the class UploadingSpeedTest method main.

public static void main(String[] args) {
    if (0 == args.length) {
        System.err.println("Lack files directory!");
        System.exit(0);
    }
    File dir = Files.findFile(args[0]);
    if (null == dir) {
        System.err.println("Fail to found directory: " + args[0]);
        System.exit(0);
    }
    final Uploading up = UploadUnit.TYPE.born();
    final UploadingContext uc = UploadingContext.create("~/nutz/unit/uploadtmp");
    File[] files = dir.listFiles();
    final MockHttpServletRequest req = request().setInputStream(insmulti("UTF-8", files));
    req.setSession(session(context()));
    req.init();
    Object monLock = new Object();
    int monInterval = 2000;
    UploadMonitor mon = new UploadMonitor(monLock, req.getSession(), out, monInterval);
    Thread monThread = new Thread(mon, "UploadingMonitor");
    monThread.start();
    out.println("Begin...");
    Stopwatch sw = null;
    try {
        sw = Stopwatch.run(new Atom() {

            public void run() {
                try {
                    up.parse(req, uc);
                } catch (UploadException e) {
                    throw Lang.wrapThrow(e);
                }
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        mon.stop();
        out.println("\n...Done!");
        if (null != sw)
            out.println(sw);
    }
}
Also used : MockHttpServletRequest(org.nutz.mock.servlet.MockHttpServletRequest) Stopwatch(org.nutz.lang.Stopwatch) Atom(org.nutz.trans.Atom) File(java.io.File)

Example 3 with Atom

use of org.nutz.trans.Atom in project nutz by nutzam.

the class TransactionTest method testRollback.

@Test
public void testRollback() {
    // In transaction
    try {
        Trans.exec(new Atom() {

            public void run() {
                Company com = Company.create("dtri");
                Master m = Master.create("zzh", com);
                Cat c1 = Cat.create("XiaoBai", m);
                Cat c2 = Cat.create("Tony", m);
                comService.dao().insert(com);
                assertEquals(1, dao.count(Company.class, null));
                m.setComId(com.getId());
                masterService.dao().insert(m);
                assertEquals(1, dao.count(Master.class, null));
                c1.setMasterId(m.getId());
                c2.setMasterId(m.getId());
                catService.dao().insert(c1);
                catService.dao().insert(c2);
                assertEquals(2, dao.count(Cat.class, null));
                throw new RuntimeException("Stop!!!");
            }
        });
        fail();
    } catch (Exception e) {
        assertEquals(0, dao.count(Company.class, null));
        assertEquals(0, dao.count(Master.class, null));
        assertEquals(0, dao.count(Cat.class, null));
        assertEquals(0, Trans.count.get().intValue());
        assertNull(Trans.get());
    }
}
Also used : Atom(org.nutz.trans.Atom) Test(org.junit.Test)

Example 4 with Atom

use of org.nutz.trans.Atom in project nutz by nutzam.

the class FieldFilterTest method test_update_by_filter.

@Test
public void test_update_by_filter() {
    final Pet p = dao.fetch(Pet.class, "xb");
    p.setNickName("XiaoBai");
    FieldFilter.create(Pet.class, "id|name").run(new Atom() {

        public void run() {
            dao.update(p);
        }
    });
    Pet p2 = dao.fetch(Pet.class, p.getId());
    assertNull(p2.getNickName());
}
Also used : Pet(org.nutz.dao.test.meta.Pet) Atom(org.nutz.trans.Atom) Test(org.junit.Test)

Example 5 with Atom

use of org.nutz.trans.Atom in project nutz by nutzam.

the class InsertTest method test_insert_by_fieldfilter.

@Test
public void test_insert_by_fieldfilter() {
    dao.create(Pet.class, true);
    final Pet pet = Pet.create("xb");
    pet.setNickName("xiaobai");
    FieldFilter.create(Pet.class, "^id|name$").run(new Atom() {

        public void run() {
            dao.insert(pet);
        }
    });
    Pet xb = dao.fetch(Pet.class, "xb");
    assertNull(xb.getNickName());
}
Also used : Pet(org.nutz.dao.test.meta.Pet) Atom(org.nutz.trans.Atom) Test(org.junit.Test)

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