Skip to content

Commit

Permalink
Fix #1240: SessionEventListener: add pre/post flush() events (#1978)
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 <Patrick.Schmitt@cpb-software.com>
  • Loading branch information
Zuplyx committed Nov 8, 2023
1 parent 8d67d25 commit 5db018c
Show file tree
Hide file tree
Showing 13 changed files with 224 additions and 7 deletions.
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.
*
* 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 @@ -43,6 +43,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 @@ -60,5 +61,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, 2021 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, 2021 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 @@ -29,6 +29,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
Expand Up @@ -414,6 +414,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 @@ -568,6 +579,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
Expand Up @@ -441,6 +441,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 @@ -465,6 +468,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 @@ -485,6 +491,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 @@ -5856,10 +5856,11 @@ public void writeChanges() {
throw ValidationException.writeChangesOnNestedUnitOfWork();
}
log(SessionLog.FINER, SessionLog.TRANSACTION, "begin_unit_of_work_flush");
mergeBmpAndWsEntities();
if (this.eventManager != null) {
this.eventManager.preCommitUnitOfWork();
this.eventManager.preFlushUnitOfWork();
}
mergeBmpAndWsEntities();

setLifecycle(CommitPending);
try {
commitToDatabaseWithChangeSet(false);
Expand All @@ -5871,6 +5872,9 @@ public void writeChanges() {
}
setLifecycle(CommitTransactionPending);
log(SessionLog.FINER, SessionLog.TRANSACTION, "end_unit_of_work_flush");
if (this.eventManager != null) {
this.eventManager.postFlushUnitOfWork();
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,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 @@ -238,6 +241,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 @@ -268,6 +274,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
Expand Up @@ -69,6 +69,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 @@ -101,6 +104,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 {
*/
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 {
*/
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

0 comments on commit 5db018c

Please sign in to comment.