请帮助我有关Spring JPA事务管理 我有两种方法:usersService.addUser和AuthorityService.addNew
@Service
@Transactional(propagation = PROPAGATION.SUPPORTS, readOnly=true)
public class UsersService {
@Autowired
UsersRepository usersRepository;
@Autowired
AuthotitiesRepository authoritiesReposotory;
@Transaction
public addUser(...){
usersRespository.addUser...
authoritiesRepository.addNew...
}
...
public interface UsersRepository extends JpaRepository<Users, String> {
@Transactional
@Modifying
@Query(value = "insert into users..."
}
public interface AuthoritiesRepository extends JpaRepository<Users, String> {
@Transactional
@Modifying
@Query(value = "insert into abc ...."
}
问题是当AuthorityRepository.addNew抛出异常(通过某些SQL语法)时,我想自动回滚用户信息。
如何在存储库,服务中进行配置?
以下是异常跟踪:
2019-05-22 08:33:16.185警告2276 --- [nio-8080-exec-4] o.h.engine.jdbc.spi.SqlExceptionHelper:SQL错误:42102,SQLState:42S02 2019-05-22 08:33:16.185错误2276 --- [nio-8080-exec-4] o.h.engine.jdbc.spi.SqlExceptionHelper:找不到表“ AUTHORITIESS”; SQL语句: 插入授权(用户名,授权)值(?,?)[42102-199] 2019-05-22 08:33:16.220错误2276 --- [nio-8080-exec-4] o.s.t.i.TransactionInterceptor:应用程序异常被提交异常覆盖
com.springboot.example.exception.DbCRUDException:添加用户时出错 在com.springboot.example.security.service.UsersService.addUser(UsersService.java:141)〜[classes /:na] 在com.springboot.example.security.service.UsersService $$ FastClassBySpringCGLIB $$ 3d37fb4e.invoke()〜[classes /:na] 在org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)[spring-core-5.1.6.RELEASE.jar:5.1.6.RELEASE] 在org.springframework.aop.framework.CglibAopProxy $ CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:749)[spring-aop-5.1.6.RELEASE.jar:5.1.6.RELEASE] 在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)[spring-aop-5.1.6.RELEASE.jar:5.1.6.RELEASE] 在org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)〜[spring-tx-5.1.6.RELEASE.jar:5.1.6.RELEASE] 在org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)上[tomcat-embed-core-9.0.17.jar:9.0.17] 在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)[na:1.8.0_162] 在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:624)[na:1.8.0_162] 在org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)[tomcat-embed-core-9.0.17.jar:9.0.17] 在java.lang.Thread.run(Thread.java:748)[na:1.8.0_162] 引起原因:com.springboot.example.exception.DbCRUDException:添加权限时出错 在com.springboot.example.security.service.AuthoritiesService.addNew(AuthoritiesService.java:58)〜[classes /:na] 在com.springboot.example.security.service.UsersService.addUser(UsersService.java:139)〜[classes /:na] ...省略了114个通用框架 由以下原因引起:org.springframework.dao.InvalidDataAccessResourceUsageException:无法准备语句; SQL [插入到授权机构(用户名,授权机构)值(?,?)];嵌套的异常是org.hibernate.exception.SQLGrammarException:无法准备语句 在org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:279)〜[spring-orm-5.1.6.RELEASE.jar:5.1.6.RELEASE] 在org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:253)〜[spring-orm-5.1.6.RELEASE.jar:5.1.6.RELEASE] 在org.springframework.data.jpa.repository.query.JpaQueryExecution $ ModifyingExecution.doExecute(JpaQueryExecution.java:256)〜[spring-data-jpa-2.1.6.RELEASE.jar:2.1.6.RELEASE] 在org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:91)〜[spring-data-jpa-2.1.6.RELEASE.jar:2.1.6.RELEASE] 在org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:136)〜[spring-data-jpa-2.1.6.RELEASE.jar:2.1.6.RELEASE] 在org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:125)〜[spring-data-jpa-2.1.6.RELEASE.jar:2.1.6.RELEASE] 在org.springframework.data.repository.core.support.RepositoryFactorySupport $ QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:605)〜[spring-data-commons-2.1.6.RELEASE.jar:2.1.6.RELEASE] 在org.springframework.data.repository.core.support.RepositoryFactorySupport $ QueryExecutorMethodInterceptor.lambda $ invoke $ 3(RepositoryFactorySupport.java:595)〜[sprin 在org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)[spring-tx-5.1.6.RELEASE.jar:5.1.6.RELEASE] 在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)[spring-aop-5.1.6.RELEASE.jar:5.1.6.RELEASE] 在org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)〜[spring-tx-5.1.6.RELEASE.jar:5.1.6.RELEASE] ...省略了134个通用框架 由以下原因引起:org.h2.jdbc.JdbcSQLSyntaxErrorException:未找到表“ AUTHORITIESS”; SQL语句: 插入权限(用户名,权限)值(?,?) [42102-199] 在org.h2.message.DbException.getJdbcSQLException(DbException.java:451)〜[h2-1.4.199.jar:1.4.199] 在org.h2.message.DbException.getJdbcSQLException(DbException.java:427)〜[h2-1.4.199.jar:1.4.199] 在org.h2.message.DbException.get(DbException.java:205)〜[h2-1.4.199.jar:1.4.199] 2019-05-22 08:33:16.226错误2276-[nio-8080-exec-4] oaccC [。[。[/]。[dispatcherServlet]:Servlet [dispatcherServlet]的Servlet.service()路径[]引发异常[请求处理失败;嵌套异常是org.springframework.transaction.UnexpectedRollbackException:事务无提示回滚,因为它已被标记为“仅回滚”,其根本原因是
org.springframework.transaction.UnexpectedRollbackException:事务静默回滚,因为它已被标记为仅回滚 在org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:755)〜[spring-tx-5.1.6.RELEASE.jar:
答案 0 :(得分:0)
正如@thanhngo所提到的。这是怎么做的。
您已经在addUser
方法上使用了正确的注释,任何RuntimeException
都将导致事务被回滚。
确保usersRespository.addUser...
和authoritiesReposotory.addNew...
也具有@Transactional
注释