PlatformTransactionManager

PlatformTransactionManager

我试图根据文档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
}


我想我在这里遗漏了一些明显的东西,非常感谢您提出任何建议。

最佳答案

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

10-08 14:58