2015-04-07 33 views
0

我想使用彈簧交易,並失敗。當我設置它們使我的web應用程序將在Tomcat下推出,呼籲春季交易:接口v類

TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); 

當我需要回滾事務,但先前的DB更改不會回滾,我得到了下面的錯誤我localhost.date.log文件:

SEVERE: Servlet.service() for servlet model threw exception 
org.springframework.transaction.NoTransactionException: No transaction aspect-managed TransactionStatus in scope 
    at org.springframework.transaction.interceptor.TransactionAspectSupport.currentTransactionStatus(TransactionAspectSupport.java:122) 
    at edu.mayo.bsi.backslapper.model.dao.impl.TransactServiceDAOImpl.giveProps(TransactServiceDAOImpl.java:160) 

public interface TransactServiceDAO 

public class TransactServiceDAOImpl implements TransactServiceDAO 
在我的應用上下文-config.xml中

<bean id = "transactService" class = "edu.mayo.bsi.backslapper.model.dao.impl.TransactServiceDAOImpl"/> 

<!-- enable the configuration of transactional behavior based on annotations --> 
<!-- the transactional advice (what 'happens'; see the <aop:advisor/> bean below) --> 
<tx:advice id = "txAdvice" transaction-manager = "txManager"> 
    <!-- the transactional semantics... --> 
    <tx:attributes> 
     <!-- all methods starting with 'get' are read-only --> 
     <tx:method name = "get*" read-only = "true"/> 
     <!-- other methods use the default transaction settings (see below) --> 
     <tx:method name = "*"/> 
    </tx:attributes> 
</tx:advice> 

<!-- ensure that the above transactional advice runs for any execution of an operation defined by the TransactServiceDAO interface --> 
<aop:config> 
    <aop:pointcut id = "transactServiceOperation" expression = "execution(* edu.mayo.bsi.backslapper.model.dao.TransactServiceDAO.*(..))"/> 
    <aop:advisor advice-ref = "txAdvice" pointcut-ref = "transactServiceOperation"/> 
</aop:config> 

<bean id = "txManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
    <property name = "dataSource" ref = "backDataSource"/> 
</bean> 

<tx:advice id = "txAdvice" transaction-manager = "txManager"> 
    <tx:attributes> 
     <tx:method name = "*" rollback-for = "Throwable"/> 
     <tx:method name = "*" rollback-for = "java.sql.SQLException"/> 
    </tx:attributes> 
</tx:advice> 

下面是一個例子,一個例程,其中一個(計數== 0)回滾不會取消「刪除老用戶」。

我在做什麼錯?

public String setUsers (DataSource dataSource, User theUser, String store, AdminUser[] newUsers) 
{ 
    NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate (dataSource); 
    Map<String, Object>   params = new HashMap<String, Object>(); 

    params.put ("lanID", theUser.getLanID()); 
    params.put ("storeName", store); 

    int count = namedParameterJdbcTemplate.update (kDeleteOldUsers, params); 

    for (AdminUser curUser : newUsers) 
    { 
     int userID = updateUser (namedParameterJdbcTemplate, curUser.getLanID(), curUser.getFullName(), curUser.getEmail(), null); 

     if (userID == kError) 
      userID = addUser (namedParameterJdbcTemplate, curUser); 

     params.put ("userID", Integer.valueOf (userID)); 
     count = namedParameterJdbcTemplate.update (kAddStoreUser, params); 
     if (count == 0) // Wasn't inserted 
      TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); 

     params.put ("backslapsToGive", Integer.valueOf (curUser.getBackslapsToGive())); 
     params.put ("monthlyBackslaps", Integer.valueOf (curUser.getMaxBackslaps())); 
     count = namedParameterJdbcTemplate.update (kUpdateUserStores, params); 
     if (count == 0) // Wasn't updated 
      TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); 
    } 

    return null; 
} 

回答

0

我從來沒有使用過 TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); 爲此目的。 我想你應該在這裏拋出異常。最好的方法是擴展RuntimeException。春季交易是一種始終如一的方法。如果引發異常事務被回滾。

如果你不想讓這個異常中斷你的流程,你可以把它放在這個方法中,並捕獲調用方法。

+0

TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();拋出異常:重度:在org.springframework.transaction.interceptor.TransactionAspectSupport.currentTransactionStatus(TransactionAspectSupport無交易方面管理的TransactionStatus範圍 \t:對servlet模型Servlet.service()拋出異常 org.springframework.transaction.NoTransactionException。 java:122) \t at edu.mayo.bsi.backslapper.model.dao.impl.TransactServiceDAOImpl.giveProps(TransactServiceDAOImpl.java:160) –

+0

很明顯,你沒有附加tx這個方法。所以首先你需要檢查你的aop:切入點是否正確。其次,我不確定這是配置您的TX的最佳方式。這意味着您每次需要添加新服務時都需要更改您的配置。這意味着該服務中的每個方法都是事務性的,並不總是您想要的。我建議你用設置你的conf,然後使用@Transactional註釋方法,並在必要時在實現方法上添加readOnly = true。 – mommcilo

+0

我的aop:切入點在那裏,在中間。它有什麼問題嗎? TransactService處理所有事務性數據庫調用,不管啓動服務如何,唯一的事情是事務性的。我試着在例程上使用Transactional,而其他8個類突然無法編譯。刪除它,並編譯。所以我想避免Transactional –