nutzam / nutz

Nutz -- Web Framework(Mvc/Ioc/Aop/Dao/Json) for ALL Java developer
https://nutzam.com
Apache License 2.0
2.53k stars 942 forks source link

官方教程中dao事务模板运行失败(sqlserver数据库) #826

Closed jackmeng2015 closed 9 years ago

jackmeng2015 commented 9 years ago

代码如下:

public void testTransaction2() {
    final Person person1 = dao.fetch(Person.class, 1);
    final Person person2 = dao.fetch(Person.class, 2);

    person1.setAge(50);
    person2.setName("ABC");

    Trans.DEBUG = true;

    Trans.exec(new Atom() {
        public void run() {
            dao.update(person1);
            dao.update(person2);
        }
    });
}

控制台输出日志如下:

15-3-26 21:21:21.574 WARN [main] !!You are using default SystemLog! Don't use it in Production environment!!
15-3-26 21:21:21.589 INFO [main] Nutz is licensed under the Apache License, Version 2.0 .
Report bugs : https://github.com/nutzam/nutz/issues
15-3-26 21:21:23.322 DEBUG [main] Locations for Scans:
[JarResourceLocation [jarPath=C:\Users\LENOVO\git\nutz_mvc\nutz_mvc\WebContent\WEB-INF\lib\nutz-1.b.53.jar], JarResourceLocation [jarPath=E:\Flex\apache-tomcat-7.0.59\lib\el-api.jar], JarResourceLocation [jarPath=E:\Flex\apache-tomcat-7.0.59\lib\catalina-ant.jar], JarResourceLocation [jarPath=C:\Users\LENOVO\git\nutz_mvc\nutz_mvc\WebContent\WEB-INF\lib\druid-0.2.2.jar], JarResourceLocation [jarPath=E:\Flex\apache-tomcat-7.0.59\lib\tomcat-coyote.jar], JarResourceLocation [jarPath=F:\util\eclipse\plugins\org.hamcrest.core_1.3.0.v201303031735.jar], JarResourceLocation [jarPath=E:\Flex\apache-tomcat-7.0.59\lib\tomcat-api.jar], JarResourceLocation [jarPath=E:\Flex\apache-tomcat-7.0.59\lib\annotations-api.jar], JarResourceLocation [jarPath=E:\Flex\apache-tomcat-7.0.59\lib\tomcat-i18n-fr.jar], FileSystemResourceLocation [root=C:\Users\LENOVO\git\nutz_mvc\nutz_mvc], JarResourceLocation [jarPath=E:\Flex\apache-tomcat-7.0.59\lib\catalina.jar], FileSystemResourceLocation [root=F:\util\eclipse\configuration\org.eclipse.osgi\361\0\.cp], JarResourceLocation [jarPath=E:\Flex\apache-tomcat-7.0.59\lib\jasper-el.jar], JarResourceLocation [jarPath=C:\Users\LENOVO\git\nutz_mvc\nutz_mvc\WebContent\WEB-INF\lib\sqljdbc4.jar], JarResourceLocation [jarPath=E:\Flex\apache-tomcat-7.0.59\lib\ecj-4.4.jar], JarResourceLocation [jarPath=E:\Flex\apache-tomcat-7.0.59\lib\catalina-ha.jar], JarResourceLocation [jarPath=C:/Users/LENOVO/git/nutz_mvc/nutz_mvc/WebContent/WEB-INF/lib/nutz-1.b.53.jar], JarResourceLocation [jarPath=C:\Users\LENOVO\git\nutz_mvc\nutz_mvc\WebContent\WEB-INF\lib\commons-logging-1.1.1.jar], JarResourceLocation [jarPath=F:\util\eclipse\plugins\org.junit_4.11.0.v201303080030\junit.jar], JarResourceLocation [jarPath=E:\Flex\apache-tomcat-7.0.59\lib\tomcat7-websocket.jar], JarResourceLocation [jarPath=E:\Flex\apache-tomcat-7.0.59\lib\tomcat-util.jar], JarResourceLocation [jarPath=E:\Flex\apache-tomcat-7.0.59\lib\jsp-api.jar], FileSystemResourceLocation [root=C:\Users\LENOVO\git\nutz_mvc\nutz_mvc\build\classes], JarResourceLocation [jarPath=E:\Flex\apache-tomcat-7.0.59\lib\jasper.jar], JarResourceLocation [jarPath=E:\Flex\apache-tomcat-7.0.59\lib\tomcat-i18n-es.jar], JarResourceLocation [jarPath=E:\Flex\apache-tomcat-7.0.59\lib\websocket-api.jar], JarResourceLocation [jarPath=E:\Flex\apache-tomcat-7.0.59\lib\tomcat-dbcp.jar], JarResourceLocation [jarPath=E:\Flex\apache-tomcat-7.0.59\lib\tomcat-i18n-ja.jar], JarResourceLocation [jarPath=E:\Flex\apache-tomcat-7.0.59\lib\servlet-api.jar], JarResourceLocation [jarPath=E:\Flex\apache-tomcat-7.0.59\lib\tomcat-jdbc.jar], JarResourceLocation [jarPath=E:\Flex\apache-tomcat-7.0.59\lib\catalina-tribes.jar], FileSystemResourceLocation [root=F:\util\eclipse\configuration\org.eclipse.osgi\362\0\.cp]]
15-3-26 21:21:23.339 DEBUG [main] Found 1 resource by src( ioc/datasource_sqlserver.js ) , regex( ^(.+[.])(js|json)$ )
15-3-26 21:21:23.340 DEBUG [main] loading ioc js config from [datasource_sqlserver.js]
15-3-26 21:21:23.359 DEBUG [main] Loaded 2 bean define from path=[ioc/datasource_sqlserver.js] --> [dataSource, dao]
15-3-26 21:21:23.383 DEBUG [main] Get 'dao'<interface org.nutz.dao.Dao>
15-3-26 21:21:23.386 DEBUG [main]    >> Load definition
15-3-26 21:21:23.391 DEBUG [main] Loading define for name=dao
15-3-26 21:21:23.425 DEBUG [main]    >> Make...'dao'<interface org.nutz.dao.Dao>
15-3-26 21:21:23.465 DEBUG [main] class org.nutz.dao.impl.NutDao , no config to enable AOP for this type.
15-3-26 21:21:23.468 DEBUG [main] Save object 'dao' to [app] 
15-3-26 21:21:23.473 DEBUG [main] Get 'dataSource'<>
15-3-26 21:21:23.473 DEBUG [main]    >> Load definition
15-3-26 21:21:23.474 DEBUG [main] Loading define for name=dataSource
15-3-26 21:21:23.634 DEBUG [main] Using 91 castor for Castors
15-3-26 21:21:23.636 DEBUG [main]    >> Make...'dataSource'<>
15-3-26 21:21:23.679 DEBUG [main] class com.alibaba.druid.pool.DruidDataSource , no config to enable AOP for this type.
15-3-26 21:21:23.679 DEBUG [main] Save object 'dataSource' to [app] 
15-3-26 21:21:23.751 DEBUG [main] Get 'dataSource'<>
15-3-26 21:21:23.860 INFO [main] Init file-pool by: C:\Users\LENOVO/.nutz/tmp/dao/ [200000]
15-3-26 21:21:23.861 DEBUG [main] file-pool.home: 'C:\Users\LENOVO\.nutz\tmp\dao'
15-3-26 21:21:23.864 INFO [main] file-pool.cursor: 0
15-3-26 21:21:23.879 DEBUG [main] Jdbcs init complete
15-3-26 21:21:23.879 INFO [main] Get Connection from DataSource for JdbcExpert
15-3-26 21:21:24.746 DEBUG [main] JDBC Driver --> 4.0.2206.100
15-3-26 21:21:24.747 DEBUG [main] JDBC Name   --> Microsoft JDBC Driver 4.0 for SQL Server
15-3-26 21:21:24.752 DEBUG [main] Database info --> SQLSERVER:[Microsoft SQL Server - 9.00.4035]
15-3-26 21:21:25.60 DEBUG [main] SELECT * FROM t_person  WHERE id=?
    | 1 |
    |---|
    | 1 |
  For example:> "SELECT * FROM t_person  WHERE id=1"
15-3-26 21:21:25.150 DEBUG [main] SELECT * FROM t_person  WHERE id=?
    | 1 |
    |---|
    | 2 |
  For example:> "SELECT * FROM t_person  WHERE id=2"
15-3-26 21:21:25.157 DEBUG [main] Start New Transaction id=0, level=2
15-3-26 21:21:25.168 DEBUG [main] UPDATE t_person SET name=?,age=?  WHERE id=?
    |   1 |  2 | 3 |
    |-----|----|---|
    | ABC | 50 | 1 |
  For example:> "UPDATE t_person SET name='ABC',age=50  WHERE id=1"
15-3-26 21:21:25.172 DEBUG [main] UPDATE t_person SET name=?,age=?  WHERE id=?
    |   1 |  2 | 3 |
    |-----|----|---|
    | ABC | 55 | 2 |
  For example:> "UPDATE t_person SET name='ABC',age=55  WHERE id=2"
15-3-26 21:21:25.181 DEBUG [main] SQLException
com.microsoft.sqlserver.jdbc.SQLServerException: 违反了 UNIQUE KEY 约束 'UQ__t_person__08EA5793'。不能在对象 'dbo.t_person' 中插入重复键。
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:216)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1515)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:404)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:350)
    at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:180)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:155)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.execute(SQLServerPreparedStatement.java:332)
    at com.alibaba.druid.pool.DruidPooledPreparedStatement.execute(DruidPooledPreparedStatement.java:437)
    at org.nutz.dao.impl.sql.run.NutDaoExecutor._runPreparedStatement(NutDaoExecutor.java:266)
    at org.nutz.dao.impl.sql.run.NutDaoExecutor.exec(NutDaoExecutor.java:81)
    at org.nutz.dao.impl.DaoSupport$DaoExec.invoke(DaoSupport.java:250)
    at org.nutz.dao.impl.sql.run.NutDaoRunner.run(NutDaoRunner.java:31)
    at org.nutz.dao.impl.DaoSupport._exec(DaoSupport.java:205)
    at org.nutz.dao.impl.EntityOperator.exec(EntityOperator.java:50)
    at org.nutz.dao.impl.NutDao.update(NutDao.java:237)
    at com.dao.test.DaoTest$2.run(DaoTest.java:181)
    at org.nutz.trans.Trans.exec(Trans.java:165)
    at org.nutz.trans.Trans.exec(Trans.java:123)
    at com.dao.test.DaoTest.testTransaction2(DaoTest.java:178)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
15-3-26 21:21:25.184 DEBUG [main] SQL NextException
15-3-26 21:21:25.187 DEBUG [main] Transaction rollback id=0, count=0
15-3-26 21:21:25.188 DEBUG [main] Transaction depose id=0, count=0
15-3-26 21:21:25.190 DEBUG [main] Depose object 'dataSource' ...
15-3-26 21:21:25.192 DEBUG [main] Depose object 'dao' ...
15-3-26 21:21:25.193 DEBUG [main] !!!Ioc is deposed, you can't use it anymore
wendal commented 9 years ago

我的测试代码

    @Test
    public void test_update_rollback() {
        dao.create(Pet.class, true);
        dao.insert(Pet.create("XiaoBai"));
        dao.insert(Pet.create("XiaoHei"));
        final Pet pet1 = dao.fetch(Pet.class,"XiaoBai");
        final Pet pet2 = dao.fetch(Pet.class,"XiaoHei");

        pet1.setNickName("BaiBai");
        pet2.setNickName("HeiHei");
        // Begin transaction        
        try {
            Trans.exec(new Atom(){
                public void run() {
                    dao.update(pet1);
                    dao.update(pet2);
                    throw Lang.noImplement();
                }
            });
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        // End transaction
        assertNotEquals(dao.fetch(Pet.class, pet1.getId()).getNickName(), pet1.getNickName());
    }

测试环境是win7 x86 及 sqlserver 2005 exp, 驱动版本4.1

日志如下

ALL Nutz Log via Log4jLogAdapter
2015-03-27 20:46:51,912 org.nutz.log.Logs.<clinit>(Logs.java:20) INFO  - Nutz is licensed under the Apache License, Version 2.0 .
Report bugs : https://github.com/nutzam/nutz/issues
2015-03-27 20:46:52,302 org.nutz.resource.Scans.<init>(Scans.java:484) DEBUG - Locations for Scans:
[JarResourceLocation [jarPath=H:\strato_work\Nutz2\build\deps\jetty\org.apache.taglibs.standard.glassfish_1.2.0.v201004190952.jar], JarResourceLocation [jarPath=H:\strato_work\Nutz2\build\deps\oracle_ojdbc6.jar], JarResourceLocation [jarPath=H:\strato_work\Nutz2\build\deps\jetty\javax.el_2.1.0.v201004190952.jar], FileSystemResourceLocation [root=H:\strato_work\Nutz2], JarResourceLocation [jarPath=H:\strato_work\Nutz2\build\deps\jetty\servlet-api-2.5.jar], JarResourceLocation [jarPath=H:\strato_work\Nutz2\build\deps\jetty\javax.servlet.jsp_2.1.0.v201004190952.jar], JarResourceLocation [jarPath=H:\strato_work\Nutz2\build\deps\jetty\com.sun.el_1.0.0.v201004190952.jar], JarResourceLocation [jarPath=H:\strato_work\Nutz2\build\deps\commons-dbcp-1.4.jar], JarResourceLocation [jarPath=H:\strato_work\Nutz2\build\deps\jetty\javax.servlet.jsp.jstl_1.2.0.v201004190952.jar], JarResourceLocation [jarPath=H:\strato_work\Nutz2\build\deps\commons-logging-1.1.1.jar], JarResourceLocation [jarPath=H:\strato_work\Nutz2\build\deps\commons-pool-1.5.4.jar], FileSystemResourceLocation [root=H:\eclipse\configuration\org.eclipse.osgi\204\0\.cp], JarResourceLocation [jarPath=H:\strato_work\Nutz2\build\deps\ecj-3.8.1.jar], JarResourceLocation [jarPath=H:\strato_work\Nutz2\build\deps\cache-annotations-ri-common-1.0.0.jar], JarResourceLocation [jarPath=H:\strato_work\Nutz2\build\deps\sqljdbc41.jar], JarResourceLocation [jarPath=H:\strato_work\Nutz2\build\deps\jetty\jetty-all-server-7.6.4.v20120524.jar], JarResourceLocation [jarPath=H:\strato_work\Nutz2\build\deps\h2-1.3.154.jar], JarResourceLocation [jarPath=H:\strato_work\Nutz2\build\deps\cache-api-1.0.0.jar], JarResourceLocation [jarPath=H:\strato_work\Nutz2\build\deps\cache-ri-impl-1.0.0.jar], JarResourceLocation [jarPath=H:\eclipse\plugins\org.junit_4.11.0.v201303080030\junit.jar], JarResourceLocation [jarPath=H:\strato_work\Nutz2\build\deps\jetty\org.apache.jasper.glassfish_2.1.0.v201007080150.jar], JarResourceLocation [jarPath=H:\strato_work\Nutz2\build\deps\mysql-connector-java-5.1.23-bin.jar], JarResourceLocation [jarPath=H:\strato_work\Nutz2\build\deps\log4j-1.2.14.jar], JarResourceLocation [jarPath=H:\eclipse\plugins\org.hamcrest.core_1.3.0.v201303031735.jar], FileSystemResourceLocation [root=H:\eclipse\configuration\org.eclipse.osgi\205\0\.cp], FileSystemResourceLocation [root=H:\strato_work\Nutz2\target\classes]]
2015-03-27 20:46:52,302 org.nutz.resource.Scans.scan(Scans.java:228) DEBUG - Found 1 resource by src( org/nutz/dao/test/meta/pojo.js ) , regex( ^(.+[.])(js|json)$ )
2015-03-27 20:46:52,318 org.nutz.ioc.loader.json.JsonLoader.<init>(JsonLoader.java:44) DEBUG - loading ioc js config from [pojo.js]
2015-03-27 20:46:52,318 org.nutz.ioc.loader.json.JsonLoader.<init>(JsonLoader.java:52) DEBUG - Loaded 5 bean define from path=[org/nutz/dao/test/meta/pojo.js] --> [sqls, metas, dao, config, dataSource]
2015-03-27 20:46:52,318 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:144) DEBUG - Get 'dao'<interface org.nutz.dao.Dao>
2015-03-27 20:46:52,318 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:166) DEBUG -    >> Load definition
2015-03-27 20:46:52,318 org.nutz.ioc.loader.map.MapLoader.load(MapLoader.java:67) DEBUG - Loading define for name=dao
2015-03-27 20:46:52,333 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:193) DEBUG -    >> Make...'dao'<interface org.nutz.dao.Dao>
2015-03-27 20:46:52,349 org.nutz.ioc.aop.impl.DefaultMirrorFactory.getMirror(DefaultMirrorFactory.java:82) DEBUG - class org.nutz.dao.impl.NutDao , no config to enable AOP for this type.
2015-03-27 20:46:52,349 org.nutz.ioc.impl.ScopeContext.save(ScopeContext.java:59) DEBUG - Save object 'dao' to [app] 
2015-03-27 20:46:52,349 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:144) DEBUG - Get 'dataSource'<>
2015-03-27 20:46:52,349 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:166) DEBUG -    >> Load definition
2015-03-27 20:46:52,349 org.nutz.ioc.loader.map.MapLoader.load(MapLoader.java:67) DEBUG - Loading define for name=dataSource
2015-03-27 20:46:52,411 org.nutz.castor.Castors.reload(Castors.java:116) DEBUG - Using 91 castor for Castors
2015-03-27 20:46:52,411 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:193) DEBUG -    >> Make...'dataSource'<>
2015-03-27 20:46:52,411 org.nutz.ioc.aop.impl.DefaultMirrorFactory.getMirror(DefaultMirrorFactory.java:82) DEBUG - class org.apache.commons.dbcp.BasicDataSource , no config to enable AOP for this type.
2015-03-27 20:46:52,411 org.nutz.ioc.impl.ScopeContext.save(ScopeContext.java:59) DEBUG - Save object 'dataSource' to [app] 
2015-03-27 20:46:52,521 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:144) DEBUG - Get 'config'<>
2015-03-27 20:46:52,521 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:166) DEBUG -    >> Load definition
2015-03-27 20:46:52,521 org.nutz.ioc.loader.map.MapLoader.load(MapLoader.java:67) DEBUG - Loading define for name=config
2015-03-27 20:46:52,521 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:193) DEBUG -    >> Make...'config'<>
2015-03-27 20:46:52,521 org.nutz.ioc.aop.impl.DefaultMirrorFactory.getMirror(DefaultMirrorFactory.java:82) DEBUG - class org.nutz.ioc.impl.PropertiesProxy , no config to enable AOP for this type.
2015-03-27 20:46:52,521 org.nutz.ioc.impl.ScopeContext.save(ScopeContext.java:59) DEBUG - Save object 'config' to [app] 
2015-03-27 20:46:52,521 org.nutz.resource.Scans.scan(Scans.java:228) DEBUG - Found 1 resource by src( nutz-test.properties ) , regex( ^.+[.]properties$ )
2015-03-27 20:46:52,536 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:144) DEBUG - Get 'config'<>
2015-03-27 20:46:52,536 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:144) DEBUG - Get 'config'<>
2015-03-27 20:46:52,536 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:144) DEBUG - Get 'config'<>
2015-03-27 20:46:52,536 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:144) DEBUG - Get 'sqls'<>
2015-03-27 20:46:52,536 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:166) DEBUG -    >> Load definition
2015-03-27 20:46:52,536 org.nutz.ioc.loader.map.MapLoader.load(MapLoader.java:67) DEBUG - Loading define for name=sqls
2015-03-27 20:46:52,536 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:193) DEBUG -    >> Make...'sqls'<>
2015-03-27 20:46:52,536 org.nutz.ioc.aop.impl.DefaultMirrorFactory.getMirror(DefaultMirrorFactory.java:82) DEBUG - class org.nutz.dao.impl.FileSqlManager , no config to enable AOP for this type.
2015-03-27 20:46:52,536 org.nutz.ioc.impl.ScopeContext.save(ScopeContext.java:59) DEBUG - Save object 'sqls' to [app] 
2015-03-27 20:46:52,536 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:144) DEBUG - Get 'dataSource'<>
2015-03-27 20:46:52,536 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:144) DEBUG - Get 'sqls'<>
2015-03-27 20:46:52,583 org.nutz.filepool.NutFilePool.<init>(NutFilePool.java:23) INFO  - Init file-pool by: C:\Users\wendal/.nutz/tmp/dao/ [200000]
2015-03-27 20:46:52,583 org.nutz.filepool.NutFilePool.<init>(NutFilePool.java:37) DEBUG - file-pool.home: 'C:\Users\wendal\.nutz\tmp\dao'
2015-03-27 20:46:52,583 org.nutz.filepool.NutFilePool.<init>(NutFilePool.java:66) INFO  - file-pool.cursor: 120
2015-03-27 20:46:52,583 org.nutz.dao.jdbc.Jdbcs.<clinit>(Jdbcs.java:85) DEBUG - Jdbcs init complete
2015-03-27 20:46:52,583 org.nutz.dao.jdbc.Jdbcs.getExpert(Jdbcs.java:98) INFO  - Get Connection from DataSource for JdbcExpert
2015-03-27 20:46:52,895 org.nutz.dao.impl.DaoSupport$1.invoke(DaoSupport.java:165) DEBUG - JDBC Driver --> 4.1.5605.100
2015-03-27 20:46:52,895 org.nutz.dao.impl.DaoSupport$1.invoke(DaoSupport.java:166) DEBUG - JDBC Name   --> Microsoft JDBC Driver 4.1 for SQL Server
2015-03-27 20:46:52,911 org.nutz.dao.impl.DaoSupport.setDataSource(DaoSupport.java:174) DEBUG - Database info --> SQLSERVER:[Microsoft SQL Server - 9.00.1399]
2015-03-27 20:46:52,911 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:144) DEBUG - Get 'metas'<class org.nutz.dao.test.meta.Pojos>
2015-03-27 20:46:52,911 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:166) DEBUG -    >> Load definition
2015-03-27 20:46:52,911 org.nutz.ioc.loader.map.MapLoader.load(MapLoader.java:67) DEBUG - Loading define for name=metas
2015-03-27 20:46:52,911 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:193) DEBUG -    >> Make...'metas'<class org.nutz.dao.test.meta.Pojos>
2015-03-27 20:46:52,911 org.nutz.ioc.aop.impl.DefaultMirrorFactory.getMirror(DefaultMirrorFactory.java:82) DEBUG - class org.nutz.dao.test.meta.Pojos , no config to enable AOP for this type.
2015-03-27 20:46:52,911 org.nutz.ioc.impl.ScopeContext.save(ScopeContext.java:59) DEBUG - Save object 'metas' to [app] 
2015-03-27 20:46:52,911 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:144) DEBUG - Get 'dao'<>
2015-03-27 20:46:52,911 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:144) DEBUG - Get 'dao'<>
2015-03-27 20:46:52,990 org.nutz.dao.impl.sql.run.NutDaoExecutor._runStatement(NutDaoExecutor.java:313) DEBUG - DROP TABLE trans_company
2015-03-27 20:46:53,005 org.nutz.dao.impl.jdbc.AbstractJdbcExpert.setupEntityField(AbstractJdbcExpert.java:102) DEBUG - Table 'trans_company' doesn't exist!
2015-03-27 20:46:53,005 org.nutz.dao.impl.sql.run.NutDaoExecutor._runStatement(NutDaoExecutor.java:313) DEBUG - CREATE TABLE trans_company(
id NUMERIC(8) IDENTITY,
name VARCHAR(50) UNIQUE NOT NULL,
PRIMARY KEY (id)
)
2015-03-27 20:46:53,005 org.nutz.dao.impl.sql.run.NutDaoExecutor._runStatement(NutDaoExecutor.java:313) DEBUG - DROP TABLE trans_master
2015-03-27 20:46:53,021 org.nutz.dao.impl.jdbc.AbstractJdbcExpert.setupEntityField(AbstractJdbcExpert.java:102) DEBUG - Table 'trans_master' doesn't exist!
2015-03-27 20:46:53,021 org.nutz.dao.impl.sql.run.NutDaoExecutor._runStatement(NutDaoExecutor.java:313) DEBUG - CREATE TABLE trans_master(
id NUMERIC(8) IDENTITY,
name VARCHAR(50) UNIQUE NOT NULL,
comId NUMERIC(8),
PRIMARY KEY (id)
)
2015-03-27 20:46:53,036 org.nutz.dao.impl.sql.run.NutDaoExecutor._runStatement(NutDaoExecutor.java:313) DEBUG - DROP TABLE trans_cat
2015-03-27 20:46:53,036 org.nutz.dao.impl.jdbc.AbstractJdbcExpert.setupEntityField(AbstractJdbcExpert.java:102) DEBUG - Table 'trans_cat' doesn't exist!
2015-03-27 20:46:53,036 org.nutz.dao.impl.sql.run.NutDaoExecutor._runStatement(NutDaoExecutor.java:313) DEBUG - CREATE TABLE trans_cat(
id NUMERIC(8) IDENTITY,
name VARCHAR(50) UNIQUE NOT NULL,
masterId NUMERIC(8),
PRIMARY KEY (id)
)
2015-03-27 20:46:53,036 org.nutz.dao.impl.sql.run.NutDaoExecutor._runPreparedStatement(NutDaoExecutor.java:255) DEBUG - INSERT INTO trans_cat(name,masterId) VALUES(?,?) 
    |  1 | 2 |
    |----|---|
    | xb | 0 |
  For example:> "INSERT INTO trans_cat(name,masterId) VALUES('xb',0) "
2015-03-27 20:46:53,068 org.nutz.dao.impl.sql.run.NutDaoExecutor._runSelect(NutDaoExecutor.java:193) DEBUG - SELECT @@IDENTITY as id
2015-03-27 20:46:53,068 org.nutz.dao.impl.sql.run.NutDaoExecutor._runStatement(NutDaoExecutor.java:313) DEBUG - DROP TABLE trans_company
2015-03-27 20:46:53,068 org.nutz.dao.impl.jdbc.AbstractJdbcExpert.setupEntityField(AbstractJdbcExpert.java:102) DEBUG - Table 'trans_company' doesn't exist!
2015-03-27 20:46:53,068 org.nutz.dao.impl.sql.run.NutDaoExecutor._runStatement(NutDaoExecutor.java:313) DEBUG - CREATE TABLE trans_company(
id NUMERIC(8) IDENTITY,
name VARCHAR(50) UNIQUE NOT NULL,
PRIMARY KEY (id)
)
2015-03-27 20:46:53,068 org.nutz.dao.impl.sql.run.NutDaoExecutor._runStatement(NutDaoExecutor.java:313) DEBUG - DROP TABLE trans_master
2015-03-27 20:46:53,083 org.nutz.dao.impl.jdbc.AbstractJdbcExpert.setupEntityField(AbstractJdbcExpert.java:102) DEBUG - Table 'trans_master' doesn't exist!
2015-03-27 20:46:53,083 org.nutz.dao.impl.sql.run.NutDaoExecutor._runStatement(NutDaoExecutor.java:313) DEBUG - CREATE TABLE trans_master(
id NUMERIC(8) IDENTITY,
name VARCHAR(50) UNIQUE NOT NULL,
comId NUMERIC(8),
PRIMARY KEY (id)
)
2015-03-27 20:46:53,083 org.nutz.dao.impl.sql.run.NutDaoExecutor._runStatement(NutDaoExecutor.java:313) DEBUG - DROP TABLE trans_cat
2015-03-27 20:46:53,083 org.nutz.dao.impl.jdbc.AbstractJdbcExpert.setupEntityField(AbstractJdbcExpert.java:102) DEBUG - Table 'trans_cat' doesn't exist!
2015-03-27 20:46:53,083 org.nutz.dao.impl.sql.run.NutDaoExecutor._runStatement(NutDaoExecutor.java:313) DEBUG - CREATE TABLE trans_cat(
id NUMERIC(8) IDENTITY,
name VARCHAR(50) UNIQUE NOT NULL,
masterId NUMERIC(8),
PRIMARY KEY (id)
)
2015-03-27 20:46:53,083 org.nutz.dao.impl.sql.run.NutDaoExecutor._runPreparedStatement(NutDaoExecutor.java:255) DEBUG - INSERT INTO trans_cat(name,masterId) VALUES(?,?) 
    |  1 | 2 |
    |----|---|
    | xb | 0 |
  For example:> "INSERT INTO trans_cat(name,masterId) VALUES('xb',0) "
2015-03-27 20:46:53,083 org.nutz.dao.impl.sql.run.NutDaoExecutor._runSelect(NutDaoExecutor.java:193) DEBUG - SELECT @@IDENTITY as id
2015-03-27 20:46:53,099 org.nutz.dao.impl.sql.run.NutDaoExecutor._runStatement(NutDaoExecutor.java:313) DEBUG - DROP TABLE t_pet
2015-03-27 20:46:53,099 org.nutz.dao.impl.jdbc.AbstractJdbcExpert.setupEntityField(AbstractJdbcExpert.java:102) DEBUG - Table 't_pet' doesn't exist!
2015-03-27 20:46:53,099 org.nutz.dao.impl.sql.run.NutDaoExecutor._runStatement(NutDaoExecutor.java:313) DEBUG - CREATE TABLE t_pet(
id NUMERIC(8) IDENTITY,
name VARCHAR(50) UNIQUE NOT NULL,
alias VARCHAR(50),
age NUMERIC(8),
mas NUMERIC(8),
birthday DATETIME,
price float,
PRIMARY KEY (id)
)
2015-03-27 20:46:53,114 org.nutz.dao.impl.sql.run.NutDaoExecutor._runStatement(NutDaoExecutor.java:313) DEBUG - Create UNIQUE Index t_pet_name_masterId ON t_pet(name,mas)
2015-03-27 20:46:53,114 org.nutz.dao.impl.sql.run.NutDaoExecutor._runStatement(NutDaoExecutor.java:313) DEBUG - Create Index t_pet_name_age ON t_pet(name,age)
2015-03-27 20:46:53,114 org.nutz.dao.impl.sql.run.NutDaoExecutor._runPreparedStatement(NutDaoExecutor.java:255) DEBUG - INSERT INTO t_pet(name,alias,age,mas,birthday,price) VALUES(?,?,?,?,?,?) 
    |       1 |         2 | 3 | 4 |                   5 |   6 |
    |---------|-----------|---|---|---------------------|-----|
    | XiaoBai | NAFNKNUet | 6 | 0 | 2015-03-27 20:46:53 | 0.0 |
  For example:> "INSERT INTO t_pet(name,alias,age,mas,birthday,price) VALUES('XiaoBai','NAFNKNUet',6,0,'2015-03-27 20:46:53',0.0) "
2015-03-27 20:46:53,114 org.nutz.dao.impl.sql.run.NutDaoExecutor._runSelect(NutDaoExecutor.java:193) DEBUG - SELECT @@IDENTITY as id
2015-03-27 20:46:53,114 org.nutz.dao.impl.sql.run.NutDaoExecutor._runPreparedStatement(NutDaoExecutor.java:255) DEBUG - INSERT INTO t_pet(name,alias,age,mas,birthday,price) VALUES(?,?,?,?,?,?) 
    |       1 |     2 | 3 | 4 |                   5 |   6 |
    |---------|-------|---|---|---------------------|-----|
    | XiaoHei | LosVV | 5 | 0 | 2015-03-27 20:46:53 | 0.0 |
  For example:> "INSERT INTO t_pet(name,alias,age,mas,birthday,price) VALUES('XiaoHei','LosVV',5,0,'2015-03-27 20:46:53',0.0) "
2015-03-27 20:46:53,130 org.nutz.dao.impl.sql.run.NutDaoExecutor._runSelect(NutDaoExecutor.java:193) DEBUG - SELECT @@IDENTITY as id
2015-03-27 20:46:53,130 org.nutz.dao.impl.sql.run.NutDaoExecutor._runSelect(NutDaoExecutor.java:212) DEBUG - SELECT * FROM t_pet  WHERE name=?
    |       1 |
    |---------|
    | XiaoBai |
  For example:> "SELECT * FROM t_pet  WHERE name='XiaoBai'"
2015-03-27 20:46:53,130 org.nutz.dao.impl.sql.run.NutDaoExecutor._runSelect(NutDaoExecutor.java:212) DEBUG - SELECT * FROM t_pet  WHERE name=?
    |       1 |
    |---------|
    | XiaoHei |
  For example:> "SELECT * FROM t_pet  WHERE name='XiaoHei'"
2015-03-27 20:46:53,130 org.nutz.trans.Trans._begain(Trans.java:58) DEBUG - Start New Transaction id=0, level=2
2015-03-27 20:46:53,130 org.nutz.dao.impl.sql.run.NutDaoExecutor._runPreparedStatement(NutDaoExecutor.java:255) DEBUG - UPDATE t_pet SET name=?,alias=?,age=?,mas=?,birthday=?,price=?  WHERE id=?
    |       1 |      2 | 3 | 4 |                   5 |   6 | 7 |
    |---------|--------|---|---|---------------------|-----|---|
    | XiaoBai | BaiBai | 6 | 0 | 2015-03-27 20:46:53 | 0.0 | 1 |
  For example:> "UPDATE t_pet SET name='XiaoBai',alias='BaiBai',age=6,mas=0,birthday='2015-03-27 20:46:53',price=0.0  WHERE id=1"
java.lang.RuntimeException: Not implement yet!
2015-03-27 20:46:53,146 org.nutz.dao.impl.sql.run.NutDaoExecutor._runPreparedStatement(NutDaoExecutor.java:255) DEBUG - UPDATE t_pet SET name=?,alias=?,age=?,mas=?,birthday=?,price=?  WHERE id=?
    |       1 |      2 | 3 | 4 |                   5 |   6 | 7 |
    |---------|--------|---|---|---------------------|-----|---|
    | XiaoHei | HeiHei | 5 | 0 | 2015-03-27 20:46:53 | 0.0 | 2 |
  For example:> "UPDATE t_pet SET name='XiaoHei',alias='HeiHei',age=5,mas=0,birthday='2015-03-27 20:46:53',price=0.0  WHERE id=2"
2015-03-27 20:46:53,146 org.nutz.trans.Trans._rollback(Trans.java:102) DEBUG - Transaction rollback id=0, count=0
2015-03-27 20:46:53,146 org.nutz.trans.Trans._depose(Trans.java:87) DEBUG - Transaction depose id=0, count=0
2015-03-27 20:46:53,146 org.nutz.dao.impl.sql.run.NutDaoExecutor._runSelect(NutDaoExecutor.java:212) DEBUG - SELECT * FROM t_pet  WHERE id=?
    | 1 |
    |---|
    | 1 |
  For example:> "SELECT * FROM t_pet  WHERE id=1"
    at org.nutz.lang.Lang.noImplement(Lang.java:77)
    at org.nutz.trans.SimpleTransTest$1.run(SimpleTransTest.java:44)
    at org.nutz.trans.Trans.exec(Trans.java:165)
    at org.nutz.trans.Trans.exec(Trans.java:123)
    at org.nutz.trans.SimpleTransTest.test_update_rollback(SimpleTransTest.java:40)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
wendal commented 9 years ago

我的测试结果是pass

wendal commented 9 years ago

附上Pet类,取自于test目录

package org.nutz.dao.test.meta;

import java.sql.Timestamp;

import org.nutz.dao.DB;
import org.nutz.dao.entity.annotation.*;
import org.nutz.lang.Strings;
import org.nutz.lang.random.R;
import org.nutz.lang.random.StringGenerator;

@Table("t_pet")
@TableIndexes({@Index(name = "t_pet_name_masterId", unique = true,
                       fields = {"name", "masterId"}),
               @Index(name = "t_pet_name_age", fields = {"name", "age"},
                       unique = false)})
public class Pet {

    private static StringGenerator sg = R.sg(5, 10);

    public static Pet create(String name) {
        Pet pet = new Pet();
        pet.setName(name);
        pet.setAge(R.random(3, 10));
        pet.setNickName(sg.next());
        pet.setBirthday(new Timestamp(System.currentTimeMillis()));
        return pet;
    }

    public static Pet[] create(int num) {
        Pet[] pets = new Pet[num];
        for (int i = 0; i < num; i++)
            pets[i] = create("pet_" + Strings.fillHex(i, 2));
        return pets;
    }

    @Id
    @Next({@SQL(db = DB.PSQL, value = "SELECT currval('$table$_id_seq')")})
    private int id;

    @Name
    private String name;

    @Column("alias")
    private String nickName;

    @Column
    private int age;

    @Column("mas")
    private int masterId;

    @Column
    private Timestamp birthday;

    @Column
    private float price;

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }

    public int getId() {
        return id;
    }

    public Pet setId(int id) {
        this.id = id;
        return this;
    }

    public int getMasterId() {
        return masterId;
    }

    public void setMasterId(int masterId) {
        this.masterId = masterId;
    }

    public String getName() {
        return name;
    }

    public Pet setName(String name) {
        this.name = name;
        return this;
    }

    public String getNickName() {
        return nickName;
    }

    public Pet setNickName(String nickName) {
        this.nickName = nickName;
        return this;
    }

    public int getAge() {
        return age;
    }

    public Pet setAge(int age) {
        this.age = age;
        return this;
    }

    public Timestamp getBirthday() {
        return birthday;
    }

    public void setBirthday(Timestamp birthday) {
        this.birthday = birthday;
    }

    public String toString() {
        return name;
    }
}
jackmeng2015 commented 9 years ago

我试过你的代码了,成功执行了dao.update(person1);和dao.update(person2);之后再执行throw Lang.noImplement();是可以整个事务回滚的,都是如果“dao.update(person2);// 违反了 UNIQUE KEY 约束,抛出异常”本身SQL出问题了,是没办法让第一句dao.update(person1);也回滚

jackmeng2015 commented 9 years ago
@Test
public void testTransaction2() {
    final Person person1 = dao.fetch(Person.class, 1);
    final Person person2 = dao.fetch(Person.class, 2);

    person1.setAge(100);
    person2.setAge(100);

// person2.setName("ABC");

    Trans.DEBUG = true;

    try{
        Trans.exec(new Atom() {
            public void run() {
                dao.update(person1);
                dao.update(person2);// 违反了 UNIQUE KEY 约束,抛出异常
                throw Lang.noImplement();
            }
        });
    }catch(Exception e){
        e.printStackTrace();
    }
}
wendal commented 9 years ago

最后结论是druid 0.2.2 的bug