Spring事务回滚

时间:2011-09-21 04:44:18

标签: java spring spring-transactions

我一直试图在没有成功的情况下解决这两天。我正在使用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";
}

1 个答案:

答案 0 :(得分:1)

我不太确定BillingFactory.getBillingImplementation("")是做什么的。它是纯Java工厂还是从应用程序上下文返回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" />