在jodd框架中使用@transaction注释时如何回滚事务?

时间:2015-08-07 15:47:55

标签: java jodd

代码是这样的:

import static jodd.jtx.JtxPropagationBehavior.PROPAGATION_SUPPORTS;

@PetiteBean
public class FooService {

    @PetiteInject
    AppDao appDao;

    @Transaction(propagation = PROPAGATION_SUPPORTS, readOnly = false)
    public void storeUser(User user) {
        appDao.store(user);
    }

    @Transaction(propagation = PROPAGATION_SUPPORTS)
    public User findUserById(long id) {
        return appDao.findById(User.class, Long.valueOf(id));
    }
    @Transaction(propagation = PROPAGATION_SUPPORTS)
    public void updateDetails(User user) {
        appDao.updateUserName(user); //line no 18
        appDao.updateAddress(user); //line no 19
    }

}

如果第18行成功执行并且第19行执行存在异常,那么由于第18行引起的更改应该是回滚。我怎么能这样做。?

已更改::

@Transaction
public void updateDetails(User user) throws DatabaseException {
    try {
        appDao.updateUserName(user); //line no 18
        appDao.updateAddress(user); //line no 19
    } catch (DatabaseException e) {
        e.printStackTrace();
        System.out.println("Throwing Exception" );
        throw new DatabaseException(e.getMessage());
    }
}

实际代码::

@Transaction
public void updateCategory(CategoryData categoryData) throws DatabaseException {
    try {
        Category category = DataConvertionUtil.prepareCategory(categoryData);
        userDao.updateCategory(category);
        userDao.updateCategory1(category);
    } catch (DatabaseException e) {
        e.printStackTrace();
        System.out.println("Throwing Exception" );
        throw new DatabaseException(e.getMessage());
    }
}

Appcore.java

public class AppCore {

    public void start() {
        // AppUtil.resolveDirs();
        initLogger();
        initProxetta();
        initPetite();
        initDb();

        // init everything else
    }

    private void initLogger() {
        LoggerFactory.setLoggerFactory(new SimpleLoggerFactory(Level.DEBUG));
    }

    public void stop() {
        // close everything
        petite.shutdown();
    }

    JtxTransactionManager jtxManager;
    ConnectionProvider connectionProvider;

    protected PetiteContainer petite;
    protected boolean isWebApplication;

    ProxyProxetta proxetta;

    void initPetite() {
        petite = new ProxettaAwarePetiteContainer(proxetta);
        if (isWebApplication == false) {
            petite.registerScope(SessionScope.class, new ProtoScope());
        }
        AutomagicPetiteConfigurator pcfg = new AutomagicPetiteConfigurator();
        pcfg.setIncludedEntries(this.getClass().getPackage().getName() + ".*");
        pcfg.configure(petite);

        // load parameters
        Props appProps = new Props();
        appProps.loadSystemProperties("sys");
        appProps.loadEnvironment("env");
        PropsUtil.loadFromClasspath(appProps, "/app*.prop*");

        petite.defineParameters(appProps);

        // add appCore to Petite (and resolve parameters)
        petite.addBean("app", this);

    }

    void initDb() {

        DbOomManager dbOomManager = DbOomManager.getInstance();

        // manual configuration (before entities registration)
        TableNamingStrategy tns = new TableNamingStrategy();
        tns.setPrefix("");
        tns.setUppercase(false);
        dbOomManager.setTableNames(tns);

        ColumnNamingStrategy cns = new ColumnNamingStrategy();
        cns.setUppercase(false);
        dbOomManager.setColumnNames(cns);

        // automatic configuration
        AutomagicDbOomConfigurator dbcfg = new AutomagicDbOomConfigurator();
        dbcfg.setIncludedEntries(this.getClass().getPackage().getName() + ".*");
        dbcfg.configure(dbOomManager);

        petite.registerPetiteBean(CoreConnectionPool.class, "dbpool", null, null, false);
        connectionProvider = (ConnectionProvider) petite.getBean("dbpool");
        connectionProvider.init();

        // transactions
        jtxManager = new DbJtxTransactionManager(connectionProvider);
        jtxManager.setValidateExistingTransaction(true);
        AnnotationTxAdviceSupport.manager = new AnnotationTxAdviceManager(jtxManager, "$class");
        DbSessionProvider sessionProvider = new DbJtxSessionProvider(jtxManager);

        // global settings
        DbManager dbManager = DbManager.getInstance();
        // dbManager.setDebug(true); for loading each time query
        dbManager.setDebug(true);
        dbManager.setConnectionProvider(connectionProvider);
        dbManager.setSessionProvider(sessionProvider);

        petite.addBean("dbManager", dbManager);
    }

    void initProxetta() {
        @SuppressWarnings("unchecked")
        ProxyAspect txServiceProxy = new ProxyAspect(AnnotationTxAdvice.class, new MethodAnnotationPointcut(Transaction.class) {
            @Override
            public boolean apply(MethodInfo mi) {
                return isPublic(mi) && isTopLevelMethod(mi) && matchClassName(mi, "*Service") && super.apply(mi);
            }
        });
        proxetta = ProxyProxetta.withAspects(txServiceProxy);
        proxetta.setClassLoader(this.getClass().getClassLoader());
    }

    public PetiteContainer getPetite() {
        return petite;
    }

    void stopDb() {
        jtxManager.close();
        connectionProvider.close();
    }
}

Debug Log ::

-----> /category.update.html   [com.kirtan.jodd.action.category.ManageCategoryAction#update]
22307 [INFO] c.k.j.a.c.ManageCategoryAction.update:151 - ManageCategoryAction.update()CategoryData [categoryId=5, name=SOA2, parentCategoryName=null, parentCategoryId=1, createdDate=null]
22307 [DEBUG] j.j.JtxTransactionManager.requestTransaction:250 - Requesting TX jtx{Supports,readonly,Default,-1}
22307 [DEBUG] j.j.JtxTransaction.<init>:72 - New JTX {status:No transaction, mode:jtx{Supports,readonly,Default,-1}}
22307 [INFO] c.k.j.d.QueryBuilder.generateSql:39 - SQL String :: update TBLMCATEGORY set  parentCategoryId = 1 , name = 'SOA2' where ( categoryId = 5 ) ; 
22307 [INFO] c.k.j.d.QueryBuilder.generateSql:42 - Time Taken for Generating SQL String :: 0
22307 [DEBUG] j.d.j.DbJtxSessionProvider.getDbSession:33 - Requesting db TX manager session
22307 [DEBUG] j.d.DbSession.<init>:45 - Creating new db session
22307 [DEBUG] j.d.p.CoreConnectionPool.getConnection:224 - Returning valid pooled connection
22307 [DEBUG] j.d.DbQueryBase.executeUpdate:660 - Executing update: update TBLMCATEGORY set  parentCategoryId = 1 , name = 'SOA2' where ( categoryId = 5 ) ; 
22307 [DEBUG] j.d.DbQueryBase.executeUpdate:685 - execution time: 0ms
22307 [INFO] c.k.j.d.QueryBuilder.generateSql:39 - SQL String :: update TBLMCATEGORY set  categoryId = 1 , parentCategoryId = 1 , name = 'SOA2' where ( categoryId = 5 ) ; 
22307 [INFO] c.k.j.d.QueryBuilder.generateSql:42 - Time Taken for Generating SQL String :: 0
22307 [DEBUG] j.d.j.DbJtxSessionProvider.getDbSession:33 - Requesting db TX manager session
22307 [DEBUG] j.d.DbQueryBase.executeUpdate:660 - Executing update: update TBLMCATEGORY set  categoryId = 1 , parentCategoryId = 1 , name = 'SOA2' where ( categoryId = 5 ) ; 
jodd.db.DbSqlException: Query execution failed
Query: update TBLMCATEGORY set  categoryId = 1 , parentCategoryId = 1 , name = 'SOA2' where ( categoryId = 5 ) ; ; <--- com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '1' for key 'PRIMARY'
    at jodd.db.DbQueryBase.executeUpdate(DbQueryBase.java:677)
    at jodd.db.DbQueryBase.executeUpdate(DbQueryBase.java:647)
    at jodd.db.DbQuery.executeUpdate(DbQuery.java:49)
    at com.kirtan.jodd.dao.AppDao.executeUpdate(AppDao.java:85)
    at com.kirtan.jodd.dao.UserDao.updateCategory1(UserDao.java:67)
    at com.kirtan.jodd.service.UserService.updateCategory(UserService.java:131)
    at com.kirtan.jodd.service.UserService$$Proxetta.updateCategory$0(Unknown Source)
    at com.kirtan.jodd.service.UserService$$Proxetta.updateCategory(Unknown Source)
    at com.kirtan.jodd.action.category.ManageCategoryAction.update(ManageCategoryAction.java:152)
    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 jodd.madvoc.ActionRequest.invokeActionMethod(ActionRequest.java:332)
    at jodd.madvoc.ActionRequest$2.invoke(ActionRequest.java:218)
    at jodd.madvoc.ActionRequest.invoke(ActionRequest.java:322)
    at jodd.madvoc.interceptor.EchoInterceptor.intercept(EchoInterceptor.java:43)
    at jodd.madvoc.interceptor.BaseActionInterceptor.invoke(BaseActionInterceptor.java:15)
    at jodd.madvoc.ActionRequest.invoke(ActionRequest.java:322)
    at jodd.madvoc.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:48)
    at jodd.madvoc.interceptor.BaseActionInterceptor.invoke(BaseActionInterceptor.java:15)
    at jodd.madvoc.ActionRequest.invoke(ActionRequest.java:322)
    at jodd.joy.i18n.I18nInterceptor.intercept(I18nInterceptor.java:22)
    at jodd.madvoc.interceptor.BaseActionInterceptor.invoke(BaseActionInterceptor.java:15)
    at jodd.madvoc.ActionRequest.invoke(ActionRequest.java:322)
    at jodd.madvoc.ActionRequest$1.invoke(ActionRequest.java:199)
    at jodd.madvoc.ActionRequest.invoke(ActionRequest.java:322)
    at com.kirtan.jodd.filter.SessionFilter.filter(SessionFilter.java:35)
    at jodd.madvoc.filter.BaseActionFilter.invoke(BaseActionFilter.java:15)
    at jodd.madvoc.ActionRequest.invoke(ActionRequest.java:322)
    at jodd.madvoc.component.MadvocController.invoke(MadvocController.java:141)
    at jodd.madvoc.MadvocServletFilter.doFilter(MadvocServletFilter.java:88)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:620)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Unknown Source)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '1' for key 'PRIMARY'
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.Util.getInstance(Util.java:386)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1040)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4096)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4028)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2490)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2728)
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1811)
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1725)
    at jodd.db.DbQueryBase.executeUpdate(DbQueryBase.java:671)
    ... 43 more
---[cause]------------------------------------------------------------------------
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '1' for key 'PRIMARY'
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.Util.getInstance(Util.java:386)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1040)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4096)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4028)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2490)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2728)
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1811)
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1725)
    at jodd.db.DbQueryBase.executeUpdate(DbQueryBase.java:671)
    at jodd.db.DbQueryBase.executeUpdate(DbQueryBase.java:647)
    at jodd.db.DbQuery.executeUpdate(DbQuery.java:49)
    at com.kirtan.jodd.dao.AppDao.executeUpdate(AppDao.java:85)
    at com.kirtan.jodd.dao.UserDao.updateCategory1(UserDao.java:67)
    at com.kirtan.jodd.service.UserService.updateCategory(UserService.java:131)
    at com.kirtan.jodd.service.UserService$$Proxetta.updateCategory$0(Unknown Source)
    at com.kirtan.jodd.service.UserService$$Proxetta.updateCategory(Unknown Source)
    at com.kirtan.jodd.action.category.ManageCategoryAction.update(ManageCategoryAction.java:152)
    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 jodd.madvoc.ActionRequest.invokeActionMethod(ActionRequest.java:332)
    at jodd.madvoc.ActionRequest$2.invoke(ActionRequest.java:218)
    at jodd.madvoc.ActionRequest.invoke(ActionRequest.java:322)
    at jodd.madvoc.interceptor.EchoInterceptor.intercept(EchoInterceptor.java:43)
    at jodd.madvoc.interceptor.BaseActionInterceptor.invoke(BaseActionInterceptor.java:15)
    at jodd.madvoc.ActionRequest.invoke(ActionRequest.java:322)
    at jodd.madvoc.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:48)
    at jodd.madvoc.interceptor.BaseActionInterceptor.invoke(BaseActionInterceptor.java:15)
    at jodd.madvoc.ActionRequest.invoke(ActionRequest.java:322)
    at jodd.joy.i18n.I18nInterceptor.intercept(I18nInterceptor.java:22)
    at jodd.madvoc.interceptor.BaseActionInterceptor.invoke(BaseActionInterceptor.java:15)
    at jodd.madvoc.ActionRequest.invoke(ActionRequest.java:322)
    at jodd.madvoc.ActionRequest$1.invoke(ActionRequest.java:199)
    at jodd.madvoc.ActionRequest.invoke(ActionRequest.java:322)
    at com.kirtan.jodd.filter.SessionFilter.filter(SessionFilter.java:35)
    at jodd.madvoc.filter.BaseActionFilter.invoke(BaseActionFilter.java:15)
    at jodd.madvoc.ActionRequest.invoke(ActionRequest.java:322)
    at jodd.madvoc.component.MadvocController.invoke(MadvocController.java:141)
    at jodd.madvoc.MadvocServletFilter.doFilter(MadvocServletFilter.java:88)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:620)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Unknown Source)
com.kirtan.jodd.exception.DatabaseException: Query execution failed
Query: update TBLMCATEGORY set  categoryId = 1 , parentCategoryId = 1 , name = 'SOA2' where ( categoryId = 5 ) ; ; <--- com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '1' for key 'PRIMARY'
    at com.kirtan.jodd.dao.AppDao.executeUpdate(AppDao.java:88)
    at com.kirtan.jodd.dao.UserDao.updateCategory1(UserDao.java:67)
    at com.kirtan.jodd.service.UserService.updateCategory(UserService.java:131)
    at com.kirtan.jodd.service.UserService$$Proxetta.updateCategory$0(Unknown Source)
    at com.kirtan.jodd.service.UserService$$Proxetta.updateCategory(Unknown Source)
    at com.kirtan.jodd.action.category.ManageCategoryAction.update(ManageCategoryAction.java:152)
    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 jodd.madvoc.ActionRequest.invokeActionMethod(ActionRequest.java:332)
    at jodd.madvoc.ActionRequest$2.invoke(ActionRequest.java:218)
    at jodd.madvoc.ActionRequest.invoke(ActionRequest.java:322)
    at jodd.madvoc.interceptor.EchoInterceptor.intercept(EchoInterceptor.java:43)
    at jodd.madvoc.interceptor.BaseActionInterceptor.invoke(BaseActionInterceptor.java:15)
    at jodd.madvoc.ActionRequest.invoke(ActionRequest.java:322)
    at jodd.madvoc.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:48)
    at jodd.madvoc.interceptor.BaseActionInterceptor.invoke(BaseActionInterceptor.java:15)
    at jodd.madvoc.ActionRequest.invoke(ActionRequest.java:322)
    at jodd.joy.i18n.I18nInterceptor.intercept(I18nInterceptor.java:22)
    at jodd.madvoc.interceptor.BaseActionInterceptor.invoke(BaseActionInterceptor.java:15)
    at jodd.madvoc.ActionRequest.invoke(ActionRequest.java:322)
    at jodd.madvoc.ActionRequest$1.invoke(ActionRequest.java:199)
    at jodd.madvoc.ActionRequest.invoke(ActionRequest.java:322)
    at com.kirtan.jodd.filter.SessionFilter.filter(SessionFilter.java:35)
    at jodd.madvoc.filter.BaseActionFilter.invoke(BaseActionFilter.java:15)
    at jodd.madvoc.ActionRequest.invoke(ActionRequest.java:322)
    at jodd.madvoc.component.MadvocController.invoke(MadvocController.java:141)
    at jodd.madvoc.MadvocServletFilter.doFilter(MadvocServletFilter.java:88)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:620)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Unknown Source)
Throwing Exception
22338 [DEBUG] j.j.w.LeanJtxWorker.markOrRollbackTransaction:92 - rollback tx
22338 [DEBUG] j.j.JtxTransaction.commitOrRollback:227 - Rollback JTX
22338 [DEBUG] j.d.DbSession.closeSession:67 - Closing db session
com.kirtan.jodd.exception.DatabaseException: Query execution failed
Query: update TBLMCATEGORY set  categoryId = 1 , parentCategoryId = 1 , name = 'SOA2' where ( categoryId = 5 ) ; ; <--- com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '1' for key 'PRIMARY'
    at com.kirtan.jodd.service.UserService.updateCategory(UserService.java:135)
    at com.kirtan.jodd.service.UserService$$Proxetta.updateCategory$0(Unknown Source)
    at com.kirtan.jodd.service.UserService$$Proxetta.updateCategory(Unknown Source)
    at com.kirtan.jodd.action.category.ManageCategoryAction.update(ManageCategoryAction.java:152)
    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 jodd.madvoc.ActionRequest.invokeActionMethod(ActionRequest.java:332)
    at jodd.madvoc.ActionRequest$2.invoke(ActionRequest.java:218)
    at jodd.madvoc.ActionRequest.invoke(ActionRequest.java:322)
    at jodd.madvoc.interceptor.EchoInterceptor.intercept(EchoInterceptor.java:43)
    at jodd.madvoc.interceptor.BaseActionInterceptor.invoke(BaseActionInterceptor.java:15)
    at jodd.madvoc.ActionRequest.invoke(ActionRequest.java:322)
    at jodd.madvoc.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:48)
    at jodd.madvoc.interceptor.BaseActionInterceptor.invoke(BaseActionInterceptor.java:15)
    at jodd.madvoc.ActionRequest.invoke(ActionRequest.java:322)
    at jodd.joy.i18n.I18nInterceptor.intercept(I18nInterceptor.java:22)
    at jodd.madvoc.interceptor.BaseActionInterceptor.invoke(BaseActionInterceptor.java:15)
    at jodd.madvoc.ActionRequest.invoke(ActionRequest.java:322)
    at jodd.madvoc.ActionRequest$1.invoke(ActionRequest.java:199)
    at jodd.madvoc.ActionRequest.invoke(ActionRequest.java:322)
    at com.kirtan.jodd.filter.SessionFilter.filter(SessionFilter.java:35)
    at jodd.madvoc.filter.BaseActionFilter.invoke(BaseActionFilter.java:15)
    at jodd.madvoc.ActionRequest.invoke(ActionRequest.java:322)
    at jodd.madvoc.component.MadvocController.invoke(MadvocController.java:141)
    at jodd.madvoc.MadvocServletFilter.doFilter(MadvocServletFilter.java:88)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:620)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Unknown Source)
<----- /category.update.html  () in 31ms.

我正在使用mysql数据库。请帮帮我...

1 个答案:

答案 0 :(得分:1)

抛出异常。抛出异常时,JTX代理将捕获它并标记事务以进行回滚。

此行为在AnnotationTxAdvice类中定义,虽然这不是您应该非常关心的事情:)

修改

快速概述告诉我发生异常是因为您有主键的重复条目:

Duplicate entry '1' for key 'PRIMARY'

这是预期的行为。现在,你问了这个:

  

如果第18行成功执行并且第19行执行存在异常,那么由于第18行引起的更改应该是回滚。我怎么能这样做?

这就是这里发生的事情:

@Transaction(propagation = PROPAGATION_SUPPORTS)
public void updateDetails(User user) {
    appDao.updateUserName(user); //line no 18
    appDao.updateAddress(user); //line no 19
}

如果您使用PROPAGATION_SUPPORTS,那么如果没有启动任何事务,代码将在非事务性上下文中运行。这意味着,如果第19行抛出异常,第18行将不会回滚(假设事务未在之前启动)。

如果您希望第18行和第19行都在一个事务中,则应将传播更改为默认值(即PROPAGATION_REQUIRED),如果尚未启动,则会创建新事务。仅使用该更改,如果第19行引发异常,则第18行也将回滚。

所以只需改变传播:)