Small refactorings + backwards compatibility

This commit is contained in:
Tom Desair
2017-04-05 11:02:58 +02:00
parent 7719848d47
commit 1e917ed845
4 changed files with 67 additions and 20 deletions

View File

@@ -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.

View File

@@ -40,12 +40,10 @@ public interface DBConnection<T> {
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;
/**

View File

@@ -134,8 +134,9 @@ public class HibernateDBConnection implements DBConnection<Session> {
}
@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<Session> {
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);

View File

@@ -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.
*/