コード例 #1
0
ファイル: JpaStore.java プロジェクト: Cotton-Ben/infinispan
  @Override
  public void clear() {
    EntityManager em = emf.createEntityManager();
    EntityTransaction txn = em.getTransaction();

    try {
      // the clear operation often deadlocks - let's try several times
      for (int i = 0; ; ++i) {
        txn.begin();
        try {
          log.trace("Clearing JPA Store");
          String entityTable = em.getMetamodel().entity(configuration.entityClass()).getName();
          @SuppressWarnings("unchecked")
          List<Object> items = em.createQuery("FROM " + entityTable).getResultList();
          for (Object o : items) em.remove(o);
          if (configuration.storeMetadata()) {
            String metadataTable = em.getMetamodel().entity(MetadataEntity.class).getName();
            Query clearMetadata = em.createQuery("DELETE FROM " + metadataTable);
            clearMetadata.executeUpdate();
          }
          txn.commit();
          em.clear();
          break;
        } catch (Exception e) {
          log.trace("Failed to clear store", e);
          if (i >= 10) throw new JpaStoreException("Exception caught in clear()", e);
        } finally {
          if (txn != null && txn.isActive()) txn.rollback();
        }
      }
    } finally {
      em.close();
    }
  }
コード例 #2
0
ファイル: JpaStore.java プロジェクト: Cotton-Ben/infinispan
  @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();
    }
  }
コード例 #3
0
ファイル: JpaStore.java プロジェクト: Cotton-Ben/infinispan
  @Override
  public void write(MarshalledEntry entry) {
    EntityManager em = emf.createEntityManager();

    Object entity = entry.getValue();
    MetadataEntity metadata =
        configuration.storeMetadata()
            ? new MetadataEntity(
                entry.getKeyBytes(),
                entry.getMetadataBytes(),
                entry.getMetadata() == null ? Long.MAX_VALUE : entry.getMetadata().expiryTime())
            : null;
    try {
      if (!configuration.entityClass().isAssignableFrom(entity.getClass())) {
        throw new JpaStoreException(
            String.format(
                "This cache is configured with JPA CacheStore to only store values of type %s - cannot write %s = %s",
                configuration.entityClass().getName(), entity, entity.getClass().getName()));
      } else {
        EntityTransaction txn = em.getTransaction();
        Object id = emf.getPersistenceUnitUtil().getIdentifier(entity);
        if (!entry.getKey().equals(id)) {
          throw new JpaStoreException(
              "Entity id value must equal to key of cache entry: "
                  + "key = ["
                  + entry.getKey()
                  + "], id = ["
                  + id
                  + "]");
        }
        long txnBegin = timeService.time();
        try {
          if (trace) log.trace("Writing " + entity + "(" + toString(metadata) + ")");
          txn.begin();

          long entityMergeBegin = timeService.time();
          em.merge(entity);
          stats.addEntityMerge(timeService.time() - entityMergeBegin);
          if (metadata != null && metadata.hasBytes()) {
            long metadataMergeBegin = timeService.time();
            em.merge(metadata);
            stats.addMetadataMerge(timeService.time() - metadataMergeBegin);
          }

          txn.commit();
          stats.addWriteTxCommited(timeService.time() - txnBegin);
        } catch (Exception e) {
          stats.addWriteTxFailed(timeService.time() - txnBegin);
          throw new JpaStoreException("Exception caught in write()", e);
        } finally {
          if (txn != null && txn.isActive()) txn.rollback();
        }
      }
    } finally {
      em.close();
    }
  }
コード例 #4
0
ファイル: JpaStore.java プロジェクト: Cotton-Ben/infinispan
  public boolean delete(Object key) {
    if (!isValidKeyType(key)) {
      return false;
    }

    EntityManager em = emf.createEntityManager();
    try {
      long entityFindBegin = timeService.time();
      Object entity = em.find(configuration.entityClass(), key);
      stats.addEntityFind(timeService.time() - entityFindBegin);
      if (entity == null) {
        return false;
      }
      MetadataEntity metadata = 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();
        metadata = em.find(MetadataEntity.class, keyBytes);
        stats.addMetadataFind(timeService.time() - metadataFindBegin);
      }

      EntityTransaction txn = em.getTransaction();
      if (trace) log.trace("Removing " + entity + "(" + toString(metadata) + ")");
      long txnBegin = timeService.time();
      txn.begin();
      try {
        long entityRemoveBegin = timeService.time();
        em.remove(entity);
        stats.addEntityRemove(timeService.time() - entityRemoveBegin);
        if (metadata != null) {
          long metadataRemoveBegin = timeService.time();
          em.remove(metadata);
          stats.addMetadataRemove(timeService.time() - metadataRemoveBegin);
        }
        txn.commit();
        stats.addRemoveTxCommitted(timeService.time() - txnBegin);
        return true;
      } catch (Exception e) {
        stats.addRemoveTxFailed(timeService.time() - txnBegin);
        throw new JpaStoreException("Exception caught in delete()", e);
      } finally {
        if (txn != null && txn.isActive()) txn.rollback();
      }
    } finally {
      em.close();
    }
  }
コード例 #5
0
ファイル: JpaStore.java プロジェクト: Cotton-Ben/infinispan
  @Override
  public boolean contains(Object key) {
    if (!isValidKeyType(key)) {
      return false;
    }

    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);
        if (trace) log.trace("Entity " + key + " -> " + entity);
        try {
          if (entity == null) return false;
          if (configuration.storeMetadata()) {
            byte[] keyBytes;
            try {
              keyBytes = marshaller.objectToByteBuffer(key);
            } catch (Exception e) {
              throw new JpaStoreException("Cannot marshall key", e);
            }
            long metadataFindBegin = timeService.time();
            MetadataEntity metadata = em.find(MetadataEntity.class, keyBytes);
            stats.addMetadataFind(timeService.time() - metadataFindBegin);
            if (trace) log.trace("Metadata " + key + " -> " + toString(metadata));
            return metadata == null || metadata.expiration > timeService.wallClockTime();
          } else {
            return true;
          }
        } finally {
          txn.commit();
          stats.addReadTxCommitted(timeService.time() - txnBegin);
        }
      } catch (RuntimeException e) {
        stats.addReadTxFailed(timeService.time() - txnBegin);
        throw e;
      } finally {
        if (txn != null && txn.isActive()) txn.rollback();
      }
    } finally {
      em.close();
    }
  }