protected void initializeOperationManager() { dbOperationManager = new DbOperationManager(); }
protected DbBulkOperation performBulkOperation(Class<? extends DbEntity> entityType, String statement, Object parameter, DbOperationType operationType) { // create operation DbBulkOperation bulkOperation = createDbBulkOperation(entityType, statement, parameter, operationType); // schedule operation dbOperationManager.addOperation(bulkOperation); return bulkOperation; }
protected DbBulkOperation performBulkOperationPreserveOrder(Class<? extends DbEntity> entityType, String statement, Object parameter, DbOperationType operationType) { DbBulkOperation bulkOperation = createDbBulkOperation(entityType, statement, parameter, operationType); // schedule operation dbOperationManager.addOperationPreserveOrder(bulkOperation); return bulkOperation; }
public List<DbOperation> calculateFlush() { List<DbOperation> flush = new ArrayList<DbOperation>(); // first INSERTs addSortedInserts(flush); // then UPDATEs + DELETEs addSortedModifications(flush); return flush; }
public boolean addOperation(DbEntityOperation newOperation) { if(newOperation.getOperationType() == INSERT) { return getInsertsForType(newOperation.getEntityType(), true) .add(newOperation); } else if(newOperation.getOperationType() == DELETE) { return getDeletesByType(newOperation.getEntityType(), true) .add(newOperation); } else { // UPDATE return getUpdatesByType(newOperation.getEntityType(), true) .add(newOperation); } }
protected void flushDbOperationManager() { // obtain totally ordered operation list from operation manager List<DbOperation> operationsToFlush = dbOperationManager.calculateFlush(); if (operationsToFlush == null || operationsToFlush.size() == 0) { return; } LOG.databaseFlushSummary(operationsToFlush); // If we want to delete all table data as bulk operation, on tables which have self references, // We need to turn the foreign key check off on MySQL and MariaDB. // On other databases we have to do nothing, the mapped statement will be empty. if (isIgnoreForeignKeysForNextFlush) { persistenceSession.executeNonEmptyUpdateStmt(TOGGLE_FOREIGN_KEY_STMT, false); persistenceSession.flushOperations(); } try { final List<List<DbOperation>> batches = CollectionUtil.partition(operationsToFlush, BATCH_SIZE); for (List<DbOperation> batch : batches) { flushDbOperations(batch); } } finally { if (isIgnoreForeignKeysForNextFlush) { persistenceSession.executeNonEmptyUpdateStmt(TOGGLE_FOREIGN_KEY_STMT, true); persistenceSession.flushOperations(); isIgnoreForeignKeysForNextFlush = false; } } }
/** Adds a correctly ordered list of UPDATE and DELETE operations to the flush. * @param flush */ protected void addSortedModifications(List<DbOperation> flush) { // calculate sorted set of all modified entity types SortedSet<Class<?>> modifiedEntityTypes = new TreeSet<Class<?>>(MODIFICATION_TYPE_COMPARATOR); modifiedEntityTypes.addAll(updates.keySet()); modifiedEntityTypes.addAll(deletes.keySet()); modifiedEntityTypes.addAll(bulkOperations.keySet()); for (Class<?> type : modifiedEntityTypes) { // first perform entity UPDATES addSortedModificationsForType(type, updates.get(type), flush); // next perform entity DELETES addSortedModificationsForType(type, deletes.get(type), flush); // last perform bulk operations SortedSet<DbBulkOperation> bulkOperationsForType = bulkOperations.get(type); if(bulkOperationsForType != null) { flush.addAll(bulkOperationsForType); } } //the very last perform bulk operations for which the order is important if(bulkOperationsInsertionOrder != null) { flush.addAll(bulkOperationsInsertionOrder); } }
protected void flushDbOperationManager() { // obtain totally ordered operation list from operation manager List<DbOperation> operationsToFlush = dbOperationManager.calculateFlush(); if (operationsToFlush == null || operationsToFlush.size() == 0) { return; } LOG.databaseFlushSummary(operationsToFlush); // If we want to delete all table data as bulk operation, on tables which have self references, // We need to turn the foreign key check off on MySQL and MariaDB. // On other databases we have to do nothing, the mapped statement will be empty. if (isIgnoreForeignKeysForNextFlush) { persistenceSession.executeNonEmptyUpdateStmt(TOGGLE_FOREIGN_KEY_STMT, false); persistenceSession.flushOperations(); } try { final List<List<DbOperation>> batches = CollectionUtil.partition(operationsToFlush, BATCH_SIZE); for (List<DbOperation> batch : batches) { flushDbOperations(batch); } } finally { if (isIgnoreForeignKeysForNextFlush) { persistenceSession.executeNonEmptyUpdateStmt(TOGGLE_FOREIGN_KEY_STMT, true); persistenceSession.flushOperations(); isIgnoreForeignKeysForNextFlush = false; } } }
public boolean addOperation(DbEntityOperation newOperation) { if(newOperation.getOperationType() == INSERT) { return getInsertsForType(newOperation.getEntityType(), true) .add(newOperation); } else if(newOperation.getOperationType() == DELETE) { return getDeletesByType(newOperation.getEntityType(), true) .add(newOperation); } else { // UPDATE return getUpdatesByType(newOperation.getEntityType(), true) .add(newOperation); } }
public List<DbOperation> calculateFlush() { List<DbOperation> flush = new ArrayList<DbOperation>(); // first INSERTs addSortedInserts(flush); // then UPDATEs + DELETEs addSortedModifications(flush); return flush; }
/** Adds a correctly ordered list of UPDATE and DELETE operations to the flush. * @param flush */ protected void addSortedModifications(List<DbOperation> flush) { // calculate sorted set of all modified entity types SortedSet<Class<?>> modifiedEntityTypes = new TreeSet<Class<?>>(MODIFICATION_TYPE_COMPARATOR); modifiedEntityTypes.addAll(updates.keySet()); modifiedEntityTypes.addAll(deletes.keySet()); modifiedEntityTypes.addAll(bulkOperations.keySet()); for (Class<?> type : modifiedEntityTypes) { // first perform entity UPDATES addSortedModificationsForType(type, updates.get(type), flush); // next perform entity DELETES addSortedModificationsForType(type, deletes.get(type), flush); // last perform bulk operations SortedSet<DbBulkOperation> bulkOperationsForType = bulkOperations.get(type); if(bulkOperationsForType != null) { flush.addAll(bulkOperationsForType); } } //the very last perform bulk operations for which the order is important if(bulkOperationsInsertionOrder != null) { flush.addAll(bulkOperationsInsertionOrder); } }
@Test public void testInsertSingleEntity() { entityManager.insert(execution1); entityManager.flushEntityCache(); List<DbOperation> flush = entityManager.getDbOperationManager().calculateFlush(); assertEquals(1, flush.size()); }
public boolean addOperation(DbEntityOperation newOperation) { if(newOperation.getOperationType() == INSERT) { return getInsertsForType(newOperation.getEntityType(), true) .add(newOperation); } else if(newOperation.getOperationType() == DELETE) { return getDeletesByType(newOperation.getEntityType(), true) .add(newOperation); } else { // UPDATE return getUpdatesByType(newOperation.getEntityType(), true) .add(newOperation); } }
public List<DbOperation> calculateFlush() { List<DbOperation> flush = new ArrayList<DbOperation>(); // first INSERTs addSortedInserts(flush); // then UPDATEs + DELETEs addSortedModifications(flush); return flush; }
protected DbBulkOperation performBulkOperation(Class<? extends DbEntity> entityType, String statement, Object parameter, DbOperationType operationType) { // create operation DbBulkOperation bulkOperation = createDbBulkOperation(entityType, statement, parameter, operationType); // schedule operation dbOperationManager.addOperation(bulkOperation); return bulkOperation; }
protected DbBulkOperation performBulkOperationPreserveOrder(Class<? extends DbEntity> entityType, String statement, Object parameter, DbOperationType operationType) { DbBulkOperation bulkOperation = createDbBulkOperation(entityType, statement, parameter, operationType); // schedule operation dbOperationManager.addOperationPreserveOrder(bulkOperation); return bulkOperation; }
protected void initializeOperationManager() { dbOperationManager = new DbOperationManager(); }
/** Adds a correctly ordered list of UPDATE and DELETE operations to the flush. * @param flush */ protected void addSortedModifications(List<DbOperation> flush) { // calculate sorted set of all modified entity types SortedSet<Class<?>> modifiedEntityTypes = new TreeSet<Class<?>>(MODIFICATION_TYPE_COMPARATOR); modifiedEntityTypes.addAll(updates.keySet()); modifiedEntityTypes.addAll(deletes.keySet()); modifiedEntityTypes.addAll(bulkOperations.keySet()); for (Class<?> type : modifiedEntityTypes) { // first perform entity UPDATES addSortedModificationsForType(type, updates.get(type), flush); // next perform entity DELETES addSortedModificationsForType(type, deletes.get(type), flush); // last perform bulk operations SortedSet<DbBulkOperation> bulkOperationsForType = bulkOperations.get(type); if(bulkOperationsForType != null) { flush.addAll(bulkOperationsForType); } } //the very last perform bulk operations for which the order is important if(bulkOperationsInsertionOrder != null) { flush.addAll(bulkOperationsInsertionOrder); } }
@Test public void testInsertIdOrdering() { entityManager.insert(execution1); entityManager.insert(execution2); entityManager.flushEntityCache(); List<DbOperation> insertOperations = entityManager.getDbOperationManager().calculateFlush(); assertHappensAfter(execution2, execution1, insertOperations); }
protected void performEntityOperation(CachedDbEntity cachedDbEntity, DbOperationType type) { DbEntityOperation dbOperation = new DbEntityOperation(); dbOperation.setEntity(cachedDbEntity.getEntity()); dbOperation.setFlushRelevantEntityReferences(cachedDbEntity.getFlushRelevantEntityReferences()); dbOperation.setOperationType(type); dbOperationManager.addOperation(dbOperation); }