无法让 @Transactional 在 Spring 3 中正常工作

发布于 2024-12-02 00:50:22 字数 17091 浏览 1 评论 0原文

更新:抱歉,我似乎忘记了一些事情......请参阅帖子底部的dispatcher-servlet.xml以及调用@Repository的bean:

由于某种原因,我似乎无法让它工作。我在 em.flush() 调用中不断收到以下异常:

这是异常:

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.springframework.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>

这是存储库代码:

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

这是调用此存储库的服务:

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

以及调用该服务的控制器:

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";
    }
}

最后是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>

如果我不调用刷新,那么我不会收到异常,但不会保留任何内容。我也尝试过在方法上添加 @Transactional 注释并更改传播等...

任何帮助将不胜感激。也许我只是看得太久了

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.springframework.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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

戏蝶舞 2024-12-09 00:50:22

您似乎忘记为您的软件包启用组件扫描,请尝试将以下行添加到您的配置中。

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

如果您简化了示例并在实际代码自调用中使用,这可能是一个问题:
(来自参考文档)

在代理模式(默认)下,只有通过代理传入的“外部”方法调用才会被拦截。这意味着“自调用”,即目标对象中的方法调用目标对象的其他方法,即使调用的方法标有@Transactional,也不会在运行时导致实际事务!

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!

瞳孔里扚悲伤 2024-12-09 00:50:22

好吧,事实证明我是个白痴(像往常一样)。这些行:

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

不属于dispatcher-servlet.xml。堆垛机部分正确。这些行属于它们使用的上下文。在本例中是在 applicationContext.xml 中。 (实际上我不确定资源之一,但其余的我是)

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)

蘑菇王子 2024-12-09 00:50:22

我清楚地看到你的问题,因为我也遇到了同样的问题,我会告诉你原因。简单地说,您有 来扫描位于 servlet 上下文中的 @Controller beans,以及其他的手上有 @Service 和 @Repository bean 可以在 applicationContext.xml 中扫描,但您根本没有用扫描标签提及它。这就是为什么您只需在 applicationContext.xml 中添加以下标签:



扫描包裹。

仅此而已,我的朋友!

祝你好运!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文