diff --git a/src/main/java/NTNU/IDATT1002/controllers/Map.java b/src/main/java/NTNU/IDATT1002/controllers/Map.java
index 5745fb9cb97545b208c18181bf22debc65ca7c36..6f184d30db6071ad24ffea3001c096b5752cbda3 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 db23b34984d2fb2024e90ff609a32a2ef49812c5..4b6b96cb5afd486f793a259036d848c56a879dad 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 6d4b2d112982a81c881efbaa6dfcd3ddca541309..1e15880d9707d51c735b8882f40d5253cde360a4 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"/>