2012-10-24 86 views
1

我現在的目標是使事務管理在我的web應用程序使用Hibernate 4.Spring MVC的3休眠4一體化

首先,我的web.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 

    <!-- The definition of the Root Spring Container shared by all Servlets 
     and Filters --> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
      /WEB-INF/spring/root-context.xml, 
      /WEB-INF/spring/appServlet/*-context.xml 
     </param-value> 
    </context-param> 


    <!-- Creates the Spring Container shared by all Servlets and Filters --> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <listener> 
     <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class> 
    </listener> 

    <!-- Processes application requests --> 
    <servlet> 
     <servlet-name>mvc-dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>mvc-dispatcher</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

    <!-- Spring Security --> 
    <filter> 
     <filter-name>springSecurityFilterChain</filter-name> 
     <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
    </filter> 

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

    <session-config> 
     <session-timeout>5</session-timeout> 
    </session-config> 
</web-app> 

MVC-調度員的servlet .XML(從servlet的context.xml中重命名):

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="sec://www.springframework.org/schema/mvc" 
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    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/security http://www.springframework.org/schema/security/spring-security-3.1.xsd"> 

    <!-- DispatcherServlet Context: defines this servlet's request-processing 
     infrastructure --> 

    <context:component-scan base-package="com.moose.springmvc.controller" /> 

    <!-- Enables the Spring MVC @Controller programming model --> 
    <mvc:annotation-driven /> 

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving 
     up static resources in the ${webappRoot}/resources directory --> 
    <mvc:resources mapping="/resources/**" location="resources/" /> 

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources 
     in the /WEB-INF/views directory --> 
    <beans:bean 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <beans:property name="prefix" value="/WEB-INF/views/" /> 
     <beans:property name="suffix" value=".jsp" /> 
    </beans:bean> 

    <mvc:interceptors> 
     <beans:bean 
      class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"> 
      <beans:property name="paramName" value="lang" /> 
     </beans:bean> 
    </mvc:interceptors> 
</beans:beans> 

我的數據庫-context.xml中(從彈簧database.xml重命名)配置:

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> 

    <context:component-scan base-package="com.moose.springmvc.service,com.moose.springmvc.dao" /> 

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

    <bean id="transactionManager" 
     class="org.springframework.orm.hibernate4.HibernateTransactionManager" 
     p:sessionFactory-ref="sessionFactory" /> 

    <bean id="sessionFactory" 
     class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" 
     p:packagesToScan="com.moose.springmvc.entity" p:dataSource-ref="dataSource" 
     p:configLocation="classpath:hibernate.cfg.xml"> 
    </bean> 

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 
     <property name="driverClass" value="com.mysql.jdbc.Driver" /> 
     <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/crud" /> 
     <property name="user" value="root" /> 
     <property name="password" value="[email protected]#" /> 
     <!-- TODO adjust pooling parameters... --> 
    </bean> 
</beans> 

我的DAO類:

@Repository 
public class UserDAO 
{ 
    @Autowired 
    private SessionFactory sessionFactory; 

    ... 

    public int save(User user) 
    { 
     return (Integer) sessionFactory.getCurrentSession().save(user); 
    } 

    ... 
} 

我的服務類:

@Controller 
public class AjaxBootstrapController { 
    @Autowired 
    private UserService userService; 

     .... 

    @RequestMapping(value = "/userAjaxCustomTag", method = RequestMethod.POST) 
    public String processFormAjax(
      @ModelAttribute(value = "user") @Valid User user, 
      BindingResult result) { 
     if (!result.hasErrors()) { 
      userService.createUser(user); 
     } 
     return "userForm"; 
    } 

編輯

0:

@Service 
@Transactional 
public class UserService { 

    protected static Logger logger = LoggerFactory 
      .getLogger(UserService.class); 

    @Autowired 
    private UserDAO userDAO; 


    public Integer createUser(User user){ 

     return userDAO.save(user); 
    } 
} 

爲AjaxBootstrapController類相關代碼

hibernate.cfg.xml的配置:

<?xml version='1.0' encoding='utf-8'?> 

<!DOCTYPE hibernate-configuration PUBLIC 
     "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
     "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 

<hibernate-configuration> 
    <session-factory> 
     <!-- Database connection settings --> 
     <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 
     <property name="connection.url">jdbc:mysql://localhost:3306/crud</property> 
     <property name="connection.username">root</property> 
     <property name="connection.password">[email protected]#</property> 

     <!-- JDBC connection pool (use the built-in) --> 
     <property name="connection.pool_size">1</property> 

     <!-- SQL dialect --> 
     <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 

     <!-- Disable the second-level cache --> 
     <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> 

     <!-- Echo all executed SQL to stdout --> 
     <property name="show_sql">true</property> 

     <!-- Drop and re-create the database schema on startup 
     <property name="hbm2ddl.auto">create</property> 
     --> 
    </session-factory> 
</hibernate-configuration> 

我得到新的異常:

org.hibernate.HibernateException: save is not valid without active transaction 
    at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:348) 
    at $Proxy31.save(Unknown Source) 
    at com.moose.springmvc.dao.UserDAO.save(UserDAO.java:45) 
    at com.moose.springmvc.service.UserService.createUser(UserService.java:23) 
    at com.moose.springmvc.service.UserService$$FastClassByCGLIB$$60239454.invoke(<generated>) 
    at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191) 
    at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:689) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) 
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) 
    at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:622) 
    at com.moose.springmvc.service.UserService$$EnhancerByCGLIB$$419f68a0.createUser(<generated>) 
    at com.moose.springmvc.controller.AjaxBootstrapController.processFormAjax(AjaxBootstrapController.java:60) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 

這有什麼錯配置或註解?

+0

它看起來有點像'@ Transactional'類'UserService'的註解不考慮在內。你可以發佈相關的代碼'AjaxBootstrapController'嗎? – Ralph

+0

我已經添加了AjaxBootstrapController的相關代碼。謝謝 – abiieez

+0

控制器和所有注射看起來不錯。我認爲Adrian Shum是對的。 – Ralph

回答

2

從您的堆棧跟蹤,似乎有在你userService實例,這是非常不正常的,因爲應該是圍繞它的代理這是事務處理方面沒有代理(假設你正在使用的加載時間編織,這是AOP在Spring中最常用的方法)

不知道這是你的問題,但我最近遇到了類似的問題,這個問題竟然是相當棘手。

我假設你正在使用Spring

的組件掃描功能看來你是用SpringMVC使用,存在由調度員的servlet加載另一個應用程序上下文文件。(通常名爲YOUR_SERVLET_NAME-servlet.xml)

Spring正在將它作爲一個獨立的子應用程序上下文加載。

如果在該Servlet應用程序上下文中已經聲明瞭組件掃描,它將加載一個單獨的UserService實例,並且如果該應用程序上下文中沒有任何事務設置,則此UserService實例將不會生成相應的代理(用於tranasaction控制)。

在這種情況下,要解決的方法很簡單,只要確保在servlet應用程序上下文中,限制組件掃描以不掃描已在主應用程序上下文中存在的bean。

+0

我在我的spring-datatabase.xml中更改了packagesToScan。此包不包含在我的servlet-context.xml的組件掃描中 – abiieez

+0

@abiieez:首先創建主應用上下文(由spring-database.xml創建),而servlet應用上下文是主要應用上下文的子節點。主應用程序上下文中的方面不會影響在子項中創建的bean。由於您正在servlet應用程序上下文中掃描bean,因此您的服務不會有與事務有關的方面。我的建議是,在主應用程序ctx中加載DAO,服務等,並讓MVC控制器在servlet應用程序ctx中掃描。或者,在servlet應用上下文中聲明 xml –

+0

我已經更新了我的配置。現在我得到新的異常:沒有活動事務,保存無效。 – abiieez

0

我有一些建議,你可以檢查他們 -

是否使用CGLIB生成代理的服務使用交易?否則,您的服務應該實現一個接口,以便創建使用該接口創建代理的jdk代理。

另外指定在您的發射機如下:註解驅動的標籤 -

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

可以在彈簧database.xml配置Hibernate的配置文件中的SessionFactory的bean。有了這個,你可以看到SQL日誌,如果你已經配置顯示-SQL標誌爲真,以更好地瞭解問題

<bean id="sessionFactory" 
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" 
    p:packagesToScan="com.moose.springmvc.dao" p:dataSource-ref="dataSource" 
    p:configLocation="specify-path-location-of-hibernate.cfg.xml e.g. /WEB-INF/hibernate/hibernate.cfg.xml"> 
</bean> 

請這些檢查。

+0

我剛在我的類路徑中添加了CGLIB。現在也出現新的異常。 – abiieez