我试图根据文档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
}
我认为我在这里遗漏了一些明显的东西,并且非常感谢任何指针。
答案 0 :(得分:1)
您需要使用JtaTransactionManager
并将JtaTransactionManager.getTransactionManager()
的结果传递给Ehcache。
答案 1 :(得分:0)