Skip to content

Commit

Permalink
Fix #1240: SessionEventListener: add pre/post flush() events (#1992)
Browse files Browse the repository at this point in the history
Fixes #1240 : SessionEventListener: add pre/post flush() events
- added preFlushUnitOfWork/postFlushUnitOfWork events
- removed preCommitUnitOfWork event from flush in UnitOfWorkImpl
- added unitTests for flush events

Signed-off-by: Patrick Schmitt <[email protected]>
  • Loading branch information
Zuplyx authored Nov 8, 2023
1 parent 01fa76f commit 9117bb2
Show file tree
Hide file tree
Showing 13 changed files with 227 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,17 @@ public void postCommitUnitOfWork(SessionEvent event) {
log(event);
}

/**
* PUBLIC: This event is raised on the unit of work after a flush.
*/
@Override
public void postFlushUnitOfWork(SessionEvent event) {
if (!isTrackingEvent(event)) {
return;
}
log(event);
}

/**
* PUBLIC:
* This event is raised after the session connects to the database.
Expand Down Expand Up @@ -543,6 +554,17 @@ public void preCommitUnitOfWork(SessionEvent event) {
log(event);
}

/**
* PUBLIC: This event is raised on the unit of work before a flush.
*/
@Override
public void preFlushUnitOfWork(SessionEvent event) {
if (!isTrackingEvent(event)) {
return;
}
log(event);
}

/**
* PUBLIC:
* This event is raised before the execution of every query against the session.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -40,6 +40,7 @@ public void test() {
UnitOfWork uow = getSession().acquireUnitOfWork();
Employee emp = (Employee)uow.readObject(Employee.class);
emp.setFirstName(emp.getFirstName() + "-modified");
uow.writeChanges();
uow.commit();
}

Expand All @@ -56,5 +57,11 @@ public void verify() {
if (this.listener.postAcquireClientSession) {
throw new TestErrorException("The post acquire event was not raised but should not have been.");
}
if(!this.listener.preFlushUnitOfWork) {
throw new TestErrorException("The pre UnitOfWork flush event did not fire");
}
if(!this.listener.postFlushUnitOfWork) {
throw new TestErrorException("The post UnitOfWork flush event did not fire");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand All @@ -22,6 +22,18 @@ public class TestSessionListener extends SessionEventAdapter {
public boolean preBeginTransaction;
public boolean postCalculateUnitOfWork;
public boolean preCalculateUnitOfWork;
public boolean preFlushUnitOfWork;
public boolean postFlushUnitOfWork;

@Override
public void postFlushUnitOfWork(SessionEvent event) {
postFlushUnitOfWork = true;
}

@Override
public void preFlushUnitOfWork(SessionEvent event) {
preFlushUnitOfWork = true;
}

/**
* TestSessionListener constructor comment.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/
package org.eclipse.persistence.testing.tests.unitofwork;

import org.eclipse.persistence.sessions.SessionEvent;
import org.eclipse.persistence.sessions.SessionEventAdapter;
import org.eclipse.persistence.sessions.UnitOfWork;
import org.eclipse.persistence.testing.framework.TestErrorException;

public class PostFlushUnitOfWorkTest extends UnitOfWorkEventTest {
@Override
public void setup() {
super.setup();
setDescription("Test the postFlushUnitOfWork Event");
SessionEventAdapter tvAdapter = new SessionEventAdapter() {
// Listen for PostFlushUnitOfWorkEvents

@Override
public void postFlushUnitOfWork(SessionEvent event) {
setEventTriggered(true);
}
};
getSession().getEventManager().addListener(tvAdapter);
}

@Override
public void test() {
UnitOfWork tvUnitOfWork = getSession().acquireUnitOfWork();
tvUnitOfWork.writeChanges();
}

@Override
public void verify() {
if (!isEventTriggered()) {
throw new TestErrorException("The preFlushUnitOfWork event was not triggered.");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/
package org.eclipse.persistence.testing.tests.unitofwork;

import org.eclipse.persistence.sessions.SessionEvent;
import org.eclipse.persistence.sessions.SessionEventAdapter;
import org.eclipse.persistence.sessions.UnitOfWork;
import org.eclipse.persistence.testing.framework.TestErrorException;

public class PreFlushUnitOfWorkTest extends UnitOfWorkEventTest {
@Override
public void setup() {
super.setup();
setDescription("Test the preFlushUnitOfWork Event");
SessionEventAdapter tvAdapter = new SessionEventAdapter() {
// Listen for PreFlushUnitOfWorkEvents

@Override
public void preFlushUnitOfWork(SessionEvent event) {
setEventTriggered(true);
}
};
getSession().getEventManager().addListener(tvAdapter);
}

@Override
public void test() {
UnitOfWork tvUnitOfWork = getSession().acquireUnitOfWork();
tvUnitOfWork.writeChanges();
}

@Override
public void verify() {
if (!isEventTriggered()) {
throw new TestErrorException("The preFlushUnitOfWork event was not triggered.");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -28,6 +28,12 @@ public void addTests() {
// PreCommitUnitOfWork
addTest(new PreCommitUnitOfWorkTest());

// PreFlushUnitOfWork
addTest(new PreFlushUnitOfWorkTest());

// PostFlushUnitOfWork
addTest(new PostFlushUnitOfWorkTest());

// PrepareUnitOfWork
addTest(new PrepareUnitOfWorkTest());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -427,6 +427,9 @@ public void writeChanges() {
return;
}
log(SessionLog.FINER, SessionLog.TRANSACTION, "begin_unit_of_work_flush");
if(eventManager != null) {
eventManager.preFlushUnitOfWork();
}

// 256277: stop any nested flushing - there should only be one level
this.isWithinFlush = true; // set before calculateChanges as a PrePersist callback may contain a query that requires a pre flush()
Expand All @@ -451,6 +454,9 @@ public void writeChanges() {
writesCompleted();
//return if there were no changes in the change set.
log(SessionLog.FINER, SessionLog.TRANSACTION, "end_unit_of_work_flush");
if(eventManager != null) {
eventManager.postFlushUnitOfWork();
}
return;
}
// Write changes to the database.
Expand All @@ -471,6 +477,9 @@ public void writeChanges() {
this.cumulativeUOWChangeSet.mergeUnitOfWorkChangeSet(changeSet, this, true);
}
log(SessionLog.FINER, SessionLog.TRANSACTION, "end_unit_of_work_flush");
if(eventManager != null) {
eventManager.postFlushUnitOfWork();
}

resumeUnitOfWork();
log(SessionLog.FINER, SessionLog.TRANSACTION, "resume_unit_of_work");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
import java.util.Map;
import java.util.Set;
import java.util.Vector;
import java.util.stream.Collectors;

import org.eclipse.persistence.annotations.CacheKeyType;
import org.eclipse.persistence.config.ReferenceMode;
Expand Down Expand Up @@ -5865,10 +5864,10 @@ public void writeChanges() {
throw ValidationException.writeChangesOnNestedUnitOfWork();
}
log(SessionLog.FINER, SessionLog.TRANSACTION, "begin_unit_of_work_flush");
mergeBmpAndWsEntities();
if (this.eventManager != null) {
this.eventManager.preCommitUnitOfWork();
if(eventManager != null) {
eventManager.preFlushUnitOfWork();
}
mergeBmpAndWsEntities();
setLifecycle(CommitPending);
try {
commitToDatabaseWithChangeSet(false);
Expand All @@ -5880,6 +5879,9 @@ public void writeChanges() {
}
setLifecycle(CommitTransactionPending);
log(SessionLog.FINER, SessionLog.TRANSACTION, "end_unit_of_work_flush");
if(eventManager != null) {
eventManager.postFlushUnitOfWork();
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ public void writeChanges() {
return;
}
log(SessionLog.FINER, SessionLog.TRANSACTION, "begin_unit_of_work_flush");
if(eventManager != null) {
eventManager.preFlushUnitOfWork();
}

// PERF: If this is an empty unit of work, do nothing (but still may need to commit SQL changes).
boolean hasChanges = (this.unitOfWorkChangeSet != null) || hasCloneMapping() || hasDeletedObjects() || hasModifyAllQueries() || hasDeferredModifyAllQueries();
Expand All @@ -239,6 +242,9 @@ public void writeChanges() {
}
if (!hasChanges) {
log(SessionLog.FINER, SessionLog.TRANSACTION, "end_unit_of_work_flush");
if(eventManager != null) {
eventManager.postFlushUnitOfWork();
}
return;
}

Expand Down Expand Up @@ -269,6 +275,9 @@ public void writeChanges() {
remoteUnitOfWork.commitIntoRemoteUnitOfWork();

log(SessionLog.FINER, SessionLog.TRANSACTION, "end_unit_of_work_flush");
if(eventManager != null) {
eventManager.postFlushUnitOfWork();
}

resumeUnitOfWork();
log(SessionLog.FINER, SessionLog.TRANSACTION, "resume_unit_of_work");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2023 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021 IBM Corporation. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand Down Expand Up @@ -66,6 +66,10 @@ public class SessionEvent extends EventObject {
public static final int PrepareUnitOfWork = 14;
public static final int PostResumeUnitOfWork = 15;

public static final int PreFlushUnitOfWork = 38;

public static final int PostFlushUnitOfWork = 39;

// Three-tier events, only raised on server/client session.
public static final int PostAcquireClientSession = 16;
public static final int PreReleaseClientSession = 17;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2023 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021 IBM Corporation. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand Down Expand Up @@ -65,6 +65,9 @@ public void postCommitTransaction(SessionEvent event) { }
@Override
public void postCommitUnitOfWork(SessionEvent event) { }

@Override
public void postFlushUnitOfWork(SessionEvent event) { }

@Override
public void postDistributedMergeUnitOfWorkChangeSet(SessionEvent event) { }

Expand Down Expand Up @@ -102,6 +105,9 @@ public void preCommitTransaction(SessionEvent event) { }
@Override
public void preCommitUnitOfWork(SessionEvent event) { }

@Override
public void preFlushUnitOfWork(SessionEvent event) { }

@Override
public void preExecuteCall(SessionEvent event) { }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2023 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021 IBM Corporation. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand Down Expand Up @@ -124,6 +124,11 @@ public interface SessionEventListener extends CoreSessionEventListener {
*/
public void postCommitUnitOfWork(SessionEvent event);

/**
* PUBLIC: This event is raised on the unit of work after a flush.
*/
void postFlushUnitOfWork(SessionEvent event);

/**
* PUBLIC:
* This event is raised after the session connects to the database.
Expand Down Expand Up @@ -207,6 +212,11 @@ public interface SessionEventListener extends CoreSessionEventListener {
*/
public void preCommitUnitOfWork(SessionEvent event);

/**
* PUBLIC: This event is raised on the unit of work before a flush.
*/
void preFlushUnitOfWork(SessionEvent event);

/**
* PUBLIC:
* This event is raised before the execution of every call against the session.
Expand Down
Loading

0 comments on commit 9117bb2

Please sign in to comment.