Loading src/main/java/org/etsi/osl/tmf/so641/reposervices/ServiceOrderRepoService.java +20 −13 Viewed Changes for src/main/java/org/etsi/osl/tmf/so641/reposervices/ServiceOrderRepoService.java: 20 added lines, 13 removed lines. Original line number Diff line number Diff line Loading @@ -1116,41 +1116,48 @@ public class ServiceOrderRepoService { return res; } /** * The order with its related party, notes, items and their services initialised, or null when it is not * found or cannot be loaded. * <p> * The transaction is always completed (committed, or rolled back in finally) before the session is closed: * a session closed with an active transaction never returns its JDBC connection to the pool (#121). */ @Transactional public ServiceOrder getServiceORderEager(String id) { try( Session session = sessionFactory.openSession() ) { Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); ServiceOrder s = null; try { s = (ServiceOrder) session.get(ServiceOrder.class, id); ServiceOrder s = session.get(ServiceOrder.class, id); if (s == null) { return this.findByUuid(id);// last resort logger.warn("getServiceORderEager: service order not found id=" + id); return null; } Hibernate.initialize(s.getRelatedParty()); Hibernate.initialize(s.getOrderItem() ); Hibernate.initialize(s.getNote() ); for (ServiceOrderItem soi : s.getOrderItem()) { if (soi.getService() != null) { Hibernate.initialize( soi.getService().getSupportingService() ); Hibernate.initialize( soi.getService().getSupportingResource()); Hibernate.initialize( soi.getService().getServiceCharacteristic() ); Hibernate.initialize( soi.getService().getRelatedParty() ); } tx.commit(); } finally { session.close(); } tx.commit(); return s; } catch (Exception e) { // TODO: handle exception } logger.error("getServiceORderEager failed for id=" + id + ": " + e.getMessage(), e); return null; } finally { if (tx.isActive()) { tx.rollback(); } session.close(); } } @Transactional(propagation = Propagation.REQUIRES_NEW) Loading src/test/java/org/etsi/osl/services/api/so641/ServiceOrderRepoServiceTest.java +65 −25 Viewed Changes for src/test/java/org/etsi/osl/services/api/so641/ServiceOrderRepoServiceTest.java: 65 added lines, 25 removed lines. Original line number Diff line number Diff line Loading @@ -19,6 +19,11 @@ import org.apache.commons.io.IOUtils; import org.etsi.osl.services.api.BaseIT; import org.etsi.osl.tmf.JsonUtils; import java.util.stream.Collectors; import javax.sql.DataSource; import com.zaxxer.hikari.HikariDataSource; import jakarta.persistence.EntityManager; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.support.TransactionTemplate; import org.etsi.osl.tmf.common.model.Any; import org.etsi.osl.tmf.common.model.service.Characteristic; import org.etsi.osl.tmf.common.model.service.Note; Loading Loading @@ -71,6 +76,15 @@ public class ServiceOrderRepoServiceTest extends BaseIT { @Autowired private WebApplicationContext context; @Autowired private DataSource dataSource; @Autowired private PlatformTransactionManager transactionManager; @Autowired private EntityManager entityManager; @BeforeEach public void setup() throws Exception { mvc = MockMvcBuilders Loading Loading @@ -820,31 +834,57 @@ public class ServiceOrderRepoServiceTest extends BaseIT { } // // This test causes "HikariPool-1 - Connection is not available, request timed out after 30000ms" error for other tests // @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) // @Test // public void testAddServiceOrderReturnEager() throws Exception { // // ServiceOrderCreate serviceOrder = new ServiceOrderCreate(); // serviceOrder.setCategory("Test Category"); // serviceOrder.setDescription("A Test Service Order"); // serviceOrder.setRequestedStartDate(OffsetDateTime.now(ZoneOffset.UTC).toString()); // serviceOrder.setRequestedCompletionDate(OffsetDateTime.now(ZoneOffset.UTC).toString()); // // Note noteItem = new Note(); // noteItem.text("Test note"); // serviceOrder.addNoteItem(noteItem); // // ServiceOrderItem soi = new ServiceOrderItem(); // serviceOrder.getOrderItem().add(soi); // soi.setState(ServiceOrderStateType.ACKNOWLEDGED); // // // String eager = serviceOrderRepoService.addServiceOrderReturnEager(serviceOrder); // ServiceOrder eagerServiceOrder = JsonUtils.toJsonObj(eager, ServiceOrder.class); // assertThat(eagerServiceOrder.getDescription()).isEqualTo("A Test Service Order"); // assertThat(eagerServiceOrder.getCategory()).isEqualTo("Test Category"); // } /** * An order whose item has no service (#121). Before the fix, loading it eagerly threw a * NullPointerException with the transaction still open, and every call leaked one pooled connection. */ @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) @Test public void testGetServiceOrderEagerItemWithoutServiceDoesNotLeakConnection() throws Exception { ServiceOrder createdServiceOrder = JsonUtils.toJsonObj(createServiceOrder(), ServiceOrder.class); String soId = createdServiceOrder.getId(); String itemId = createdServiceOrder.getOrderItem().iterator().next().getId(); // a client cannot create such an item any more, so drop its service in the database new TransactionTemplate(transactionManager).executeWithoutResult(status -> entityManager.createQuery("UPDATE ServiceOrderItem soi SET soi.service = NULL WHERE soi.uuid = :id") .setParameter("id", itemId) .executeUpdate()); int activeBefore = activeConnections(); // more calls than the test pool size: with the leak, the pool would run out for (int i = 0; i < 6; i++) { ServiceOrder eagerServiceOrder = serviceOrderFromDb(soId); assertThat(eagerServiceOrder).isNotNull(); assertThat(eagerServiceOrder.getDescription()).isEqualTo("A Test Service Order"); assertThat(eagerServiceOrder.findOrderItemById(itemId).getService()).isNull(); } assertThat(activeConnections()).isLessThanOrEqualTo(activeBefore); } /** * A service order that does not exist (#121): the reply stays "null" and no connection is leaked */ @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) @Test public void testGetServiceOrderEagerMissingOrderDoesNotLeakConnection() throws Exception { int activeBefore = activeConnections(); // more calls than the test pool size: with the leak, the pool would run out for (int i = 0; i < 6; i++) { assertThat(serviceOrderRepoService.getServiceOrderEagerAsString("non-existent-uuid")).isEqualTo("null"); } assertThat(activeConnections()).isLessThanOrEqualTo(activeBefore); } private int activeConnections() throws Exception { return dataSource.unwrap(HikariDataSource.class).getHikariPoolMXBean().getActiveConnections(); } @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) Loading Loading
src/main/java/org/etsi/osl/tmf/so641/reposervices/ServiceOrderRepoService.java +20 −13 Viewed Changes for src/main/java/org/etsi/osl/tmf/so641/reposervices/ServiceOrderRepoService.java: 20 added lines, 13 removed lines. Original line number Diff line number Diff line Loading @@ -1116,41 +1116,48 @@ public class ServiceOrderRepoService { return res; } /** * The order with its related party, notes, items and their services initialised, or null when it is not * found or cannot be loaded. * <p> * The transaction is always completed (committed, or rolled back in finally) before the session is closed: * a session closed with an active transaction never returns its JDBC connection to the pool (#121). */ @Transactional public ServiceOrder getServiceORderEager(String id) { try( Session session = sessionFactory.openSession() ) { Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); ServiceOrder s = null; try { s = (ServiceOrder) session.get(ServiceOrder.class, id); ServiceOrder s = session.get(ServiceOrder.class, id); if (s == null) { return this.findByUuid(id);// last resort logger.warn("getServiceORderEager: service order not found id=" + id); return null; } Hibernate.initialize(s.getRelatedParty()); Hibernate.initialize(s.getOrderItem() ); Hibernate.initialize(s.getNote() ); for (ServiceOrderItem soi : s.getOrderItem()) { if (soi.getService() != null) { Hibernate.initialize( soi.getService().getSupportingService() ); Hibernate.initialize( soi.getService().getSupportingResource()); Hibernate.initialize( soi.getService().getServiceCharacteristic() ); Hibernate.initialize( soi.getService().getRelatedParty() ); } tx.commit(); } finally { session.close(); } tx.commit(); return s; } catch (Exception e) { // TODO: handle exception } logger.error("getServiceORderEager failed for id=" + id + ": " + e.getMessage(), e); return null; } finally { if (tx.isActive()) { tx.rollback(); } session.close(); } } @Transactional(propagation = Propagation.REQUIRES_NEW) Loading
src/test/java/org/etsi/osl/services/api/so641/ServiceOrderRepoServiceTest.java +65 −25 Viewed Changes for src/test/java/org/etsi/osl/services/api/so641/ServiceOrderRepoServiceTest.java: 65 added lines, 25 removed lines. Original line number Diff line number Diff line Loading @@ -19,6 +19,11 @@ import org.apache.commons.io.IOUtils; import org.etsi.osl.services.api.BaseIT; import org.etsi.osl.tmf.JsonUtils; import java.util.stream.Collectors; import javax.sql.DataSource; import com.zaxxer.hikari.HikariDataSource; import jakarta.persistence.EntityManager; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.support.TransactionTemplate; import org.etsi.osl.tmf.common.model.Any; import org.etsi.osl.tmf.common.model.service.Characteristic; import org.etsi.osl.tmf.common.model.service.Note; Loading Loading @@ -71,6 +76,15 @@ public class ServiceOrderRepoServiceTest extends BaseIT { @Autowired private WebApplicationContext context; @Autowired private DataSource dataSource; @Autowired private PlatformTransactionManager transactionManager; @Autowired private EntityManager entityManager; @BeforeEach public void setup() throws Exception { mvc = MockMvcBuilders Loading Loading @@ -820,31 +834,57 @@ public class ServiceOrderRepoServiceTest extends BaseIT { } // // This test causes "HikariPool-1 - Connection is not available, request timed out after 30000ms" error for other tests // @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) // @Test // public void testAddServiceOrderReturnEager() throws Exception { // // ServiceOrderCreate serviceOrder = new ServiceOrderCreate(); // serviceOrder.setCategory("Test Category"); // serviceOrder.setDescription("A Test Service Order"); // serviceOrder.setRequestedStartDate(OffsetDateTime.now(ZoneOffset.UTC).toString()); // serviceOrder.setRequestedCompletionDate(OffsetDateTime.now(ZoneOffset.UTC).toString()); // // Note noteItem = new Note(); // noteItem.text("Test note"); // serviceOrder.addNoteItem(noteItem); // // ServiceOrderItem soi = new ServiceOrderItem(); // serviceOrder.getOrderItem().add(soi); // soi.setState(ServiceOrderStateType.ACKNOWLEDGED); // // // String eager = serviceOrderRepoService.addServiceOrderReturnEager(serviceOrder); // ServiceOrder eagerServiceOrder = JsonUtils.toJsonObj(eager, ServiceOrder.class); // assertThat(eagerServiceOrder.getDescription()).isEqualTo("A Test Service Order"); // assertThat(eagerServiceOrder.getCategory()).isEqualTo("Test Category"); // } /** * An order whose item has no service (#121). Before the fix, loading it eagerly threw a * NullPointerException with the transaction still open, and every call leaked one pooled connection. */ @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) @Test public void testGetServiceOrderEagerItemWithoutServiceDoesNotLeakConnection() throws Exception { ServiceOrder createdServiceOrder = JsonUtils.toJsonObj(createServiceOrder(), ServiceOrder.class); String soId = createdServiceOrder.getId(); String itemId = createdServiceOrder.getOrderItem().iterator().next().getId(); // a client cannot create such an item any more, so drop its service in the database new TransactionTemplate(transactionManager).executeWithoutResult(status -> entityManager.createQuery("UPDATE ServiceOrderItem soi SET soi.service = NULL WHERE soi.uuid = :id") .setParameter("id", itemId) .executeUpdate()); int activeBefore = activeConnections(); // more calls than the test pool size: with the leak, the pool would run out for (int i = 0; i < 6; i++) { ServiceOrder eagerServiceOrder = serviceOrderFromDb(soId); assertThat(eagerServiceOrder).isNotNull(); assertThat(eagerServiceOrder.getDescription()).isEqualTo("A Test Service Order"); assertThat(eagerServiceOrder.findOrderItemById(itemId).getService()).isNull(); } assertThat(activeConnections()).isLessThanOrEqualTo(activeBefore); } /** * A service order that does not exist (#121): the reply stays "null" and no connection is leaked */ @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) @Test public void testGetServiceOrderEagerMissingOrderDoesNotLeakConnection() throws Exception { int activeBefore = activeConnections(); // more calls than the test pool size: with the leak, the pool would run out for (int i = 0; i < 6; i++) { assertThat(serviceOrderRepoService.getServiceOrderEagerAsString("non-existent-uuid")).isEqualTo("null"); } assertThat(activeConnections()).isLessThanOrEqualTo(activeBefore); } private int activeConnections() throws Exception { return dataSource.unwrap(HikariDataSource.class).getHikariPoolMXBean().getActiveConnections(); } @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) Loading