簡體   English   中英

春季交易回滾

[英]Spring transaction rollback

我一直在設法解決這一問題,但沒有成功。 我在Spring 3.0.5和Postgress中使用注釋驅動的事務。 我從業務邏輯方法中調用了兩種dao方法:

@Transactional 
public void registerTransaction(GoogleTransaction transaction) {
       long transactionID = DBFactory.getTransactionDBInstance().addTransaction(transaction);
       DBFactory.getGoogleTransactionDBInstance().addGoogleTransaction(transaction, transactionID);

}

第二種方法(addGoogleTransaction)在結尾處引發RuntimeException,但是不會回滾事務,並且會插入兩行。

DAO方法如下所示:

public void addGoogleTransaction(GoogleTransaction transaction, long id) {
    log.trace("Entering addGoogleTransaction DAO method ");
    log.trace(transaction.toString());
    getSimpleJdbcTemplate().update(QRY_ADD_GOOGLE_TRANSACTION, new Object[] {id, transaction.getGoogleSerialNumber() ,
        transaction.getGoogleBuyerID(), transaction.getGoogleOrderID()});
    log.trace("Google transaction added successfully");
    throw new RuntimeException();
}

Spring配置文件:

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven />

我是否需要配置其他內容? 我試圖將@Transactional添加到業務邏輯類中,並將@Transactional添加到dao方法中,但這都不起作用。 感謝名單

從控制器類(用@Controller注釋)中調用它以測試目的。

@RequestMapping(value = "/registration")
public String sendToRegistrationPage() throws ServiceException {

    GoogleTransaction googleTransaction = new GoogleTransaction(0, "aei", new Date(), TransactionStatus.NEW, BigDecimal.ZERO, "", "", 0, "");
    BillingFactory.getBillingImplementation("").registerTransaction(googleTransaction);
    return "registration";
}

我不太確定BillingFactory.getBillingImplementation("")作用。 它是純Java工廠還是ist從應用程序上下文返回Spring服務? 我也不確定您是否有Spring事務代理-如果沒有,那么您的工作很可能會自動提交。 我認為為包org.springframework.transaction啟用日志記錄是一個好主意。

實際上,我期望這樣的事情:

@Controller
public class MyController {

    @Resource
    private BillingService billingService;

    @RequestMapping(value = "/registration")
    public String sendToRegistrationPage() throws ServiceException {
        GoogleTransaction googleTransaction = new GoogleTransaction(0, "aei", new Date(), TransactionStatus.NEW, BigDecimal.ZERO, "", "", 0, "");
        billingService.registerTransaction(googleTransaction);
        return "registration";
    }
}

在您的Spring配置中,例如(或@Service注釋的bean):

<bean id="billingService" class="foo.bar.BillingImplementation" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM