From 3069eee15688f7401a9009116b7f77a3c708dd02 Mon Sep 17 00:00:00 2001
From: Eirik Steira <eirsteir@stud.ntnu.no>
Date: Wed, 22 Apr 2020 09:23:53 +0200
Subject: [PATCH] Add c3po connection pooling and close outgoing requests when
 exiting the map

---
 .../java/NTNU/IDATT1002/controllers/Map.java  | 11 ++++++-
 .../controllers/NavBarController.java         | 33 ++++++++++++++++---
 src/main/resources/META-INF/persistence.xml   |  6 +++-
 3 files changed, 44 insertions(+), 6 deletions(-)

diff --git a/src/main/java/NTNU/IDATT1002/controllers/Map.java b/src/main/java/NTNU/IDATT1002/controllers/Map.java
index 5745fb9c..6f184d30 100644
--- a/src/main/java/NTNU/IDATT1002/controllers/Map.java
+++ b/src/main/java/NTNU/IDATT1002/controllers/Map.java
@@ -62,6 +62,7 @@ public class Map extends NavBarController implements Initializable, MapComponent
     private GoogleMap googleMap;
     private GeoApiContext geoApiContext;
     private StringProperty address = new SimpleStringProperty();
+    private EntityManager entityManager;
     private ImageService imageService;
     private AlbumService albumService;
     private ExecutorService executorService = Executors.newCachedThreadPool();
@@ -72,11 +73,19 @@ public class Map extends NavBarController implements Initializable, MapComponent
      */
     public Map() {
         App.ex.newPage("map");
-        EntityManager entityManager = App.ex.getEntityManager();
+        entityManager = App.ex.getEntityManager();
         imageService = new ImageService(entityManager);
         albumService = new AlbumService(entityManager);
     }
 
+    /**
+     * Close outgoing requests when exiting the page.
+     */
+    @Override
+    public void doBeforePageExit() {
+        entityManager.close();
+    }
+
     /**
      * Initialize {@link GoogleMapView} and {@link GeoApiContext} with required API key.
      * Also add listener for map initialization and bind the address text field to a {@link SimpleStringProperty}.
diff --git a/src/main/java/NTNU/IDATT1002/controllers/NavBarController.java b/src/main/java/NTNU/IDATT1002/controllers/NavBarController.java
index db23b349..4b6b96cb 100644
--- a/src/main/java/NTNU/IDATT1002/controllers/NavBarController.java
+++ b/src/main/java/NTNU/IDATT1002/controllers/NavBarController.java
@@ -33,6 +33,8 @@ public class NavBarController {
      * @throws IOException
      */
     public void switchToMain(MouseEvent mouseEvent) throws IOException {
+        doBeforePageExit();
+
         App.ex.setChosenAlbumId(null);
         App.setRoot("main");
     }
@@ -44,10 +46,12 @@ public class NavBarController {
      * @throws IOException
      */
     public void goToPrevious(ActionEvent actionEvent) throws IOException {
+        doBeforePageExit();
+
         String previousPage = App.ex.previousPage();
-        if (previousPage != null){
+        if (previousPage != null)
             App.setRoot(previousPage);
-        }
+
     }
 
     /**
@@ -57,9 +61,12 @@ public class NavBarController {
      * @throws IOException
      */
     public void switchToSearch(ActionEvent actionEvent) throws IOException {
-        if (!navBarSearch.getText().isEmpty()){
+        doBeforePageExit();
+
+        if (!navBarSearch.getText().isEmpty())
             App.ex.setSearchField(navBarSearch.getText());
-        }
+
+
         App.ex.setChosenAlbumId(null);
         App.setRoot("search");
     }
@@ -70,6 +77,8 @@ public class NavBarController {
      * @throws IOException
      */
     public void switchToExplore(ActionEvent actionEvent) throws IOException {
+        doBeforePageExit();
+
         App.ex.setChosenAlbumId(null);
         App.setRoot("explore");
     }
@@ -80,6 +89,8 @@ public class NavBarController {
      * @throws IOException
      */
     public void switchToAlbums(ActionEvent actionEvent) throws IOException {
+        doBeforePageExit();
+
         App.ex.setChosenAlbumId(null);
         App.setRoot("explore_albums");
     }
@@ -90,6 +101,8 @@ public class NavBarController {
      * @throws IOException
      */
     public void switchToMap(ActionEvent actionEvent) throws IOException {
+        doBeforePageExit();
+
         App.ex.setChosenAlbumId(null);
         App.setRoot("map");
     }
@@ -100,6 +113,8 @@ public class NavBarController {
      * @throws IOException
      */
     public void switchToUpload(ActionEvent actionEvent) throws IOException {
+        doBeforePageExit();
+
         App.ex.setChosenAlbumId(null);
         App.setRoot("upload");
     }
@@ -111,9 +126,19 @@ public class NavBarController {
      * @throws IOException
      */
     public void logOut(ActionEvent actionEvent) throws IOException {
+        doBeforePageExit();
+
         App.ex.emptyPageLog();
         UserService.logOut();
         App.ex.setChosenAlbumId(null);
         App.setRoot("login");
     }
+
+    /**
+     * Actions to perform when exiting the page.
+     * Can be implemented by subclasses to define custom actions on page exit.
+     */
+    public void doBeforePageExit(){
+
+    };
 }
diff --git a/src/main/resources/META-INF/persistence.xml b/src/main/resources/META-INF/persistence.xml
index 6d4b2d11..1e15880d 100644
--- a/src/main/resources/META-INF/persistence.xml
+++ b/src/main/resources/META-INF/persistence.xml
@@ -19,7 +19,11 @@
         <properties>
 
             <!-- Hibernate properties -->
-            <property name="hibernate.connection.pool_size" value="50"/>
+            <property name="hibernate.c3p0.min_size" value="5"/>
+            <property name="hibernate.c3p0.max_size" value="50"/>
+            <property name="hibernate.c3p0.timeout" value="1800"/>
+            <property name="hibernate.c3p0.max_statements" value="1800"/>
+
             <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL8Dialect"/>
             <property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/>
             <property name="hibernate.format_sql" value="true"/>
-- 
GitLab