Move db initialization to beans.

This commit is contained in:
Paulo Gustavo Veiga 2023-11-22 21:43:11 -08:00
parent cd3d21f106
commit 15325672a0
7 changed files with 89 additions and 65 deletions

View File

@ -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();

View File

@ -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;
}
}

View File

@ -9,8 +9,6 @@
<context:property-placeholder location="/WEB-INF/app.properties" ignore-unresolvable="true"/>
<import resource="wisemapping-datasource.xml"/>
<import resource="wisemapping-dao.xml"/>
<import resource="wisemapping-service.xml"/>
<import resource="wisemapping-servlet.xml"/>
</beans>

View File

@ -1,38 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="mindmapSessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="wiseDataSource"/>
<property name="annotatedClasses">
<list>
<value>com.wisemapping.model.User</value>
<value>com.wisemapping.model.Collaborator</value>
<value>com.wisemapping.model.Collaboration</value>
<value>com.wisemapping.model.Mindmap</value>
<value>com.wisemapping.model.Label</value>
<value>com.wisemapping.model.CollaborationProperties</value>
<value>com.wisemapping.model.AccessAuditory</value>
<value>com.wisemapping.model.MindMapHistory</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${database.hibernate.dialect}</prop>
<!-- <prop key="hibernate.cache.use_second_level_cache">true</prop>-->
<!-- <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>-->
<prop key="hibernate.default_batch_fetch_size">200</prop>
<prop key="hibernate.nestedTransactionAllowed">true</prop>
<prop key="hibernate.auto_quote_keyword">true</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="mindmapSessionFactory"/>
<property name="nestedTransactionAllowed" value="true"/>
</bean>
</beans>

View File

@ -1,20 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="wiseDataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${database.driver}"/>
<property name="url" value="${database.url}"/>
<property name="username" value="${database.username}"/>
<property name="password" value="${database.password}"/>
<property name="testOnBorrow" value="${database.validation.enabled:true}"/>
<property name="defaultQueryTimeout" value="15"/>
<property name="maxTotal" value="100"/>
<property name="maxIdle" value="30"/>
<property name="initialSize" value="5"/>
<property name="maxWaitMillis" value="10000"/>
<property name="validationQuery" value="${database.validation.query:SELECT 1}"/>
</bean>
</beans>

View File

@ -52,7 +52,7 @@
</init-param>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>mindmapSessionFactory</param-value>
<param-value>sessionFactory</param-value>
</init-param>
</filter>