2016-04-28 151 views
0

我的事務中的回滾無效(Spring 3.1)。我試圖編輯我的配置xml文件,如here但沒有結果。 這裏我的XML文件:Spring @Transactional配置xml

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:mvc="http://www.springframework.org/schema/mvc" 
     xmlns="http://www.springframework.org/schema/beans" 
     xmlns:jee="http://www.springframework.org/schema/jee" 
     xmlns:aop= "http://www.springframework.org/schema/aop" 
     xmlns:tx= "http://www.springframework.org/schema/tx" 
     xmlns:jpa="http://www.springframework.org/schema/data/jpa" 
     xsi:schemaLocation=" 
       http://www.springframework.org/schema/mvc 
       http://www.springframework.org/schema/mvc/spring-mvc.xsd 
       http://www.springframework.org/schema/jee 
       http://www.springframework.org/schema/jee/spring-jee-3.1.xsd 
       http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans.xsd 
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-2.0.xsd 
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-2.0.xsd 
       http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd 
       "> 

     <jee:jndi-lookup id="dataSourceUsrAppe1" jndi-name="jdbc/ZhabDS"/> 

     <bean id="utentiDAO" class="it.dao.UtentiDAO"> 
      <property name="dataSourceUsrAppe1"> 
       <ref bean="dataSourceUsrAppe1"/> 
      </property> 
     </bean> 
<!-- doesn't work with this: 
     <tx:annotation-driven transaction-manager="txManager"/> 
      <property name="dataSourceUsrAppe1"> 
       <ref bean="dataSourceUsrAppe1"/> 
      </property> 
     </bean> 
    </beans> 
--> 
</beans> 

我應該在這裏增加一個事務管理器?

這裏是我的服務:

@Service 
public class UtentiService { 
    @Autowired 
    private UtentiDAO utentiDAO; 

    @Transactional(rollbackFor={Exception.class}, propagation=Propagation.REQUIRED) 
    public boolean createUser(Zhabuten user) throws Exception 
    { 
      long idPrincipale; 
      idPrincipale = utentiDAO.insert(user, utentiDAO.query1); 
      idPrincipale = utentiDAO.insert(user, utentiDAO.query2); 

      if (idPrincipale!=0) throw new java.lang.Exception(); 

      idPrincipale = utentiDAO.insert(user, utentiDAO.query3); 
     return false; 
    } 
} 

異常被拋出正確的,它從控制器獲取,並數據庫沒有rollbacked。 我是否缺少xml中的任何配置?

+1

您的交易管理器來自哪裏? –

+0

你檢查過日誌嗎?交易是否開始和提交? –

+0

我不明白如何配置它。我應該刪除這個? <屬性名= 「dataSourceUsrAppe1」> Accollativo

回答

2

使用以下xml配置。

<!-- Enable Annotation based Declarative Transaction Management --> 
<tx:annotation-driven proxy-target-class="true" 
    transaction-manager="transactionManager" /> 

<!-- Creating TransactionManager Bean, since JDBC we are creating of type 
    DataSourceTransactionManager --> 
<bean id="transactionManager" 
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
    <property name="dataSource" ref="dataSourceUsrAppe1" /> 
</bean> 
+0

謝謝,這個作品! 奇怪的是,在這之後,我剛剛從eclipse中以調試模式得到了一個新奇怪的彈出錯誤(之後,我清理了所有舊的斷點):無法安裝斷點it.service.UtentiService $$ ... CGLIB ... $$ someHEXnumber due缺少行編譯器屬性。修改編譯器選項以生成行號屬性。 原因:缺席行號信息。 – Accollativo