开发者

Cannot get @Transactional to work properly in Spring 3

UPDATE: Sorry I seemed to have forgot some things...see bottom of the post for dispatcher-servlet.xml as well as the beans that call the @Repository:

I can't seem to get this to work for some reason. I keep getting the following exception on a em.flush() call:

here is the exception:

2011-08-29 00:29:32,142 DEBUG [org.springframework.web.servlet.DispatcherServlet] - <DispatcherServlet with name 'dispatcher' processing GET request for [/orphanware.com/]>
2011-08-29 00:29:32,142 DEBUG [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - <Mapping [/] to HandlerExecutionChain with handler [com.orphanware.web.controller.HomeController@55ca6954] and 2 interceptors>
2011-08-29 00:29:32,143 DEBUG [org.springframework.web.servlet.DispatcherServlet] - <Last-Modified value for [/orphanware.com/] is: -1>
2011-08-29 00:29:32,143 DEBUG [org.springframework.web.bind.annotation.support.HandlerMethodInvoker] - <Invoking request handler method: public java.lang.String com.orphanware.web.controller.HomeController.index(java.util.Map)>
2011-08-29 00:29:32,144 DEBUG [org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler] - <Creating new EntityManager for shared EntityManager invocation>
2011-08-29 00:29:32,144 DEBUG [org.hibernate.impl.SessionImpl] - <opened session at timestamp: 13146029721>
2011-08-29 00:29:32,144 DEBUG [org.hibernate.event.def.AbstractSaveEventListener] - <generated identifier: 111, using strategy: org.hibernate.id.SequenceHiLoGenerator>
2011-08-29 00:29:32,145 DEBUG [org.springframework.orm.jpa.EntityManagerFactoryUtils] - <Closing JPA EntityManager>
2011-08-29 00:29:32,145 DEBUG [org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler] - <Creating new EntityManager for shared EntityManager invocation>
2011-08-29 00:29:32,146 DEBUG [org.hibernate.impl.SessionImpl] - <opened session at timestamp: 13146029721>
2011-08-29 00:29:32,146 DEBUG [org.springframework.orm.jpa.EntityManagerFactoryUtils] - <Closing JPA EntityManager>
2011-08-29 00:29:32,147 DEBUG [org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver] - <Resolving exception from handler [com.orphanware.web.controller.HomeController@55ca6954]: javax.persistence.TransactionRequiredException: no transaction is in progress>
2011-08-29 00:29:32,147 DEBUG [org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver] - <Resolving exception from handler [com.orphanware.web.controller.HomeController@55ca6954]: javax.persistence.TransactionRequiredException: no transaction is in progress>
2011-08-29 00:29:32,147 DEBUG [org.sprin开发者_StackOverflow中文版gframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver] - <Resolving exception from handler [com.orphanware.web.controller.HomeController@55ca6954]: javax.persistence.TransactionRequiredException: no transaction is in progress>
2011-08-29 00:29:32,148 DEBUG [org.springframework.web.servlet.DispatcherServlet] - <Could not complete request>
javax.persistence.TransactionRequiredException: no transaction is in progress
    at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:301)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:365)
    at $Proxy15.flush(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240)
    at $Proxy15.flush(Unknown Source)
    at com.orphanware.web.dao.SampleDao.saveSample(SampleDao.java:22)
    at com.orphanware.web.services.SampleService.saveSample(SampleService.java:23)
    at com.orphanware.web.controller.HomeController.index(HomeController.java:24)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.sitemesh.webapp.contentfilter.ContentBufferingFilter.bufferAndPostProcess(ContentBufferingFilter.java:169)
    at org.sitemesh.webapp.contentfilter.ContentBufferingFilter.doFilter(ContentBufferingFilter.java:126)
    at org.sitemesh.config.ConfigurableSiteMeshFilter.doFilter(ConfigurableSiteMeshFilter.java:157)
    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:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:662)
Aug 29, 2011 12:29:32 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet dispatcher threw exception
javax.persistence.TransactionRequiredException: no transaction is in progress
    at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:301)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:365)
    at $Proxy15.flush(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240)
    at $Proxy15.flush(Unknown Source)
    at com.orphanware.web.dao.SampleDao.saveSample(SampleDao.java:22)
    at com.orphanware.web.services.SampleService.saveSample(SampleService.java:23)
    at com.orphanware.web.controller.HomeController.index(HomeController.java:24)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.sitemesh.webapp.contentfilter.ContentBufferingFilter.bufferAndPostProcess(ContentBufferingFilter.java:169)
    at org.sitemesh.webapp.contentfilter.ContentBufferingFilter.doFilter(ContentBufferingFilter.java:126)
    at org.sitemesh.config.ConfigurableSiteMeshFilter.doFilter(ConfigurableSiteMeshFilter.java:157)
    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:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:662)

applicationContext.xml :

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc
           http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.1.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> 

    <!-- entity manager factory -->

    <bean id="emf"
          class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
         <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
    </bean>

    <!-- jpaVendorAdapter (works in conjunction with the persistence.xml) -->

    <bean id="jpaVendorAdapter"
          class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="database" value="HSQL" />
        <property name="showSql" value="true" />
        <property name="databasePlatform" value="org.hibernate.dialect.HSQLDialect" />
    </bean>


    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

    <tx:annotation-driven 
         transaction-manager="transactionManager" 
         proxy-target-class="false" />

    <bean id="transactionManager" 
          class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="emf" />
    </bean>

</beans>

Here is the repository code:

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.orphanware.web.domain.SampleEntity;

@Repository
@Transactional
public class SampleDao {

    @PersistenceContext(unitName="appPU")
    private EntityManager em;

    public void saveSample(SampleEntity sample) {
        em.persist(sample);
        em.flush();
    }

    public SampleEntity getSample(long id) {
        return em.find(SampleEntity.class, id);
    }
}

Here is the Service that calls this repository:

package com.orphanware.web.services;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.orphanware.web.dao.SampleDao;
import com.orphanware.web.domain.SampleEntity;

@Service
public class SampleService {

    @Autowired
    private SampleDao sampleDao;

    public void saveSample(String name){
        SampleEntity se = new SampleEntity();
        se.setName(name);

        sampleDao.saveSample(se);
    }

    public SampleEntity getSample(long id) {
        return sampleDao.getSample(id);
    }
}

and the controller that calls the service:

package com.orphanware.web.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.orphanware.web.domain.SampleEntity;
import com.orphanware.web.services.SampleService;

@Controller
public class HomeController {

    @Autowired
    private SampleService sampleService;

    @RequestMapping({"/home", "/"})
    public String index(Map<String, Object> model) {
        String[] stuff ={"stuff0", "stuff1", "stuff3"};

        for(String item : stuff) {
        sampleService.saveSample(item);
        }

        SampleEntity firstOne = sampleService.getSample(0);

        model.put("myVar", firstOne.name);
        return "home/index";
    }
}

and finally the dispatcher-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <mvc:resources mapping="/resources/**" location="/resources/" />
    <mvc:annotation-driven />
    <context:component-scan base-package="com.orphanware.web" />
    <context:annotation-config />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

If I don't call flush then I don't get an exception but nothing is persisted. I've tried also putting the @Transactional annotation on the method and also change the propagation, etc....

Any help would be greatly appriciated. Maybe I've just been looking at it for too long


It seems that you forgot to enable component-scan for you packages, try to add the following line to your configuration.

<context:component-scan base-package="com.orphanware" />

If you have simplyfied your example and use in your real code self-invocation, this could be an issue: (from the reference documentation)

In proxy mode (which is the default), only 'external' method calls coming in through the proxy will be intercepted. This means that 'self-invocation', i.e. a method within the target object calling some other method of the target object, won't lead to an actual transaction at runtime even if the invoked method is marked with @Transactional!


Ok turns out that I'm an idiot (like always). The lines:

<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven />
<context:component-scan base-package="com.orphanware.web" />
<context:annotation-config />

don't belong in dispatcher-servlet.xml. stacker was partially correct. Those lines belong in the context in which they are used. In this case within the applicationContext.xml. (I'm actually not sure on the resources one, but the rest I am)


I see your problem clearly because i did have the same problem and i will tell you why. Simply is you have the <context:component-scan base-package="com.orphanware.web" /> to scan your @Controller beans which are at the servlet context ok and on the other hand you have @Service and @Repository beans to scan in your applicationContext.xml but you just don't mentions it at all with a scan tag. That's why you only have to add at your applicationContext.xml the following tags: <context:component-scan base-package="com.orphanware.web.services" /> and <context:component-scan base-package="com.orphanware.web.repositories" /> to scan the packages.

And that's all, my friend!

Good luck!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜