/** * Flushes the value caches of the specified entities along with all of the relevant relations * cache entries. This should be called after a process outside of AO control may have modified * the values in the specified rows. This does not actually remove the entity instances themselves * from the instance cache. Rather, it just flushes all of their internally cached values (with * the exception of the primary key). */ public void flush(RawEntity<?>... entities) { List<Class<? extends RawEntity<?>>> types = new ArrayList<Class<? extends RawEntity<?>>>(entities.length); Map<RawEntity<?>, EntityProxy<?, ?>> toFlush = new HashMap<RawEntity<?>, EntityProxy<?, ?>>(); proxyLock.readLock().lock(); try { for (RawEntity<?> entity : entities) { verify(entity); types.add(entity.getEntityType()); toFlush.put(entity, proxies.get(entity)); } } finally { proxyLock.readLock().unlock(); } for (Entry<RawEntity<?>, EntityProxy<?, ?>> entry : toFlush.entrySet()) { entry.getValue().flushCache(entry.getKey()); } relationsCache.remove(types.toArray(new Class[types.size()])); }
/** * Deletes the specified entities from the database. DELETE statements are called on the rows in * the corresponding tables and the entities are removed from the instance cache. The entity * instances themselves are not invalidated, but it doesn't even make sense to continue using the * instance without a row with which it is paired. * * <p>This method does attempt to group the DELETE statements on a per-type basis. Thus, if you * pass 5 instances of <code>EntityA</code> and two instances of <code>EntityB</code>, the * following SQL prepared statements will be invoked: * * <pre>DELETE FROM entityA WHERE id IN (?,?,?,?,?); * DELETE FROM entityB WHERE id IN (?,?);</pre> * * <p>Thus, this method scales very well for large numbers of entities grouped into types. * However, the execution time increases linearly for each entity of unique type. * * @param entities A varargs array of entities to delete. Method returns immediately if length == * 0. */ @SuppressWarnings("unchecked") public void delete(RawEntity<?>... entities) throws SQLException { if (entities.length == 0) { return; } Map<Class<? extends RawEntity<?>>, List<RawEntity<?>>> organizedEntities = new HashMap<Class<? extends RawEntity<?>>, List<RawEntity<?>>>(); for (RawEntity<?> entity : entities) { verify(entity); Class<? extends RawEntity<?>> type = getProxyForEntity(entity).getType(); if (!organizedEntities.containsKey(type)) { organizedEntities.put(type, new LinkedList<RawEntity<?>>()); } organizedEntities.get(type).add(entity); } entityCacheLock.writeLock().lock(); try { DatabaseProvider provider = getProvider(); Connection conn = provider.getConnection(); try { for (Class<? extends RawEntity<?>> type : organizedEntities.keySet()) { List<RawEntity<?>> entityList = organizedEntities.get(type); StringBuilder sql = new StringBuilder("DELETE FROM "); tableNameConverterLock.readLock().lock(); try { sql.append(provider.processID(tableNameConverter.getName(type))); } finally { tableNameConverterLock.readLock().unlock(); } sql.append(" WHERE ") .append(provider.processID(Common.getPrimaryKeyField(type, getFieldNameConverter()))) .append(" IN (?"); for (int i = 1; i < entityList.size(); i++) { sql.append(",?"); } sql.append(')'); Logger.getLogger("net.java.ao").log(Level.INFO, sql.toString()); PreparedStatement stmt = conn.prepareStatement(sql.toString()); int index = 1; for (RawEntity<?> entity : entityList) { TypeManager.getInstance() .getType((Class) entity.getEntityType()) .putToDatabase(this, stmt, index++, entity); } relationsCache.remove(type); stmt.executeUpdate(); stmt.close(); } } finally { conn.close(); } for (RawEntity<?> entity : entities) { entityCache.remove(new CacheKey(Common.getPrimaryKeyValue(entity), entity.getEntityType())); } proxyLock.writeLock().lock(); try { for (RawEntity<?> entity : entities) { proxies.remove(entity); } } finally { proxyLock.writeLock().unlock(); } } finally { entityCacheLock.writeLock().unlock(); } }
private void verify(RawEntity<?> entity) { if (entity.getEntityManager() != this) { throw new RuntimeException("Entities can only be used with a single EntityManager instance"); } }