将PlatformTransactionManager注入Ehcache

时间:2015-02-12 19:48:18

标签: java spring ehcache

我试图根据文档here将Spring的PlatformTransactionManager注入Ehcache。我现有的事务管理器定义如下:

@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {

    JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
    jpaTransactionManager.setEntityManagerFactory(entityManagerFactory);

    return jpaTransactionManager;
}

文档说明您需要编写自己的实现TransactionManagerLookup的类:

public class MyTransactionManagerLookup implements TransactionManagerLookup {

    @Override
    public TransactionManager getTransactionManager() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void init() {
        // TODO Auto-generated method stub
    }

    @Override
    public void register(EhcacheXAResource arg0, boolean arg1) {
        // TODO Auto-generated method stub
    }

    @Override
    public void setProperties(Properties arg0) {
        // TODO Auto-generated method stub
    }

    @Override
    public void unregister(EhcacheXAResource arg0, boolean arg1) {
        // TODO Auto-generated method stub
    }
}

我已尝试将PlatformTransactionManager自动装入MyTransactionManagerLookup但您无法从PlatformTransactionManager转换为TransactionManager:

@Autowired
PlatformTransactionManager ptm;

@Override
public TransactionManager getTransactionManager() {

    return ptm; // cannot convert from PlatformTransactionManager to TransactionManager
}

我认为我在这里遗漏了一些明显的东西,并且非常感谢任何指针。

2 个答案:

答案 0 :(得分:1)

您需要使用JtaTransactionManager并将JtaTransactionManager.getTransactionManager()的结果传递给Ehcache。

答案 1 :(得分:0)

感谢Louis Jacomet的answer我意识到我需要使用JtaTransactionManager代替JpaTransactionManager

我选择Atomikos,因为Ehcache会自动识别此事务管理器并注册它而无需自定义TransactionManagerLookup