两天来,我一直在尝试解决这个问题,但没有成功。我在Spring3.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";
}发布于 2011-09-22 03:24:17
我不太确定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" />https://stackoverflow.com/questions/7494722
复制相似问题