" TransactionRequiredException:没有正在进行的交易"即使应用了事务拦截器 - hibernate-5和spring-4.3

时间:2016-10-15 11:12:21

标签: java spring hibernate spring-transactions transactional

即使似乎应用了事务拦截器,我也面临这个TransactionRequiredException:

"Subject" should "throw proper exceptions.." in {
  a[IllegalArgumentException] should be thrownBy {
    val tryValue = // some method call..
    if (tryValue.isFailure) throw tryValue.failed.get
  }
}

以下是我正在使用的内容:

javax.persistence.TransactionRequiredException: no transaction is in progress
at org.hibernate.internal.SessionImpl.checkTransactionNeeded(SessionImpl.java:3393)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1386)
at org.springframework.orm.hibernate5.SessionFactoryUtils.flush(SessionFactoryUtils.java:144)
at org.springframework.orm.hibernate5.SpringSessionSynchronization.beforeCommit(SpringSessionSynchronization.java:95)
at org.springframework.transaction.support.TransactionSynchronizationUtils.triggerBeforeCommit(TransactionSynchronizationUtils.java:95)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.triggerBeforeCommit(AbstractPlatformTransactionManager.java:932)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:744)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:730)
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:487)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:291)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655)
at my.pkg.entity.impl.StoreImpl$$EnhancerBySpringCGLIB$$73283c3f.merge(<generated>)
at my.pkg.controller.LoginController.setClient(LoginController.java:243)
at my.pkg.controller.LoginController.setupUserSession(LoginController.java:185)
at my.pkg.controller.LoginController.login(LoginController.java:121)
at my.pkg.controller.LoginController$$FastClassBySpringCGLIB$$d71b2e05.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:651)
at my.pkg.controller.LoginController$$EnhancerBySpringCGLIB$$442b15b7.login(<generated>)
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:497)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
............

我的调度程序&lt; servlet&gt; -servlet.xml为空,所有spring配置都在applicationContext.xml中:

Hibernate-5.2 (hibernate-entitymanager-5.1, hibernate-commons-annotations-5.0.1)
Spring-4.3.3 - (tried 4.3.2 as well, same error)

db-config.xml具有:

<context:annotation-config />
<context:component-scan base-package="my.pkg.controller, my.pkg.entity"></context:component-scan>
<mvc:annotation-driven enable-matrix-variables="true" content-negotiation-manager="contentNegotiationManager" />
<import resource="db-config.xml" />

我的my.pkg.entity.impl。* Impl类扩展自my.pkg.entity。* Home类(我使用jboss工具自动生成&lt; entity&gt;和&lt; entity&gt; Home类),所有* Impl类(不是* Home类)也使用@Transactional注释。 *家庭用传播表示#34;支持&#34;咨询。 LoginController.setClient()使用@Transactional注释。

我无法弄清楚我做错了什么或错过了什么。 我确实尝试搜索SOF和谷歌,但找不到可以在这里应用的内容。

如果需要任何其他信息,请告诉我。如果我在这里偏离任何标准/首选配置方法,也请告诉我。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

未应用LoginController.setClient的@Transactional注释。因为它是bean内部调用。 你在Stacktrace中看到了这个

at my.pkg.controller.LoginController.setClient(LoginController.java:243)
at my.pkg.controller.LoginController.setupUserSession(LoginController.java:185)
at my.pkg.controller.LoginController.login(LoginController.java:121)

您可以使用登录方法启动它。但我没有足够的细节可以说明。