2017-08-04 110 views
1

@Transactional不在春天mvc工作。假設我刪除了 @Transactional註釋數據到達RepositoryClass。 Throwable targetException - org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(Object ...) 我需要將數據到達存儲庫類。@Transactional不能在春天工作mvc

請幫幫我, 謝謝。

ServiceImplClass

@Service("userService") 
public class UserServiceImpl implements UserService{ 

    @Autowired 
    UserRepository userRepository; 

    public String saveUserData(User user,HttpSession session) { 
     return userRepository.saveUserData(user); 
    } 
} 

RepositoryClass:

@Component 
@Transactional 
public class UserRepository { 

@Autowired 
protected SessionFactory sessionFactory; 

public SessionFactory getSessionFactory() { 
    return sessionFactory; 
} 

public void setSessionFactory(SessionFactory sessionFactory) { 
    this.sessionFactory = sessionFactory; 
} 

public String saveUserData(User user) { 

    final Session session = (Session) getSessionFactory(); 
    try { 
     session.beginTransaction(); 
     Query query=session.createQuery("UPDATE User set user_Name =:userName," 
       + "reg_Date =:regDate," 
       + "img_Id=:imgId, emailId =:emailId"); 
     query.setParameter("userName", user.getUserName()); 
     query.setParameter("regDate", user.getRegDate()); 
     query.setParameter("imgId", user.getImgId()); 
     query.setParameter("emailId", user.getEmailId()); 
     session.save(user); 
     session.getTransaction().commit(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 

} 

調度-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:context="http://www.springframework.org/schema/context" 
    xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.2.xsd 
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd 
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd 
        "> 

    <context:annotation-config /> 
    <context:component-scan base-package="com.demo.app" /> 
    <mvc:default-servlet-handler /> 
    <mvc:annotation-driven> 
     <mvc:message-converters register-defaults="true"> 
      <bean class="org.springframework.http.converter.StringHttpMessageConverter"> 
       <property name="supportedMediaTypes" value="text/plain;charset=UTF-8" /> 
      </bean> 
     </mvc:message-converters> 
    </mvc:annotation-driven> 

    <bean id="jspViewResolver" 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="viewClass" 
      value="org.springframework.web.servlet.view.JstlView" /> 
     <property name="prefix" value="/WEB-INF/html/" /> 
     <property name="suffix" value="html" /> 
    </bean> 

    <bean id="dataSource" 
     class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
     <property name="url" value="jdbc:mysql://localhost:3306/UserDB" /> 
     <property name="username" value="root" /> 
     <property name="password" value="password" /> 
    </bean> 

    <bean id="sessionFactory" 
     class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="annotatedClasses"> 
      <list> 
       <value>com.demo.app.model.User</value> 
      </list> 
     </property> 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> 
       <prop key="hibernate.show_sql">true</prop> 
       <prop key="hibernate.hbm2ddl.auto">update</prop> 
      </props> 
     </property> 
    </bean> 

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

    <bean id="transactionManager" 
     class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
     <property name="sessionFactory" ref="sessionFactory" /> 
    </bean> 
    <bean 
     class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> 
     <property name="exceptionMappings"> 
      <props> 
       <prop key="java.lang.Exception">Error</prop> 
      </props> 
     </property> 
    </bean> 
    <bean id="multipartResolver" 
     class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 
     <property name="maxUploadSize" value="2097152" /> 
    </bean> 
</beans> 

回答

3

您正在手動管理您的交易。這是交易管理器的任務。 saveUserData應該是這樣的:

public User saveUserData(User user) { 
    return (User)sessionFactory.getCurrentSession().merge(user); 
} 

就是這樣。 而且您可能需要使用@Transactional而不是存儲庫註釋您的服務。

+0

感謝您的replay.but它不工作得到Throwable java.lang.reflect.InvocationTargetException.getTargetException() – umapathi

0

Your Repository不使用@component註釋。並使用

<jpa:repositories base-package="your.package.put.repository"></jpa:repositories> 

的話,你已經使用

<bean id="transactionManager" 
    class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactory" /> 
</bean> 
+0

而不是組件我使用的存儲庫,但沒有解決 – umapathi

+0

你能解釋 umapathi

+0

對不起。你可以搜索它。 – Byeon0gam

0

首先所有事務和會議應當由Spring容器管理的是很好的做法,所以請不要在自己的管理會話,只使用現有的數據庫查詢會話。現在,僅適用於您的情況,請在控制器級別嘗試@Transactional註釋,如果它可以工作,則需要按照以下給出的內容進行一些修改。

For web MVC Spring app should @Transactional go on controller or service?

0

使用@Transactional的服務水平,在操作中的一個不工作,因爲它應該(例如,更新操作返回0,這意味着它失敗)拋出新的RuntimeException()。如果其中一個操作失敗,那麼屬於事務部分的所有其他操作都將回滾。

0

您正試圖同時使用容器管理的事務和用戶管理的事務。儘量一次僅使用一個。

刪除@Transactional註釋或從您的方法中刪除交易報表。