Example #1
0
  @Override
  public MarshalledEntry load(Object key) {
    if (!isValidKeyType(key)) {
      return null;
    }

    EntityManager em = emf.createEntityManager();
    try {
      EntityTransaction txn = em.getTransaction();
      long txnBegin = timeService.time();
      txn.begin();
      try {
        long entityFindBegin = timeService.time();
        Object entity = em.find(configuration.entityClass(), key);
        stats.addEntityFind(timeService.time() - entityFindBegin);
        try {
          if (entity == null) return null;
          InternalMetadata m = null;
          if (configuration.storeMetadata()) {
            byte[] keyBytes;
            try {
              keyBytes = marshaller.objectToByteBuffer(key);
            } catch (Exception e) {
              throw new JpaStoreException("Failed to marshall key", e);
            }
            long metadataFindBegin = timeService.time();
            MetadataEntity metadata = em.find(MetadataEntity.class, keyBytes);
            stats.addMetadataFind(timeService.time() - metadataFindBegin);
            if (metadata != null && metadata.getMetadata() != null) {
              try {
                m = (InternalMetadata) marshaller.objectFromByteBuffer(metadata.getMetadata());
              } catch (Exception e) {
                throw new JpaStoreException("Failed to unmarshall metadata", e);
              }
              if (m.isExpired(timeService.wallClockTime())) {
                return null;
              }
            }
          }
          if (trace) log.trace("Loaded " + entity + " (" + m + ")");
          return marshallerEntryFactory.newMarshalledEntry(key, entity, m);
        } finally {
          try {
            txn.commit();
            stats.addReadTxCommitted(timeService.time() - txnBegin);
          } catch (Exception e) {
            stats.addReadTxFailed(timeService.time() - txnBegin);
            throw new JpaStoreException("Failed to load entry", e);
          }
        }
      } finally {
        if (txn != null && txn.isActive()) txn.rollback();
      }
    } finally {
      em.close();
    }
  }
Example #2
0
 @Override
 public void write(MarshalledEntry entry) throws CacheLoaderException {
   if (log.isTraceEnabled()) {
     log.tracef("Adding entry: %s", entry);
   }
   InternalMetadata metadata = entry.getMetadata();
   long lifespan = metadata != null ? metadata.lifespan() : -1;
   long maxIdle = metadata != null ? metadata.maxIdle() : -1;
   remoteCache.put(
       entry.getKey(),
       configuration.rawValues() ? entry.getValue() : entry,
       toSeconds(lifespan, entry.getKey(), LIFESPAN),
       TimeUnit.SECONDS,
       toSeconds(maxIdle, entry.getKey(), MAXIDLE),
       TimeUnit.SECONDS);
 }
Example #3
0
  @Override
  public void process(
      KeyFilter filter,
      final CacheLoaderTask task,
      Executor executor,
      boolean fetchValue,
      final boolean fetchMetadata) {
    ExecutorAllCompletionService eacs = new ExecutorAllCompletionService(executor);
    final TaskContextImpl taskContext = new TaskContextImpl();
    EntityManager em = emf.createEntityManager();

    try {
      CriteriaBuilder cb = em.getCriteriaBuilder();
      CriteriaQuery cq = cb.createQuery();
      Root root = cq.from(configuration.entityClass());
      Type idType = root.getModel().getIdType();
      SingularAttribute idAttr = root.getModel().getId(idType.getJavaType());
      cq.select(root.get(idAttr));

      for (final Object key : em.createQuery(cq).getResultList()) {
        if (taskContext.isStopped()) break;
        if (filter != null && !filter.shouldLoadKey(key)) {
          if (trace) log.trace("Key " + key + " filtered");
          continue;
        }
        EntityTransaction txn = em.getTransaction();

        Object tempEntity = null;
        InternalMetadata tempMetadata = null;
        boolean loaded = false;
        txn.begin();
        try {
          do {
            try {
              tempEntity = fetchValue ? em.find(configuration.entityClass(), key) : null;
              tempMetadata = fetchMetadata ? getMetadata(em, key) : null;
            } finally {
              try {
                txn.commit();
                loaded = true;
              } catch (Exception e) {
                log.trace("Failed to load once", e);
              }
            }
          } while (!loaded);
        } finally {
          if (txn != null && txn.isActive()) txn.rollback();
        }
        final Object entity = tempEntity;
        final InternalMetadata metadata = tempMetadata;
        if (trace) log.trace("Processing " + key + " -> " + entity + "(" + metadata + ")");

        if (metadata != null && metadata.isExpired(timeService.wallClockTime())) continue;
        eacs.submit(
            new Callable<Void>() {
              @Override
              public Void call() throws Exception {
                try {
                  final MarshalledEntry marshalledEntry =
                      marshallerEntryFactory.newMarshalledEntry(key, entity, metadata);
                  if (marshalledEntry != null) {
                    task.processEntry(marshalledEntry, taskContext);
                  }
                  return null;
                } catch (Exception e) {
                  log.errorExecutingParallelStoreTask(e);
                  throw e;
                }
              }
            });
      }
      eacs.waitUntilAllCompleted();
      if (eacs.isExceptionThrown()) {
        throw new org.infinispan.persistence.spi.PersistenceException(
            "Execution exception!", eacs.getFirstException());
      }
    } finally {
      em.close();
    }
  }