From b6538f9a10edfa3f57ae1c70bc51190b4793f131 Mon Sep 17 00:00:00 2001 From: "FunThomas424242 (Thomas Schubert)" Date: Wed, 11 Dec 2019 23:48:33 +0100 Subject: [PATCH 1/6] [#2_singletonfix]simple fix Signed-off-by: FunThomas424242 (Thomas Schubert) --- .../sbstarter/nitrite/NitriteTemplate.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/github/funthomas424242/sbstarter/nitrite/NitriteTemplate.java b/src/main/java/com/github/funthomas424242/sbstarter/nitrite/NitriteTemplate.java index bb4724d..028b393 100644 --- a/src/main/java/com/github/funthomas424242/sbstarter/nitrite/NitriteTemplate.java +++ b/src/main/java/com/github/funthomas424242/sbstarter/nitrite/NitriteTemplate.java @@ -53,9 +53,15 @@ public class NitriteTemplate { // Das funktioniert hier nur, weil der Scope vom NitriteTemplate auf Singleton gesetzt ist. // https://www.baeldung.com/spring-bean-scopes - protected Nitrite nitriteInstanz; + protected static Nitrite nitriteInstanz; protected void init() { + + if( this.nitriteInstanz != null) { + LOG.debug("Nitrite Datenbank ist bereits gestartet."); + return; + } + LOG.debug("Starte die Nitrite Datenbank."); final NitriteBuilder builder = Nitrite.builder(); From 69fa5a50f270bf0ca0636b2a923d9a98dfef220f Mon Sep 17 00:00:00 2001 From: "FunThomas424242 (Thomas Schubert)" Date: Thu, 12 Dec 2019 23:01:31 +0100 Subject: [PATCH 2/6] [#2_singletonfix] correct fixed via bean Signed-off-by: FunThomas424242 (Thomas Schubert) --- .../sbstarter/nitrite/NitriteInstanz.java | 94 +++++++++++++++++++ .../sbstarter/nitrite/NitriteTemplate.java | 68 +------------- .../nitrite/NitriteTemplateProvider.java | 14 ++- 3 files changed, 105 insertions(+), 71 deletions(-) create mode 100644 src/main/java/com/github/funthomas424242/sbstarter/nitrite/NitriteInstanz.java diff --git a/src/main/java/com/github/funthomas424242/sbstarter/nitrite/NitriteInstanz.java b/src/main/java/com/github/funthomas424242/sbstarter/nitrite/NitriteInstanz.java new file mode 100644 index 0000000..76883cc --- /dev/null +++ b/src/main/java/com/github/funthomas424242/sbstarter/nitrite/NitriteInstanz.java @@ -0,0 +1,94 @@ +package com.github.funthomas424242.sbstarter.nitrite; + +/*- + * #%L + * Nitrite Spring Boot Starter + * %% + * Copyright (C) 2019 PIUG + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * . + * #L% + */ + +import org.dizitart.no2.Nitrite; +import org.dizitart.no2.NitriteBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.annotation.PreDestroy; + +public class NitriteInstanz { + + protected static final Logger LOG = LoggerFactory.getLogger(NitriteInstanz.class); + + protected final Nitrite nitrite; + + protected NitriteInstanz(NitriteAutoConfiguration nitriteConfig) { + + LOG.debug("Starte die Nitrite Datenbank."); + final NitriteBuilder builder = Nitrite.builder(); + + LOG.debug("[Nitrite Config]\n dbFile: {} \n compressed {} \n disableautocommit {} \n username {} \n password {}\n" + , nitriteConfig.dbfilePath + , nitriteConfig.compressed + , nitriteConfig.disableautocommit + , nitriteConfig.username + , nitriteConfig.password + ); + + + if (nitriteConfig.dbfilePath != null && !nitriteConfig.dbfilePath.isEmpty()) { + LOG.info("[Nitrite] used dbFile: {}", nitriteConfig.dbfilePath); + builder.filePath(nitriteConfig.dbfilePath); + } else { + LOG.info("[Nitrite] used as in-memory database."); + } + + if (nitriteConfig.compressed != null && nitriteConfig.compressed) { + LOG.info("[Nitrite] will be compressed automatically."); + builder.compressed(); + } + + if (nitriteConfig.disableautocommit != null && nitriteConfig.disableautocommit) { + LOG.info("[Nitrite] has autocommit disabled."); + builder.disableAutoCommit(); + } + + if (nitriteConfig.username == null || nitriteConfig.username.isEmpty() || nitriteConfig.password == null || nitriteConfig.password.isEmpty()) { + LOG.info("[Nitrite] openOrCreate without Credentials."); + this.nitrite = builder.openOrCreate(); + } else { + LOG.info("[Nitrite] openOrCreate with Credentials."); + this.nitrite = builder.openOrCreate(nitriteConfig.username, nitriteConfig.password); + } + } + + @PreDestroy + protected void destroy() { + LOG.debug("Zerstöre die Nitrite Datenbank."); + /** + * Shutdown Hook + * + * While opening the database, Nitrite registers itself to a JVM shutdown hook, which before exiting will close the + * database without persisting any unsaved changes to the disk. This shutdown hook protects the data file from + * corruption due to JVM shutdown before properly closing the database. + */ + this.nitrite.close(); + } + + public Nitrite getNitrite() { + return this.nitrite; + } +} diff --git a/src/main/java/com/github/funthomas424242/sbstarter/nitrite/NitriteTemplate.java b/src/main/java/com/github/funthomas424242/sbstarter/nitrite/NitriteTemplate.java index 028b393..ccdc2c7 100644 --- a/src/main/java/com/github/funthomas424242/sbstarter/nitrite/NitriteTemplate.java +++ b/src/main/java/com/github/funthomas424242/sbstarter/nitrite/NitriteTemplate.java @@ -26,8 +26,6 @@ import org.dizitart.no2.FindOptions; import org.dizitart.no2.Index; import org.dizitart.no2.IndexOptions; -import org.dizitart.no2.Nitrite; -import org.dizitart.no2.NitriteBuilder; import org.dizitart.no2.NitriteCollection; import org.dizitart.no2.NitriteId; import org.dizitart.no2.RemoveOptions; @@ -48,73 +46,11 @@ public class NitriteTemplate { protected static final Logger LOG = LoggerFactory.getLogger(NitriteTemplate.class); @Autowired - protected NitriteAutoConfiguration nitriteConfig; - - - // Das funktioniert hier nur, weil der Scope vom NitriteTemplate auf Singleton gesetzt ist. - // https://www.baeldung.com/spring-bean-scopes - protected static Nitrite nitriteInstanz; - - protected void init() { - - if( this.nitriteInstanz != null) { - LOG.debug("Nitrite Datenbank ist bereits gestartet."); - return; - } - - LOG.debug("Starte die Nitrite Datenbank."); - final NitriteBuilder builder = Nitrite.builder(); - - LOG.debug("[Nitrite Config]\n dbFile: {} \n compressed {} \n disableautocommit {} \n username {} \n password {}\n" - , nitriteConfig.dbfilePath - , nitriteConfig.compressed - , nitriteConfig.disableautocommit - , nitriteConfig.username - , nitriteConfig.password - ); - - - if (nitriteConfig.dbfilePath != null && !nitriteConfig.dbfilePath.isEmpty()) { - LOG.info("[Nitrite] used dbFile: {}", nitriteConfig.dbfilePath); - builder.filePath(nitriteConfig.dbfilePath); - } else { - LOG.info("[Nitrite] used as in-memory database."); - } - - if (nitriteConfig.compressed != null && nitriteConfig.compressed) { - LOG.info("[Nitrite] will be compressed automatically."); - builder.compressed(); - } - - if (nitriteConfig.disableautocommit != null && nitriteConfig.disableautocommit) { - LOG.info("[Nitrite] has autocommit disabled."); - builder.disableAutoCommit(); - } - - if (nitriteConfig.username == null || nitriteConfig.username.isEmpty() || nitriteConfig.password == null || nitriteConfig.password.isEmpty()) { - LOG.info("[Nitrite] openOrCreate without Credentials."); - this.nitriteInstanz = builder.openOrCreate(); - } else { - LOG.info("[Nitrite] openOrCreate with Credentials."); - this.nitriteInstanz = builder.openOrCreate(nitriteConfig.username, nitriteConfig.password); - } - } - - protected void destroy() { - LOG.debug("Zerstöre die Nitrite Datenbank."); - /** - * Shutdown Hook - * - * While opening the database, Nitrite registers itself to a JVM shutdown hook, which before exiting will close the - * database without persisting any unsaved changes to the disk. This shutdown hook protects the data file from - * corruption due to JVM shutdown before properly closing the database. - */ - this.nitriteInstanz.close(); - } + protected NitriteInstanz nitriteInstanz; public NitriteRepository getRepository(Class type) { LOG.debug("Erzeuge neues Nitrite Repository für: {}", type.getName()); - final ObjectRepository tmpRepository = this.nitriteInstanz.getRepository(type); + final ObjectRepository tmpRepository = this.nitriteInstanz.getNitrite().getRepository(type); return new NitriteRepository() { protected ObjectRepository objectRepository = tmpRepository; diff --git a/src/main/java/com/github/funthomas424242/sbstarter/nitrite/NitriteTemplateProvider.java b/src/main/java/com/github/funthomas424242/sbstarter/nitrite/NitriteTemplateProvider.java index aef9c3f..ee89651 100644 --- a/src/main/java/com/github/funthomas424242/sbstarter/nitrite/NitriteTemplateProvider.java +++ b/src/main/java/com/github/funthomas424242/sbstarter/nitrite/NitriteTemplateProvider.java @@ -10,12 +10,12 @@ * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Lesser Public License for more details. - * + * * You should have received a copy of the GNU General Lesser Public * License along with this program. If not, see * . @@ -24,16 +24,20 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Service; @Service class NitriteTemplateProvider { @ConditionalOnMissingBean - @Bean(initMethod = "init", destroyMethod = "destroy") - @Scope("singleton") // Trotz default, hier soll sichergestellt werden, dass beim upgrade alles so bleibt + @Bean public NitriteTemplate nitriteTemplate() { return new NitriteTemplate(); } + + @ConditionalOnMissingBean + @Bean + public NitriteInstanz nitriteInstanz(final NitriteAutoConfiguration config) { + return new NitriteInstanz(config); + } } From 2321b53ee4f209850045581dab8b42b855091288 Mon Sep 17 00:00:00 2001 From: "FunThomas424242 (Thomas Schubert)" Date: Thu, 12 Dec 2019 23:09:44 +0100 Subject: [PATCH 3/6] [#2_singletonfix] Conditional fixed? Signed-off-by: FunThomas424242 (Thomas Schubert) --- .../sbstarter/nitrite/NitriteTemplateProvider.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/com/github/funthomas424242/sbstarter/nitrite/NitriteTemplateProvider.java b/src/main/java/com/github/funthomas424242/sbstarter/nitrite/NitriteTemplateProvider.java index ee89651..6e06926 100644 --- a/src/main/java/com/github/funthomas424242/sbstarter/nitrite/NitriteTemplateProvider.java +++ b/src/main/java/com/github/funthomas424242/sbstarter/nitrite/NitriteTemplateProvider.java @@ -22,6 +22,8 @@ * #L% */ +import org.dizitart.no2.Nitrite; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Service; @@ -29,12 +31,14 @@ @Service class NitriteTemplateProvider { + @ConditionalOnClass({Nitrite.class}) @ConditionalOnMissingBean @Bean public NitriteTemplate nitriteTemplate() { return new NitriteTemplate(); } + @ConditionalOnClass({Nitrite.class}) @ConditionalOnMissingBean @Bean public NitriteInstanz nitriteInstanz(final NitriteAutoConfiguration config) { From bcdb0bd5709576f9dd3cd73b32c6a45c5bd89782 Mon Sep 17 00:00:00 2001 From: "FunThomas424242 (Thomas Schubert)" Date: Thu, 12 Dec 2019 23:22:47 +0100 Subject: [PATCH 4/6] [#2_singletonfix] spring-boot-maven plugin upgrade Signed-off-by: FunThomas424242 (Thomas Schubert) --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index f4cfe6f..fb445e0 100644 --- a/pom.xml +++ b/pom.xml @@ -50,7 +50,7 @@ 11 - 2.2.1.RELEASE + 2.2.2.RELEASE Hoxton.RC2 UTF-8 UTF-8 From ee5695a8c538f1e1b752cebfaeb132c4e9b0852a Mon Sep 17 00:00:00 2001 From: "FunThomas424242 (Thomas Schubert)" Date: Thu, 12 Dec 2019 23:42:26 +0100 Subject: [PATCH 5/6] [#2_singletonfix] Repository api: insert for single object added Signed-off-by: FunThomas424242 (Thomas Schubert) --- .../sbstarter/nitrite/NitriteRepository.java | 7 +++++-- .../funthomas424242/sbstarter/nitrite/NitriteTemplate.java | 4 ++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/github/funthomas424242/sbstarter/nitrite/NitriteRepository.java b/src/main/java/com/github/funthomas424242/sbstarter/nitrite/NitriteRepository.java index 2892970..7d94728 100644 --- a/src/main/java/com/github/funthomas424242/sbstarter/nitrite/NitriteRepository.java +++ b/src/main/java/com/github/funthomas424242/sbstarter/nitrite/NitriteRepository.java @@ -10,20 +10,23 @@ * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Lesser Public License for more details. - * + * * You should have received a copy of the GNU General Lesser Public * License along with this program. If not, see * . * #L% */ +import org.dizitart.no2.WriteResult; import org.dizitart.no2.objects.ObjectRepository; public interface NitriteRepository extends ObjectRepository { + public WriteResult insert(T et); + } diff --git a/src/main/java/com/github/funthomas424242/sbstarter/nitrite/NitriteTemplate.java b/src/main/java/com/github/funthomas424242/sbstarter/nitrite/NitriteTemplate.java index ccdc2c7..7ac5c7d 100644 --- a/src/main/java/com/github/funthomas424242/sbstarter/nitrite/NitriteTemplate.java +++ b/src/main/java/com/github/funthomas424242/sbstarter/nitrite/NitriteTemplate.java @@ -60,6 +60,10 @@ public WriteResult insert(ET et, ET... ets) { return objectRepository.insert(et, ets); } + @Override + public WriteResult insert(ET et) { + return objectRepository.insert(et); + } @Override public WriteResult update(ObjectFilter objectFilter, ET et) { From ded0f805b474ae08e4f209452fac749ff2a80f4d Mon Sep 17 00:00:00 2001 From: "FunThomas424242 (Thomas Schubert)" Date: Thu, 12 Dec 2019 23:49:35 +0100 Subject: [PATCH 6/6] [#2_singletonfix] Repository api: getNitrite added Signed-off-by: FunThomas424242 (Thomas Schubert) --- .../funthomas424242/sbstarter/nitrite/NitriteTemplate.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/com/github/funthomas424242/sbstarter/nitrite/NitriteTemplate.java b/src/main/java/com/github/funthomas424242/sbstarter/nitrite/NitriteTemplate.java index 7ac5c7d..2f2cc3d 100644 --- a/src/main/java/com/github/funthomas424242/sbstarter/nitrite/NitriteTemplate.java +++ b/src/main/java/com/github/funthomas424242/sbstarter/nitrite/NitriteTemplate.java @@ -26,6 +26,7 @@ import org.dizitart.no2.FindOptions; import org.dizitart.no2.Index; import org.dizitart.no2.IndexOptions; +import org.dizitart.no2.Nitrite; import org.dizitart.no2.NitriteCollection; import org.dizitart.no2.NitriteId; import org.dizitart.no2.RemoveOptions; @@ -48,6 +49,10 @@ public class NitriteTemplate { @Autowired protected NitriteInstanz nitriteInstanz; + public Nitrite getNitrite(){ + return nitriteInstanz.getNitrite(); + } + public NitriteRepository getRepository(Class type) { LOG.debug("Erzeuge neues Nitrite Repository für: {}", type.getName()); final ObjectRepository tmpRepository = this.nitriteInstanz.getNitrite().getRepository(type);