From 19b4618fb5eca3a9212caafecb1e7ad85ce78bd4 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 22 Mar 2010 14:23:02 +0000 Subject: [PATCH] avoid Synchronization List preparations upfront if possible (SPR-6999) --- .../jta/WebSphereUowTransactionManager.java | 4 +++- .../AbstractPlatformTransactionManager.java | 4 ++-- .../TransactionSynchronizationManager.java | 20 ++++++++++--------- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/org.springframework.transaction/src/main/java/org/springframework/transaction/jta/WebSphereUowTransactionManager.java b/org.springframework.transaction/src/main/java/org/springframework/transaction/jta/WebSphereUowTransactionManager.java index c5fbafa5376..e2349a4f56c 100644 --- a/org.springframework.transaction/src/main/java/org/springframework/transaction/jta/WebSphereUowTransactionManager.java +++ b/org.springframework.transaction/src/main/java/org/springframework/transaction/jta/WebSphereUowTransactionManager.java @@ -352,7 +352,9 @@ public class WebSphereUowTransactionManager extends JtaTransactionManager if (status.isNewSynchronization()) { List synchronizations = TransactionSynchronizationManager.getSynchronizations(); TransactionSynchronizationManager.clear(); - uowManager.registerInterposedSynchronization(new JtaAfterCompletionSynchronization(synchronizations)); + if (!synchronizations.isEmpty()) { + uowManager.registerInterposedSynchronization(new JtaAfterCompletionSynchronization(synchronizations)); + } } } } diff --git a/org.springframework.transaction/src/main/java/org/springframework/transaction/support/AbstractPlatformTransactionManager.java b/org.springframework.transaction/src/main/java/org/springframework/transaction/support/AbstractPlatformTransactionManager.java index f0f0a1724e6..ee147383c13 100644 --- a/org.springframework.transaction/src/main/java/org/springframework/transaction/support/AbstractPlatformTransactionManager.java +++ b/org.springframework.transaction/src/main/java/org/springframework/transaction/support/AbstractPlatformTransactionManager.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2010 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -970,7 +970,7 @@ public abstract class AbstractPlatformTransactionManager implements PlatformTran // invoke the afterCompletion callbacks immediately invokeAfterCompletion(synchronizations, completionStatus); } - else { + else if (!synchronizations.isEmpty()) { // Existing transaction that we participate in, controlled outside // of the scope of this Spring transaction manager -> try to register // an afterCompletion callback with the existing (JTA) transaction. diff --git a/org.springframework.transaction/src/main/java/org/springframework/transaction/support/TransactionSynchronizationManager.java b/org.springframework.transaction/src/main/java/org/springframework/transaction/support/TransactionSynchronizationManager.java index 392a0042a62..e68398389fa 100644 --- a/org.springframework.transaction/src/main/java/org/springframework/transaction/support/TransactionSynchronizationManager.java +++ b/org.springframework.transaction/src/main/java/org/springframework/transaction/support/TransactionSynchronizationManager.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2010 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -287,16 +287,21 @@ public abstract class TransactionSynchronizationManager { * @see TransactionSynchronization */ public static List getSynchronizations() throws IllegalStateException { - if (!isSynchronizationActive()) { + List synchs = synchronizations.get(); + if (synchs == null) { throw new IllegalStateException("Transaction synchronization is not active"); } - List synchs = synchronizations.get(); - // Sort lazily here, not in registerSynchronization. - OrderComparator.sort(synchs); // Return unmodifiable snapshot, to avoid ConcurrentModificationExceptions // while iterating and invoking synchronization callbacks that in turn // might register further synchronizations. - return Collections.unmodifiableList(new ArrayList(synchs)); + if (synchs.isEmpty()) { + return Collections.emptyList(); + } + else { + // Sort lazily here, not in registerSynchronization. + OrderComparator.sort(synchs); + return Collections.unmodifiableList(new ArrayList(synchs)); + } } /** @@ -359,9 +364,6 @@ public abstract class TransactionSynchronizationManager { * flush mode of a Hibernate Session to "FlushMode.NEVER" upfront. * @see org.springframework.transaction.TransactionDefinition#isReadOnly() * @see TransactionSynchronization#beforeCommit(boolean) - * @see org.hibernate.Session#flush - * @see org.hibernate.Session#setFlushMode - * @see org.hibernate.FlushMode#NEVER */ public static boolean isCurrentTransactionReadOnly() { return (currentTransactionReadOnly.get() != null);