2

这两天我一直在尝试解决这个问题,但没有成功。我在 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();
}

弹簧配置文件:

<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";
}
4

1 回答 1

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" />
于 2011-09-21T19:24:17.497 回答