Skip to content

Commit c33d3fa

Browse files
committed
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
1 parent 94822b5 commit c33d3fa

4 files changed

Lines changed: 38 additions & 3 deletions

File tree

dspace-api/src/main/java/org/dspace/core/Context.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,16 @@ public <E extends ReloadableEntity> void uncacheEntity(E entity) throws SQLExcep
880880
dbConnection.uncacheEntity(entity);
881881
}
882882

883+
/**
884+
* Flush the current Session to synchronizes the in-memory state of the Session
885+
* with the database (write changes to the database)
886+
*
887+
* @throws SQLException passed through.
888+
*/
889+
public void flushDBChanges() throws SQLException {
890+
dbConnection.flushSession();
891+
}
892+
883893
public Boolean getCachedAuthorizationResult(DSpaceObject dspaceObject, int action, EPerson eperson) {
884894
if (isReadOnly()) {
885895
return readOnlyCache.getCachedAuthorizationResult(dspaceObject, action, eperson);

dspace-api/src/main/java/org/dspace/core/DBConnection.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,12 @@ public interface DBConnection<T> {
148148
* @throws java.sql.SQLException passed through.
149149
*/
150150
public <E extends ReloadableEntity> void uncacheEntity(E entity) throws SQLException;
151+
152+
/**
153+
* Do a manual flush. This synchronizes the in-memory state of the Session
154+
* with the database (write changes to the database)
155+
*
156+
* @throws SQLException passed through.
157+
*/
158+
public void flushSession() throws SQLException;
151159
}

dspace-api/src/main/java/org/dspace/core/HibernateDBConnection.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,4 +337,17 @@ public <E extends ReloadableEntity> void uncacheEntity(E entity) throws SQLExcep
337337
}
338338
}
339339
}
340+
341+
/**
342+
* Do a manual flush. This synchronizes the in-memory state of the Session
343+
* with the database (write changes to the database)
344+
*
345+
* @throws SQLException passed through.
346+
*/
347+
@Override
348+
public void flushSession() throws SQLException {
349+
if (getSession().isDirty()) {
350+
getSession().flush();
351+
}
352+
}
340353
}

dspace-api/src/main/java/org/dspace/discovery/IndexEventConsumer.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,11 @@ public void consume(Context ctx, Event event) throws Exception {
201201
@Override
202202
public void end(Context ctx) throws Exception {
203203

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

@@ -234,9 +238,9 @@ public void end(Context ctx) throws Exception {
234238
uniqueIdsToDelete.clear();
235239
createdItemsToUpdate.clear();
236240
}
237-
}
238241

239-
ctx.setMode(originalMode);
242+
ctx.setMode(originalMode);
243+
}
240244
}
241245

242246
private void indexObject(Context ctx, IndexableObject iu, boolean preDb) throws SQLException {

0 commit comments

Comments
 (0)