2

代码是这样的:

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();
    }
}

调试日志::

-----> /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数据库。请帮我解决这个...

4

1 回答 1

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_SUPPORTSthen 如果没有事务启动,代码将在非事务上下文中运行。这意味着,如果第 19 行抛出异常,第 18 行将不会回滚(假设之前未启动事务)。

如果您希望第 18 行和第 19 行都在一个事务中,您应该将传播更改为默认值(即PROPAGATION_REQUIRED),如果尚未开始,这将创建新事务。仅通过该更改,如果第 19 行抛出异常,第 18 行也将回滚。

所以只需改变传播:)

于 2015-08-08T06:02:24.960 回答