diff --git a/dspace-api/src/main/java/org/dspace/core/Context.java b/dspace-api/src/main/java/org/dspace/core/Context.java index 602d3917b3..a5e5f15d45 100644 --- a/dspace-api/src/main/java/org/dspace/core/Context.java +++ b/dspace-api/src/main/java/org/dspace/core/Context.java @@ -665,28 +665,28 @@ public class Context * READ_WRITE: This is the default mode and enables the normal database behaviour. This behaviour is optimal for querying and updating a * small number of records. * - * @param mode The mode to put this context in + * @param newMode The mode to put this context in * @throws SQLException When configuring the database connection fails. */ - public void setMode(Mode mode) throws SQLException { - - switch (mode) { + public void setMode(Mode newMode) throws SQLException { + //update the database settings + switch (newMode) { case BATCH_EDIT: - dbConnection.setOptimizedForBatchProcessing(true); - dbConnection.setReadOnly(false); + dbConnection.setConnectionMode(true, false); break; case READ_ONLY: - dbConnection.setReadOnly(true); - dbConnection.setOptimizedForBatchProcessing(false); + dbConnection.setConnectionMode(false, true); break; case READ_WRITE: - dbConnection.setOptimizedForBatchProcessing(false); - dbConnection.setReadOnly(false); + dbConnection.setConnectionMode(false, false); break; default: log.warn("New context mode detected that has nog been configured."); break; } + + //save the new mode + this.mode = newMode; } /** @@ -697,6 +697,36 @@ public class Context return mode; } + /** + * Enable or disable "batch processing mode" for this context. + * + * Enabling batch processing mode means that the database connection is configured so that it is optimized to + * process a large number of records. + * + * Disabling batch processing mode restores the normal behaviour that is optimal for querying and updating a + * small number of records. + * + * @param batchModeEnabled When true, batch processing mode will be enabled. If false, it will be disabled. + * @throws SQLException When configuring the database connection fails. + */ + @Deprecated + public void enableBatchMode(boolean batchModeEnabled) throws SQLException { + if(batchModeEnabled) { + setMode(Mode.BATCH_EDIT); + } else { + setMode(Mode.READ_WRITE); + } + } + + /** + * Check if "batch processing mode" is enabled for this context. + * @return True if batch processing mode is enabled, false otherwise. + */ + @Deprecated + public boolean isBatchModeEnabled() { + return mode != null && mode == Mode.BATCH_EDIT; + } + /** * Reload an entity from the database into the cache. This method will return a reference to the "attached" * entity. This means changes to the entity will be tracked and persisted to the database. diff --git a/dspace-api/src/main/java/org/dspace/core/DBConnection.java b/dspace-api/src/main/java/org/dspace/core/DBConnection.java index 77fcc3fbee..019b6c67eb 100644 --- a/dspace-api/src/main/java/org/dspace/core/DBConnection.java +++ b/dspace-api/src/main/java/org/dspace/core/DBConnection.java @@ -40,12 +40,10 @@ public interface DBConnection { public DatabaseConfigVO getDatabaseConfig() throws SQLException; - public void setOptimizedForBatchProcessing(boolean batchOptimized) throws SQLException; + public void setConnectionMode(boolean batchOptimized, boolean readOnlyOptimized) throws SQLException; public boolean isOptimizedForBatchProcessing(); - public void setReadOnly(boolean readOnlyOptimized) throws SQLException; - public long getCacheSize() throws SQLException; /** diff --git a/dspace-api/src/main/java/org/dspace/core/HibernateDBConnection.java b/dspace-api/src/main/java/org/dspace/core/HibernateDBConnection.java index 8085e81e4b..54d9ee2a30 100644 --- a/dspace-api/src/main/java/org/dspace/core/HibernateDBConnection.java +++ b/dspace-api/src/main/java/org/dspace/core/HibernateDBConnection.java @@ -134,8 +134,9 @@ public class HibernateDBConnection implements DBConnection { } @Override - public void setOptimizedForBatchProcessing(final boolean batchOptimized) throws SQLException { + public void setConnectionMode(final boolean batchOptimized, final boolean readOnlyOptimized) throws SQLException { this.batchModeEnabled = batchOptimized; + this.readOnlyEnabled = readOnlyOptimized; configureDatabaseMode(); } @@ -144,12 +145,6 @@ public class HibernateDBConnection implements DBConnection { return batchModeEnabled; } - @Override - public void setReadOnly(boolean readOnlyOptimized) throws SQLException { - this.readOnlyEnabled = readOnlyOptimized; - configureDatabaseMode(); - } - private void configureDatabaseMode() throws SQLException { if(batchModeEnabled) { getSession().setFlushMode(FlushMode.ALWAYS); diff --git a/dspace-api/src/test/java/org/dspace/core/ContextTest.java b/dspace-api/src/test/java/org/dspace/core/ContextTest.java index 30679e6ea6..00cade17df 100644 --- a/dspace-api/src/test/java/org/dspace/core/ContextTest.java +++ b/dspace-api/src/test/java/org/dspace/core/ContextTest.java @@ -317,6 +317,30 @@ public class ContextTest extends AbstractUnitTest cleanupContext(instance); } + /** + * Test that commit cannot be called when the context is in read-only mode + */ + @Test + public void testIsReadOnlyCommit() throws SQLException + { + // Create a new read-only context + Context instance = new Context(Context.Mode.READ_ONLY); + assertThat("testIsReadOnly 1", instance.isReadOnly(), equalTo(true)); + + try { + //When in read-only, calling commit() should result in an error + instance.commit(); + fail(); + } catch (Exception ex) { + assertTrue(ex instanceof UnsupportedOperationException); + } + + instance.abort(); + + // Cleanup our context + cleanupContext(instance); + } + /** * Test of getCacheSize method, of class Context. */