2010-11-02 60 views
1

嗨,Spring MVC中的HibernateTransactionManager錯誤

我試圖從Spring MVC控制器添加用戶到數據庫。而我得到以下異常:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here 
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:656) 
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:637) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 

我的代碼如下:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 
    <property name="driverClassName" value="org.postgresql.Driver" /> 
    <property name="url" value="jdbc:postgresql://127.0.0.1/doolloop2" /> 
    <property name="username" value="doolloop2" /> 
    <property name="password" value="doolloop" /> 
</bean> 

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="mappingLocations"> 
     <list> 
     <value>WEB-INF/mapping/User.hbm.xml</value> 
     </list> 
    </property> 
    <property name="hibernateProperties"> 
<props> 
    <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop> 
    <prop key="hibernate.show_sql">true</prop> 
    <prop key="hibernate.hbm2ddl.auto">update</prop> 
    </props> 
    </property> 
</bean> 
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactory" /> 
</bean> 

<bean id="txService" class="com.doolloop.services.TxDataBaseServiceImpl"> 
     <property name="sessionFactory"> 
     <ref local="sessionFactory"/> 
     </property> 
    </bean> 

然後,我的服務是這樣的:

public class TxDataBaseServiceImpl implements TxDataBaseService { 

private SessionFactory sessionFactory; 

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

@Override 
@Transactional 
public void add(User user) { 
    Session session = this.sessionFactory.getCurrentSession(); 

    Transaction tr = session.getTransaction(); 
    tr.begin(); 
    session.save(user); 
    tr.commit(); 
}  
} 

現在在我的Servlet我接線類

@Autowired 
private TxDataBaseServiceImpl service; 



    @RequestMapping(value="/signup.dlp",method = RequestMethod.POST) 
     public ModelAndView addUser(@Valid User user, BindingResult result){ 
     logger.info("Return View"); 
     registrationValidation.validate(user, result); 
       /// all required validations.... 
     service.add(user); 
     ModelAndView mv = new ModelAndView("/Accounts/signupSuccess"); 
     return mv; 
    } 

T他的問題是我做錯了什麼?

+1

也許你忘了''? – axtavt 2010-11-02 17:29:37

+0

是的,這也很重要! – 2010-11-02 17:32:46

+2

這不是真正導致你遇到的錯誤,但你爲什麼要在方法上使用聲明式事務,然後試圖以編程方式管理方法內部的事務? – Affe 2010-11-02 17:44:36

回答

3

這是錯誤:

@Autowired 
private TxDataBaseServiceImpl service; 

您正在嘗試自動裝配的實現類,而不是接口,因此你注射生豆,而不是事務代理。像這樣做:

@Autowired 
private TxDataBaseService service; 

總是喜歡對編碼接口,因爲一)約書亞·布洛克這樣說:-)和b)在春季有很多的功能是在接口內置。

+0

不幸的是,它沒有幫助:( – 2010-11-02 19:22:51

+0

請參閱上面的axtavt的註釋。如果你遵循該建議(添加tx:註釋驅動並丟失開始/提交事務),那麼它應該可以工作 – 2010-11-02 19:52:44

+0

是的,它幫助。你的幫助。 – 2010-11-02 21:17:53