@Test public void testSuccessfulExecutionConnectionClosed() throws SQLException { TransactionDefinition definition = TransactionDefinition.builder().isReadonly(true).build(); when(factory.createEntityManager()).thenReturn(entityManager); when(entityManager.unwrap(Session.class)).thenReturn(session); when(session.getHibernateFlushMode()).thenReturn(FlushMode.AUTO); when(session.getTransaction()).thenReturn(transaction); when(session.connection()).thenReturn(connection); DefaultExecution<Integer> execution = new DefaultExecution<>(definition, factory); Integer result = execution.execute(entityManager -> { when(session.connection()).thenReturn(null); return 1; }); assertEquals(result.intValue(), 1); verify(connection, times(1)).setReadOnly(true); verify(connection, times(0)).setReadOnly(false); when(session.connection()).thenReturn(connection); result = execution.execute(entityManager -> { when(session.isConnected()).thenReturn(false); return 1; }); assertEquals(result.intValue(), 1); verify(connection, times(2)).setReadOnly(true); verify(connection, times(0)).setReadOnly(false); }
@Override public T execute(@NonNull Function<EntityManager, T> function) throws SQLException { DefaultTransactionData data = null; EntityManager entityManager = sessionFactory.createEntityManager(); try { data = checkAndBeginTransaction(entityManager); T response = function.apply(entityManager); checkAndCommit(data, entityManager); entityManager.close(); return response; } catch (Exception e) { handleException(entityManager, data); throw e; } }
DefaultExecution implements Execution<T> { @Override public T execute(@NonNull Function<EntityManager, T> function) throws SQLException { DefaultTransactionData data = null; EntityManager entityManager = sessionFactory.createEntityManager(); try { data = checkAndBeginTransaction(entityManager); T response = function.apply(entityManager); checkAndCommit(data, entityManager); entityManager.close(); return response; } catch (Exception e) { handleException(entityManager, data); throw e; } } }
DefaultExecution implements Execution<T> { @Override public T execute(@NonNull Function<EntityManager, T> function) throws SQLException { DefaultTransactionData data = null; EntityManager entityManager = sessionFactory.createEntityManager(); try { data = checkAndBeginTransaction(entityManager); T response = function.apply(entityManager); checkAndCommit(data, entityManager); entityManager.close(); return response; } catch (Exception e) { handleException(entityManager, data); throw e; } }  }
DefaultExecution implements Execution<T> { @Override public T execute(@NonNull Function<EntityManager, T> function) throws SQLException { DefaultTransactionData data = null; EntityManager entityManager = sessionFactory.createEntityManager(); try { data = checkAndBeginTransaction(entityManager); T response = function.apply(entityManager); checkAndCommit(data, entityManager); entityManager.close(); return response; } catch (Exception e) { handleException(entityManager, data); throw e; } }  @Override T execute(@NonNull Function<EntityManager, T> function); }
DefaultExecution implements Execution<T> { @Override public T execute(@NonNull Function<EntityManager, T> function) throws SQLException { DefaultTransactionData data = null; EntityManager entityManager = sessionFactory.createEntityManager(); try { data = checkAndBeginTransaction(entityManager); T response = function.apply(entityManager); checkAndCommit(data, entityManager); entityManager.close(); return response; } catch (Exception e) { handleException(entityManager, data); throw e; } }  @Override T execute(@NonNull Function<EntityManager, T> function);  }