gpt4 book ai didi

java - Spring 事务回滚

转载 作者:行者123 更新时间:2023-11-30 11:51:23 32 4
gpt4 key购买 nike

这两天我一直在努力解决这个问题,但没有成功。我在 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 类(用@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 工厂还是从应用程序上下文中返回一个 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" />

关于java - Spring 事务回滚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7494722/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com