From 15325672a0f82d894200080261327cfcef7e3a40 Mon Sep 17 00:00:00 2001 From: Paulo Gustavo Veiga Date: Wed, 22 Nov 2023 21:43:11 -0800 Subject: [PATCH] Move db initialization to beans. --- .../com/wisemapping/config/Application.java | 4 - .../wisemapping/config/HibernateConfig.java | 88 +++++++++++++++++++ .../main/resources/{spring => }/spring.tld | 0 .../resources/spring/wisemapping-common.xml | 2 - .../main/resources/spring/wisemapping-dao.xml | 38 -------- .../spring/wisemapping-datasource.xml | 20 ----- wise-webapp/src/main/webapp/WEB-INF/web.xml | 2 +- 7 files changed, 89 insertions(+), 65 deletions(-) create mode 100644 wise-webapp/src/main/java/com/wisemapping/config/HibernateConfig.java rename wise-webapp/src/main/resources/{spring => }/spring.tld (100%) delete mode 100644 wise-webapp/src/main/resources/spring/wisemapping-dao.xml delete mode 100644 wise-webapp/src/main/resources/spring/wisemapping-datasource.xml diff --git a/wise-webapp/src/main/java/com/wisemapping/config/Application.java b/wise-webapp/src/main/java/com/wisemapping/config/Application.java index b406ca6e..54b02bda 100644 --- a/wise-webapp/src/main/java/com/wisemapping/config/Application.java +++ b/wise-webapp/src/main/java/com/wisemapping/config/Application.java @@ -20,10 +20,6 @@ import org.springframework.web.servlet.view.JstlView; @ComponentScan @ImportResource("classpath:spring/wisemapping-common.xml") public class Application { - - @Autowired - TransactionManager txManager; - @Bean HandlerExceptionResolver errorHandler() { final SimpleMappingExceptionResolver result = new SimpleMappingExceptionResolver(); diff --git a/wise-webapp/src/main/java/com/wisemapping/config/HibernateConfig.java b/wise-webapp/src/main/java/com/wisemapping/config/HibernateConfig.java new file mode 100644 index 00000000..401f7f0a --- /dev/null +++ b/wise-webapp/src/main/java/com/wisemapping/config/HibernateConfig.java @@ -0,0 +1,88 @@ +package com.wisemapping.config; + +import org.apache.commons.dbcp2.BasicDataSource; +import org.hibernate.SessionFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.orm.hibernate5.HibernateTransactionManager; +import org.springframework.orm.hibernate5.LocalSessionFactoryBean; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +import javax.sql.DataSource; +import java.util.Properties; + +@Configuration +@EnableTransactionManagement +public class HibernateConfig { + + @Value("${database.hibernate.dialect}") + private String dbDialect; + + @Value("${database.driver}") + private String dbDriver; + + @Value("${database.url}") + private String dbUrl; + + @Value("${database.username}") + private String dbUsername; + @Value("${database.password}") + private String dbPassword; + + @Value("${database.validation.enabled:true}") + private boolean dbSetOnBorrow; + + @Value("${database.validation.query:SELECT 1}") + private String dbValQuery; + + @Bean + public LocalSessionFactoryBean sessionFactory() { + final LocalSessionFactoryBean result = new LocalSessionFactoryBean(); + result.setPackagesToScan("com.wisemapping.model"); + result.setDataSource(dataSource()); + result.setHibernateProperties(hibernateProperties()); + + return result; + } + + + @Bean + public HibernateTransactionManager hibernateTransactionManager() { + final HibernateTransactionManager result = new HibernateTransactionManager(); + result.setNestedTransactionAllowed(true); + // @Todo: Am I creatting two instances ??? + result.setSessionFactory(sessionFactory().getObject()); + return result; + } + + private Properties hibernateProperties() { + final Properties result = new Properties(); + result.setProperty("hibernate.dialect", dbDialect); + result.setProperty("hibernate.default_batch_fetch_size", "200"); + result.setProperty("hibernate.nestedTransactionAllowed", "true"); + result.setProperty("hibernate.auto_quote_keyword", "true"); + + return result; + } + + @Bean + public DataSource dataSource() { + final BasicDataSource result = new BasicDataSource(); + result.setDriverClassName(dbDriver); + result.setUrl(dbUrl); + result.setUsername(dbUsername); + result.setPassword(dbPassword); + result.setTestOnBorrow(dbSetOnBorrow); + + result.setDefaultQueryTimeout(15); + result.setMaxTotal(100); + result.setMaxIdle(30); + result.setInitialSize(5); + result.setMaxWaitMillis(10000); + result.setValidationQuery(dbValQuery); + + return result; + } +} diff --git a/wise-webapp/src/main/resources/spring/spring.tld b/wise-webapp/src/main/resources/spring.tld similarity index 100% rename from wise-webapp/src/main/resources/spring/spring.tld rename to wise-webapp/src/main/resources/spring.tld diff --git a/wise-webapp/src/main/resources/spring/wisemapping-common.xml b/wise-webapp/src/main/resources/spring/wisemapping-common.xml index 87b69494..ef3b1361 100644 --- a/wise-webapp/src/main/resources/spring/wisemapping-common.xml +++ b/wise-webapp/src/main/resources/spring/wisemapping-common.xml @@ -9,8 +9,6 @@ - - diff --git a/wise-webapp/src/main/resources/spring/wisemapping-dao.xml b/wise-webapp/src/main/resources/spring/wisemapping-dao.xml deleted file mode 100644 index ba55daa0..00000000 --- a/wise-webapp/src/main/resources/spring/wisemapping-dao.xml +++ /dev/null @@ -1,38 +0,0 @@ - - - - - - - - com.wisemapping.model.User - com.wisemapping.model.Collaborator - com.wisemapping.model.Collaboration - com.wisemapping.model.Mindmap - com.wisemapping.model.Label - com.wisemapping.model.CollaborationProperties - com.wisemapping.model.AccessAuditory - com.wisemapping.model.MindMapHistory - - - - - ${database.hibernate.dialect} - - - 200 - true - true - - - - - - - - - - diff --git a/wise-webapp/src/main/resources/spring/wisemapping-datasource.xml b/wise-webapp/src/main/resources/spring/wisemapping-datasource.xml deleted file mode 100644 index a944d6bc..00000000 --- a/wise-webapp/src/main/resources/spring/wisemapping-datasource.xml +++ /dev/null @@ -1,20 +0,0 @@ - - - - - - - - - - - - - - - - - diff --git a/wise-webapp/src/main/webapp/WEB-INF/web.xml b/wise-webapp/src/main/webapp/WEB-INF/web.xml index 80132f68..921ab35f 100644 --- a/wise-webapp/src/main/webapp/WEB-INF/web.xml +++ b/wise-webapp/src/main/webapp/WEB-INF/web.xml @@ -52,7 +52,7 @@ sessionFactoryBeanName - mindmapSessionFactory + sessionFactory