首页 文章

Spring事务回滚

提问于
浏览
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();
}

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 回答

  • 1

    我不太确定 BillingFactory.getBillingImplementation("") 的作用 . 它是纯Java工厂还是从应用程序上下文返回Spring服务?我'm also not sure if you have Spring transactional proxies - if not then what you do is very likely autocommited. I think it' d为包 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" />
    

相关问题