@PostConstruct public void init() { if (getTransactionManager() instanceof JpaTransactionManager) { em = ((JpaTransactionManager) getTransactionManager()).getEntityManagerFactory().createEntityManager(); } }
@Override public <G extends Throwable> void runTransactionalOperation(StreamCapableTransactionalOperation operation, Class<G> exceptionType) throws G { runOptionalTransactionalOperation(operation, exceptionType, true, TransactionDefinition.PROPAGATION_REQUIRED, TransactionDefinition.ISOLATION_DEFAULT, false, getTransactionManager()); }
@Override public <G extends Throwable> void runOptionalTransactionalOperation(StreamCapableTransactionalOperation operation, Class<G> exceptionType, boolean useTransaction, int transactionBehavior, int isolationLevel) throws G { runOptionalTransactionalOperation(operation, exceptionType, useTransaction, transactionBehavior, isolationLevel, false, getTransactionManager()); }
@Override public <G extends Throwable> void runTransactionalOperation(StreamCapableTransactionalOperation operation, Class<G> exceptionType, int transactionBehavior, int isolationLevel) throws G { runOptionalTransactionalOperation(operation, exceptionType, true, transactionBehavior, isolationLevel, false, getTransactionManager()); }
@Override public <G extends Throwable> void runOptionalTransactionalOperation(StreamCapableTransactionalOperation operation, Class<G> exceptionType, boolean useTransaction) throws G { runOptionalTransactionalOperation(operation, exceptionType, useTransaction, TransactionDefinition.PROPAGATION_REQUIRED, TransactionDefinition.ISOLATION_DEFAULT, false, getTransactionManager()); }
protected Site retrieveSiteByIdentifier(final String identifier, final boolean persistentResult) { //Since the methods on this class are frequently called during regular page requests and transactions are expensive, //only run the operation under a transaction if there is not already an entity manager in the view if (identifier == null) { return null; } final Site[] response = new Site[1]; transUtil.runOptionalTransactionalOperation(new StreamCapableTransactionalOperationAdapter() { @Override public void execute() throws Throwable { Site site = siteDao.retrieveSiteByIdentifier(identifier); if (persistentResult) { response[0] = site; } else { response[0] = getNonPersistentSite(site); } } }, RuntimeException.class, !TransactionSynchronizationManager.hasResource(((JpaTransactionManager) transUtil.getTransactionManager()).getEntityManagerFactory())); return response[0]; }
@Override public void runOptionalEntityManagerInViewOperation(Runnable runnable) { EntityManagerFactory emf = ((JpaTransactionManager) getTransactionManager()).getEntityManagerFactory(); boolean isEntityManagerInView = TransactionSynchronizationManager.hasResource(emf); try { if (!isEntityManagerInView) { EntityManager em = emf.createEntityManager(); EntityManagerHolder emHolder = new EntityManagerHolder(em); TransactionSynchronizationManager.bindResource(emf, emHolder); } runnable.run(); } finally { if (!isEntityManagerInView) { EntityManagerHolder emHolder = (EntityManagerHolder) TransactionSynchronizationManager.unbindResource(emf); EntityManagerFactoryUtils.closeEntityManager(emHolder.getEntityManager()); } } }
runOptionalTransactionalOperation(operation, exceptionType, true, transactionBehavior, isolationLevel, false, getTransactionManager()); if (em != null) {
EntityManagerFactory emf = ((JpaTransactionManager) transUtil.getTransactionManager()).getEntityManagerFactory(); boolean isEntityManagerInView = TransactionSynchronizationManager.hasResource(emf); if (!isEntityManagerInView) {
/** * Complete the scope of the EntityManager-In-View operation. See {@link #startView(Long, String)}. */ public void endView() { EntityManagerFactory emf = ((JpaTransactionManager) transUtil.getTransactionManager()).getEntityManagerFactory(); boolean isEntityManagerInView = TransactionSynchronizationManager.hasResource(emf); if (isEntityManagerInView) { Stack<Integer> stack = (Stack<Integer>) TransactionSynchronizationManager.getResource("emStack"); if (stack.size() <= 1) { EntityManagerHolder emHolder = (EntityManagerHolder) TransactionSynchronizationManager.unbindResource(emf); EntityManagerFactoryUtils.closeEntityManager(emHolder.getEntityManager()); TransactionSynchronizationManager.unbindResource("emStack"); ThreadLocalManager.remove(); } else { stack.pop(); } } }
private SandBox findSandBox(Long siteId, String sandBoxName, boolean initialize) { if (initialize) { startView(siteId, sandBoxName); } try { if (DEFAULT_SB.equals(sandBoxName)) { return getDefaultSandBox(siteId); } else { EntityManagerFactory emf = ((JpaTransactionManager) transUtil.getTransactionManager()).getEntityManagerFactory(); EntityManagerHolder emHolder = (EntityManagerHolder) TransactionSynchronizationManager.getResource(emf); return findSandBox(sandBoxName, emHolder.getEntityManager()); } } finally { if (initialize) { endView(); } } }