Spring PlatformTransactionManager

时间:2017-06-29 12:31:26

标签: spring spring-transactions

在我的代码中PlatformTransactionManager无法正常工作,我在我的普通pojo java类中使用下面的代码

Java类

TransactionDefinition def = new DefaultTransactionDefinition();
TransactionStatus status = transactionManager.getTransaction(def);

这里我得到了def值PROPAGATION_REQUIRED,ISOLATION_DEFAULT 之后TransactionStatus我得到了NullPointerException

Error 

java.lang.NullPointerException
    at com.polaris.collateral.services.CollateralInterfaceBean.attachCollateralLine(JavaClass.java:138)
    at com.polaris.collateral.collateral.domain.CollMaintDomain.earMarkColl(Dao.java:2993)

我还在我的xml文件中配置了数据源

<bean id="JavaClass" class="com.test.JavaClass">
    <property name="dataSource" ref="dataSource" />
    <property name="transactionManager" ref="transactionManager" />
</bean>     

1 个答案:

答案 0 :(得分:0)

您还需要xml.config中的PlatformTransactionManager bean,该bean在事务类中为@Autowired。 看起来您也喜欢采用编程方法,所以我想它会像这样。

public foo(){
    DefaultTransactionDefinition def = new DefaultTransactionDefinition();
    def.setName("Transaction");
    def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); 
    TransactionStatus ts = txMgr.getTransaction(def);
     try
    {
        txMgr.getTransaction(def);

        //some business logic 
        txMgr.commit(ts);

        logger.info("Commit Transaction");
        return bar;
    }
    catch (Exception ex)
    {
        ex.printStackTrace();
        txMgr.rollback(ts);
        logger.info("Rollback Transaction");
        throw ex;
    }
}