Skip to content

Commit

Permalink
Flush database changes after switching to READONLY mode
Browse files Browse the repository at this point in the history
  • Loading branch information
toniprieto committed Oct 13, 2023
1 parent c33d3fa commit 00a6531
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
19 changes: 9 additions & 10 deletions dspace-api/src/main/java/org/dspace/core/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,15 @@ public void setMode(Mode newMode) {
readOnlyCache.clear();
}

// When going to READ_ONLY, flush database changes to ensure that the current data is retrieved
if (newMode == Mode.READ_ONLY && mode != Mode.READ_ONLY) {
try {
dbConnection.flushSession();
} catch (SQLException ex) {
log.warn("Unable to flush database changes after switching to READ_ONLY mode", ex);
}
}

//save the new mode
mode = newMode;
}
Expand Down Expand Up @@ -880,16 +889,6 @@ public <E extends ReloadableEntity> void uncacheEntity(E entity) throws SQLExcep
dbConnection.uncacheEntity(entity);
}

/**
* Flush the current Session to synchronizes the in-memory state of the Session
* with the database (write changes to the database)
*
* @throws SQLException passed through.
*/
public void flushDBChanges() throws SQLException {
dbConnection.flushSession();
}

public Boolean getCachedAuthorizationResult(DSpaceObject dspaceObject, int action, EPerson eperson) {
if (isReadOnly()) {
return readOnlyCache.getCachedAuthorizationResult(dspaceObject, action, eperson);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,6 @@ public void consume(Context ctx, Event event) throws Exception {
public void end(Context ctx) throws Exception {

// Change the mode to readonly to improve performance
// First, we flush the changes to database, if session is dirty, has pending changes
// to synchronize with database, without this flush it could index an old version of
// the object
ctx.flushDBChanges();
Context.Mode originalMode = ctx.getCurrentMode();
ctx.setMode(Context.Mode.READ_ONLY);

Expand Down

0 comments on commit 00a6531

Please sign in to comment.