|
1 | 1 | /* |
2 | | - * Copyright 2002-2014 the original author or authors. |
| 2 | + * Copyright 2002-2015 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
@@ -221,20 +221,23 @@ public static EntityManager doGetTransactionalEntityManager( |
221 | 221 | (EntityManagerHolder) TransactionSynchronizationManager.getResource(emf); |
222 | 222 | if (emHolder != null) { |
223 | 223 | if (synchronizedWithTransaction) { |
224 | | - if (!emHolder.isSynchronizedWithTransaction() && |
225 | | - TransactionSynchronizationManager.isSynchronizationActive()) { |
226 | | - // Try to explicitly synchronize the EntityManager itself |
227 | | - // with an ongoing JTA transaction, if any. |
228 | | - try { |
229 | | - emHolder.getEntityManager().joinTransaction(); |
| 224 | + if (!emHolder.isSynchronizedWithTransaction()) { |
| 225 | + if (TransactionSynchronizationManager.isActualTransactionActive()) { |
| 226 | + // Try to explicitly synchronize the EntityManager itself |
| 227 | + // with an ongoing JTA transaction, if any. |
| 228 | + try { |
| 229 | + emHolder.getEntityManager().joinTransaction(); |
| 230 | + } |
| 231 | + catch (TransactionRequiredException ex) { |
| 232 | + logger.debug("Could not join transaction because none was actually active", ex); |
| 233 | + } |
230 | 234 | } |
231 | | - catch (TransactionRequiredException ex) { |
232 | | - logger.debug("Could not join transaction because none was actually active", ex); |
| 235 | + if (TransactionSynchronizationManager.isSynchronizationActive()) { |
| 236 | + Object transactionData = prepareTransaction(emHolder.getEntityManager(), emf); |
| 237 | + TransactionSynchronizationManager.registerSynchronization( |
| 238 | + new TransactionalEntityManagerSynchronization(emHolder, emf, transactionData, false)); |
| 239 | + emHolder.setSynchronizedWithTransaction(true); |
233 | 240 | } |
234 | | - Object transactionData = prepareTransaction(emHolder.getEntityManager(), emf); |
235 | | - TransactionSynchronizationManager.registerSynchronization( |
236 | | - new TransactionalEntityManagerSynchronization(emHolder, emf, transactionData, false)); |
237 | | - emHolder.setSynchronizedWithTransaction(true); |
238 | 241 | } |
239 | 242 | // Use holder's reference count to track synchronizedWithTransaction access. |
240 | 243 | // isOpen() check used below to find out about it. |
|
0 commit comments