本文介绍了@Transactional在Spring Web MVC中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在Spring MVC中调用EntityManager.persist()时,虽然在方法中添加了 @Transactional ,但它可以在不整合spring mvc :

  HTTP错误500 

访问/ ssshp / addDept / wwewd时出现问题。原因:

没有可用于当前线程的实际事务的EntityManager - 无法可靠地处理'persist'调用
导致:

javax.persistence.TransactionRequiredException:No EntityManager实际交易可用于当前线程 - 无法可靠地处理'坚持'调用
在org.springframework.orm.jpa.SharedEntityManagerCreator $ SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:278)
在com.sun.proxy 。$ Proxy49.persist(Unknown Source)
at cn.ziav.ssshp.facade.SpringFacadeImpl.createDept(SpringFacadeImpl.java:28)
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 org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandle rMethod.java:221)
处org.springframework.web.servlet.mvc.method.annotation org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
。 ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
位于org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:817)
位于org.springframework.web。 servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:731)
在org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
。在org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
处org.springframework org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
。 web.servlet.FrameworkServlet.processReque st(FrameworkServlet.java:968)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:859)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:844)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
在org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1221)
at org .springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:178)
在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
在org.mortbay .jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1212)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:7 7)
。在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)$ B $在org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter B(ServletHandler.java:1212)在org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:121)

在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1212)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:399)
at org .mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty .handler.ContextHandler.handle(ContextHandler.java:766)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:450)
at org.mortbay.jetty.handler.ContextH andlerCollection.handle(ContextHandlerCollection.java:230)
at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)
at org.mortbay.jetty.handler.HandlerWrapper.handle( HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:326)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
at org.mortbay.jetty.HttpConnection $ RequestHandler.headerComplete(HttpConnection.java:928)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:549)
at org.mortbay .jetty.HttpParser.parseAvailable(HttpParser.java:212)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at org.mortbay.io.nio.SelectChannelEndPoint.run (SelectChannelEndPoint.java:410)
at org.mortbay.thread.QueuedThreadPool $ PoolThread.run(QueuedThreadPool.java:582)

SpringFacadeImpl.java:

  @RestController 
public c lass SpringFacadeImpl实现SpringFacade {
@PersistenceContext
私有EntityManager entityManager;
$ b $覆盖
@RequestMapping(/ addDept / {dept})
@Transactional
public void createDept(@PathVariable String dept){
Department.valueOf(DEPT);
entityManager.persist(dept);


dispatcher-servlet.xml

 <?xml version =1.0encoding =UTF-8?> 
< beans xmlns =http://www.springframework.org/schema/beans
xmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexmlns :mvc =http://www.springframework.org/schema/mvc
xmlns:p =http://www.springframework.org/schema/pxmlns:context =http:// www.springframework.org/schema/context
xsi:schemaLocation =http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc- 4.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework .org / schema / context http://www.springframework.org/schema/context/spring-context-4.2.xsd\">
< context:component-scan base-package =cn.ziav.ssshp。*/>
< mvc:annotation-driven />
< mvc:default-servlet-handler />
< bean id =viewResolver
class =org.springframework.web.servlet.view.InternalResourceViewResolver
p:prefix =/p:suffix =。html/ >

< / beans>





 <?xml version =1.0encoding =UTF-8?> 
< web-app xmlns =http://xmlns.jcp.org/xml/ns/javaeexmlns:xsi =http://www.w3.org/2001/XMLSchema-instance
xsi:schemaLocation =http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd
version =3.1>
< context-param>
< param-name> contextConfigLocation< / param-name>
< param-value> classpath *:applicationContext.xml< / param-value>
< / context-param>
< servlet>
< servlet-name>调度程序< / servlet-name>
< servlet-class> org.springframework.web.servlet.DispatcherServlet< / servlet-class>
1< / load-on-startup>
< / servlet>
< servlet-mapping>
< servlet-name>调度程序< / servlet-name>
< url-pattern> /< / url-pattern>
< / servlet-mapping>
< listener>
< listener-class> org.springframework.web.context.ContextLoaderListener< / listener-class>
< / listener>
< filter>
< filter-name> httpPutFormFilter< / filter-name>
< filter-class> org.springframework.web.filter.HttpPutFormContentFilter< / filter-class>
< / filter>
< filter-mapping>
< filter-name> httpPutFormFilter< / filter-name>
< servlet-name> dispatcherServlet< / servlet-name>
< / filter-mapping>
< filter>
< filter-name> CharacterEncodingFilter< / filter-name>
< filter-class> org.springframework.web.filter.CharacterEncodingFilter< / filter-class>
< init-param>
< / param-name>编码< / param-name>
< param-value> UTF-8< /参数值>
< / init-param>
< / filter>

< filter-mapping>
< filter-name> CharacterEncodingFilter< / filter-name>
< url-pattern> / *< / url-pattern>
< / filter-mapping>
< filter>
< filter-name> HiddenHttpMethodFilter< / filter-name>
< filter-class> org.springframework.web.filter.HiddenHttpMethodFilter< / filter-class>
< / filter>

< filter-mapping>
< filter-name> HiddenHttpMethodFilter< / filter-name>
< url-pattern> / *< / url-pattern>
< / filter-mapping>

< filter>
< filter-name> OpenEntityManagerInViewFilter< / filter-name>
< filter-class> org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter< / filter-class>
< / filter>

< filter-mapping>
< filter-name> OpenEntityManagerInViewFilter< / filter-name>
< url-pattern> / *< / url-pattern>
< / filter-mapping>
< welcome-file-list>
< welcome-file> index.html< / welcome-file>
< / welcome-file-list>
< / web-app>

applicationContext.xml

 <?xml version =1.0encoding =UTF-8?> 
< beans xmlns =http://www.springframework.org/schema/beans
xmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexmlns :context =http://www.springframework.org/schema/context
xmlns:tx =http://www.springframework.org/schema/txxmlns:p =http:// www.springframework.org/schema/p
xmlns:jpa =http://www.springframework.org/schema/data/jpaxmlns:mvc =http://www.springframework.org/ schema / mvc
xsi:schemaLocation =http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/ schema / data / jpa / spring-jpa-1.8.xsd
http://www.springframework.org/schema/tx http://www.spring framework.org/schema/tx/spring-tx-4.2.xsd\">
< context:component-scan base-package =cn.ziav.ssshp。*/>
< context:property-placeholder location =classpath *:db.properties/>
< bean id =dataSourceclass =com.mchange.v2.c3p0.ComboPooledDataSource
p:user =$ {user}p:password =$ {password}p: driverClass =$ {driver}
p:jdbcUrl =$ {url}p:initialPoolSize =$ {initPoolSize}p:maxPoolSize =$ {maxPoolSize}
p:maxIdleTime =300 p:maxIdleTimeExcessConnections =30>< / bean>

< bean id =entityManagerFactory
class =org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean>
< property name =dataSourceref =dataSource>< / property>
< property name =jpaVendorAdapter>
< bean class =org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter
p:generateDdl =truep:showSql =true>< / bean>
< / property>
< property name =packagesToScanvalue =cn.ziav.ssshp。*>< / property>
< property name =jpaProperties>
<道具>
< prop key =hibernate.cache.region.factory_class> org.hibernate.cache.ehcache.EhCacheRegionFactory
< / prop>
< prop key =hibernate.cache.use_second_level_cache> true< / prop>
< prop key =hibernate.cache.use_query_cache> true< / prop>
<! - < prop key =hibernate.cache.use_minimal_puts> true< / prop> - >
<! - < prop key =hibernate.cache.region.factory_class> org.hibernate.cache.ehcache.EhCacheRegionFactory< / prop>
< prop key =net.sf.ehcache.configurationResourceName> ehcache-hibernate.xml< / prop> - >
< prop key =hibernate.dialect> org.hibernate.dialect.MySQL5InnoDBDialect< / prop>
< prop key =hibernate.show_sql> true< / prop>
< prop key =hibernate.format_sql> true< / prop>
< prop key =hibernate.hbm2ddl.auto> update< / prop>
< /道具>
< / property>
< / bean>

< bean id =transactionManagerclass =org.springframework.orm.jpa.JpaTransactionManager>
< property name =entityManagerFactoryref =entityManagerFactory>< / property>
< / bean>

< tx:注解驱动的事务管理器=transactionManager/>
< / beans>

它困扰了我几天,我不知道如何解决这个问题。我已经通过移动< tx:annotation-driven transaction-manager =transactionManager解决了这个问题。 /> 从applicationContext.xml到dispatcher-servlet.xml,Web应用程序通常可以正常工作。它出现的原因是applicationContext.xml中的< tx:annotation-driven transaction-manager =transactionManager/> 仅用于应用程序上下文,例如。 @服务,@库,@组件。所以您需要将它添加到您的servlet上下文中以使事务可用。


The Exception emerges when I i invoke EntityManager.persist() in spring mvc although @Transactional is added on the method, it could work without integrating spring mvc:

HTTP ERROR 500

Problem accessing /ssshp/addDept/wwewd. Reason:

    No EntityManager with actual transaction available for current thread - cannot reliably process 'persist' call
Caused by:

javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread - cannot reliably process 'persist' call
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:278)
    at com.sun.proxy.$Proxy49.persist(Unknown Source)
    at cn.ziav.ssshp.facade.SpringFacadeImpl.createDept(SpringFacadeImpl.java:28)
    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 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:110)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:817)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:731)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:968)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:859)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:844)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1221)
    at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:178)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1212)
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1212)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:121)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1212)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:399)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:766)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:450)
    at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:230)
    at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:326)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
    at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:928)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:549)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
    at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:410)
    at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)

SpringFacadeImpl.java:

@RestController
public class SpringFacadeImpl implements SpringFacade {
    @PersistenceContext
    private EntityManager entityManager;

    @Override
    @RequestMapping("/addDept/{dept}")
    @Transactional
    public void createDept(@PathVariable String dept) {
        Department.valueOf(dept);
        entityManager.persist(dept);
    }
}

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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
    <context:component-scan base-package="cn.ziav.ssshp.*" />
    <mvc:annotation-driven />
    <mvc:default-servlet-handler />
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:prefix="/" p:suffix=".html" />

</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
         http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    version="3.1">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:applicationContext.xml</param-value>
    </context-param>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- To support HTTP PUT and PATCH requests -->
    <filter>
        <filter-name>httpPutFormFilter</filter-name>
        <filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>httpPutFormFilter</filter-name>
        <servlet-name>dispatcherServlet</servlet-name>
    </filter-mapping>
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <filter>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter>
        <filter-name>OpenEntityManagerInViewFilter</filter-name>
        <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>OpenEntityManagerInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    </welcome-file-list>
</web-app>

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:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.8.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
    <context:component-scan base-package="cn.ziav.ssshp.*" />
    <context:property-placeholder location="classpath*:db.properties" />
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        p:user="${user}" p:password="${password}" p:driverClass="${driver}"
        p:jdbcUrl="${url}" p:initialPoolSize="${initPoolSize}" p:maxPoolSize="${maxPoolSize}"
        p:maxIdleTime="300" p:maxIdleTimeExcessConnections="30"></bean>

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
                p:generateDdl="true" p:showSql="true"></bean>
        </property>
        <property name="packagesToScan" value="cn.ziav.ssshp.*"></property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory
                </prop>
                <prop key="hibernate.cache.use_second_level_cache">true</prop>
                <prop key="hibernate.cache.use_query_cache">true</prop>
                <!-- <prop key="hibernate.cache.use_minimal_puts">true</prop> -->
                <!-- <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
                    <prop key="net.sf.ehcache.configurationResourceName">ehcache-hibernate.xml</prop> -->
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
    </bean>

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

    <tx:annotation-driven transaction-manager="transactionManager" />
</beans>

it has troubled me a couple days, i don't know how to tackle this issue.

解决方案

I have solved this issue by moving <tx:annotation-driven transaction-manager="transactionManager" />which is from applicationContext.xml to dispatcher-servlet.xml, the web application can normally work right now. the reason why it emerges is that <tx:annotation-driven transaction-manager="transactionManager" /> in applicationContext.xml only serve for application context,eg. @Service,@Repository,@Component. so you need to add it into your servlet context to make transaction available.

这篇关于@Transactional在Spring Web MVC中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 06:58