Add functions to do a manual flush of the db session and call flush before change to READ_ONLY mode to be sure we index the current object

This commit is contained in:
Toni Prieto
2023-07-28 09:19:37 +02:00
parent 94822b50af
commit c33d3fa87d
4 changed files with 38 additions and 3 deletions

View File

@@ -880,6 +880,16 @@ public class Context implements AutoCloseable {
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);

View File

@@ -148,4 +148,12 @@ public interface DBConnection<T> {
* @throws java.sql.SQLException passed through.
*/
public <E extends ReloadableEntity> void uncacheEntity(E entity) throws SQLException;
/**
* Do a manual flush. This synchronizes the in-memory state of the Session
* with the database (write changes to the database)
*
* @throws SQLException passed through.
*/
public void flushSession() throws SQLException;
}

View File

@@ -337,4 +337,17 @@ public class HibernateDBConnection implements DBConnection<Session> {
}
}
}
/**
* Do a manual flush. This synchronizes the in-memory state of the Session
* with the database (write changes to the database)
*
* @throws SQLException passed through.
*/
@Override
public void flushSession() throws SQLException {
if (getSession().isDirty()) {
getSession().flush();
}
}
}

View File

@@ -201,7 +201,11 @@ public class IndexEventConsumer implements Consumer {
@Override
public void end(Context ctx) throws Exception {
// Change the mode to readonly to improve the performance
// 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);
@@ -234,10 +238,10 @@ public class IndexEventConsumer implements Consumer {
uniqueIdsToDelete.clear();
createdItemsToUpdate.clear();
}
}
ctx.setMode(originalMode);
}
}
private void indexObject(Context ctx, IndexableObject iu, boolean preDb) throws SQLException {
/* we let all types through here and