【发布时间】:2015-08-28 13:32:25
【问题描述】:
我正在使用带有 Infinispan (6.0.2) 的 Wildfly 8.2,并且我正在尝试将某个 Oracle 数据库表中的所有值缓存在 Infispan 缓存中。在大多数情况下,它似乎有效,但有时却无效。访问 cache.values() 时(这对于性能来说也可能不是一个好主意,但只是一个示例),它有时看起来是空的,有时它包含正确的值。因此,我认为事务的配置可能存在问题。当使 Infinispan 缓存非事务性时,问题就消失了。
访问缓存和数据库的服务是一个带有容器管理事务的 EJB bean。在初始化服务时,所有数据都从数据库加载(它不包含很多条目)。
根据what's new in ejb 3.2,应该可以在 EJB Singleton bean 中以事务方式访问数据库。
数据源和 Infinispan 缓存的配置是否正确?我可以在 Infinispan 中使用非 XA 数据源并期望它始终如一地工作吗?根据Infinispan doc,NON_XA表示Infinispan注册为Synchronization,应该没问题吧?
standalone-full.xml 中的缓存配置如下(删除<transaction mode="NON_XA" locking="PESSIMISTIC"/> 时问题消失,代价是没有事务缓存):
<cache-container name="cacheContainer" start="EAGER">
<local-cache name="my_table_cache">
<locking isolation="REPEATABLE_READ"/>
<transaction mode="NON_XA" locking="PESSIMISTIC"/>
</local-cache>
</cache-container>
Oracle DS 定义如下
<datasource jndi-name="java:jboss/datasources/myDataSource" pool-name="dataSource" enabled="true">
<connection-url>jdbc:oracle:thin:@127.0.0.1:1523:orcl</connection-url>
<driver>ojdbc7.jar</driver>
<pool>
<max-pool-size>25</max-pool-size>
</pool>
<security>
<user-name>myuser</user-name>
<password>myuser</password>
</security>
<timeout>
<blocking-timeout-millis>5000</blocking-timeout-millis>
</timeout>
</datasource>
我的服务(dao 使用简单的 JDBC 操作,而不是 Hibernate 或类似的)
@Singleton
@ConcurrencyManagement(ConcurrencyManagementType.BEAN)
public class MyService {
@Resource(lookup = "java:jboss/infinispan/container/cacheContainer")
protected EmbeddedCacheManager cm;
protected Cache<String, MyEntity> cache;
@PostConstruct
private void init() {
try {
cache = getCache();
} catch (SQLException ex) {
log.fatal("could not initialize caches", ex);
throw new IllegalStateException(ex);
}
}
public Cache<String, MyEntity> getCache() {
Cache<String, MyEntity> cache = cm.getCache(getCacheName(), true);
fillCache(cache);
return cache;
}
protected void fillCache(Cache<String, MyEntity> cache) {
List<MyEntity> entities = myDao.getEntities();
for (MyEntity e : entities) {
cache.put(e.getKey, e);
}
}
public MyEntity getEntity(String key) {
return cache.get(key);
}
public void insert(MyEntity entity) {
myDao.insert(entity);
cache.put(entity.getKey(), entity);
}
public void debug() {
log.debug(cache.values());
}
}
【问题讨论】:
标签: java transactions ejb wildfly infinispan