Skip to content

Commit

Permalink
gestion datasource
Browse files Browse the repository at this point in the history
  • Loading branch information
khergalant committed Feb 15, 2024
1 parent 96f1236 commit 0444ccc
Show file tree
Hide file tree
Showing 56 changed files with 311 additions and 152 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
<releases>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
<checksumPolicy>ignore</checksumPolicy>
</releases>
</repository>

Expand Down Expand Up @@ -703,7 +703,7 @@
<configuration>
<source>
<![CDATA[
def puMap = ['pun-jpa' : 'fr.univlorraine.ecandidat.entities']
def puMap = ['pun-jpa-ecandidat' : 'fr.univlorraine.ecandidat.entities.ecandidat', 'pun-jpa-apogee' : 'fr.univlorraine.ecandidat.entities.siscol.apogee']
println '[INFO] Generating file persistence.xml'
def directory = new File(project.build.directory + '/generated-persistenceinfo/META-INF')
Expand Down
158 changes: 158 additions & 0 deletions src/main/java/fr/univlorraine/ecandidat/config/JpaConfigApogee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/**
* ESUP-Portail eCandidat - Copyright (c) 2016 ESUP-Portail consortium
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package fr.univlorraine.ecandidat.config;

import java.util.Properties;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.persistence.config.PersistenceUnitProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.AdviceMode;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect;
import org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import fr.univlorraine.ecandidat.entities.siscol.apogee.AnneeUni;
import fr.univlorraine.ecandidat.utils.JpaConfigApogeeCondition;

/**
* Configuration JPA
* @author Kevin Hergalant
*/
@Configuration
@EnableTransactionManagement(mode = AdviceMode.ASPECTJ)
@Conditional(JpaConfigApogeeCondition.class)
public class JpaConfigApogee {

public final static String PERSISTENCE_UNIT_NAME = "pun-jpa-apogee";

private final Logger logger = LoggerFactory.getLogger(JpaConfigApogee.class);

@Value("${showSql:false}")
private transient Boolean showSql;

@Value("${datasource.apogee.url:}")
private transient String dataSourceUrl;

@Value("${datasource.apogee.username:}")
private transient String dataSourceUserName;

@Value("${datasource.apogee.password:}")
private transient String dataSourcePassword;

@Value("${datasource.apogee.driver-class-name:}")
private transient String driverClassName;

@Value("${datasource.apogee.properties.max-pool-size:}")
private transient String maximumPoolSize;

@Value("${datasource.apogee.properties.test-query:}")
private transient String connectionTestQuery;

/**
* @return Source de données
*/
@Bean(name = "dataSourceApogee")
public DataSource dataSourceApogee() {
if (StringUtils.isNotBlank(dataSourceUrl)) {
logger.info("Manually datasource configuration...");
final HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setPoolName("poolHikariApogee");
hikariConfig.setJdbcUrl(dataSourceUrl);
hikariConfig.setUsername(dataSourceUserName);
hikariConfig.setPassword(dataSourcePassword);

/* Driver */
if (StringUtils.isNotBlank(driverClassName)) {
hikariConfig.setDriverClassName(driverClassName);
} else {
hikariConfig.setDriverClassName("oracle.jdbc.OracleDriver");
}

/* Gestion des properties */

/* Max pool Size */
if (StringUtils.isNotBlank(maximumPoolSize)) {
try {
hikariConfig.setMaximumPoolSize(Integer.parseInt(maximumPoolSize));
} catch (final Exception e) {
}
}
/* Test Query */
if (StringUtils.isNotBlank(connectionTestQuery)) {
hikariConfig.setConnectionTestQuery(connectionTestQuery);
}
return new HikariDataSource(hikariConfig);
} else {
logger.info("Automatic datasource Apogee configuration...");
final JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
return dsLookup.getDataSource("java:/comp/env/jdbc/dbSiScol");
}
}

/**
* @return EntityManager Factory
*/
@Bean(name = "entityManagerFactoryApogee")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryApogee() {
final DataSource ds = dataSourceApogee();

final LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
localContainerEntityManagerFactoryBean.setPersistenceUnitName(PERSISTENCE_UNIT_NAME);
localContainerEntityManagerFactoryBean.setPackagesToScan(AnneeUni.class.getPackage().getName());
localContainerEntityManagerFactoryBean.setDataSource(ds);
localContainerEntityManagerFactoryBean.setJpaDialect(new EclipseLinkJpaDialect());

final Properties jpaProperties = new Properties();
/* Active le static weaving d'EclipseLink */
jpaProperties.put(PersistenceUnitProperties.WEAVING, "static");
/* Désactive le cache partagé */
jpaProperties.put(PersistenceUnitProperties.CACHE_SHARED_DEFAULT, String.valueOf(false));
localContainerEntityManagerFactoryBean.setJpaProperties(jpaProperties);

final EclipseLinkJpaVendorAdapter jpaVendorAdapter = new EclipseLinkJpaVendorAdapter();
jpaVendorAdapter.setGenerateDdl(false);
jpaVendorAdapter.setShowSql(showSql);
localContainerEntityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter);

return localContainerEntityManagerFactoryBean;
}

/**
* @return Transaction Manager
*/
@Bean(name = "transactionManagerApogee")
@Primary
public JpaTransactionManager transactionManagerApogee(final EntityManagerFactory entityManagerFactoryApogee) {
return new JpaTransactionManager(entityManagerFactoryApogee);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
import com.zaxxer.hikari.HikariDataSource;

import fr.univlorraine.ecandidat.entities.ecandidat.Candidat;
import fr.univlorraine.ecandidat.entities.tools.LocalTimePersistenceConverter;
import fr.univlorraine.ecandidat.entities.ecandidat.tools.LocalTimePersistenceConverter;
import fr.univlorraine.ecandidat.repositories.CandidatRepository;
import fr.univlorraine.ecandidat.utils.ConstanteUtils;
import fr.univlorraine.ecandidat.utils.migration.FlywayCallbackMigration;
Expand Down Expand Up @@ -73,23 +73,49 @@ public class JpaConfigEcandidat {
@Value("${datasource.ecandidat.password:}")
private transient String dataSourcePassword;

@Value("${datasource.ecandidat.driver-class-name:}")
private transient String driverClassName;

@Value("${datasource.ecandidat.properties.max-pool-size:}")
private transient String maximumPoolSize;

@Value("${datasource.ecandidat.properties.test-query:}")
private transient String connectionTestQuery;

/**
* @return Source de données
*/
@Bean
@Primary
public DataSource dataSourceEcandidat() {
if (StringUtils.isNotBlank(dataSourceUrl)) {
logger.info("Manually datasource configuration...");
final HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setDriverClassName("com.mysql.cj.jdbc.Driver");
hikariConfig.setPoolName("poolHikariEcandidat");
hikariConfig.setJdbcUrl(dataSourceUrl);
hikariConfig.setUsername(dataSourceUserName);
hikariConfig.setPassword(dataSourcePassword);

hikariConfig.setMaximumPoolSize(5);
hikariConfig.setConnectionTestQuery("SELECT 1");
hikariConfig.setPoolName("springHikariCP");

/* Driver */
if (StringUtils.isNotBlank(driverClassName)) {
hikariConfig.setDriverClassName(driverClassName);
} else {
hikariConfig.setDriverClassName("com.mysql.cj.jdbc.Driver");
}

/* Gestion des properties */

/* Max pool Size */
if (StringUtils.isNotBlank(maximumPoolSize)) {
try {
hikariConfig.setMaximumPoolSize(Integer.parseInt(maximumPoolSize));
} catch (final Exception e) {
}
}
/* Test Query */
if (StringUtils.isNotBlank(connectionTestQuery)) {
hikariConfig.setConnectionTestQuery(connectionTestQuery);
}
return new HikariDataSource(hikariConfig);
} else {
logger.info("Automatic datasource configuration...");
Expand Down Expand Up @@ -123,6 +149,7 @@ private void initFlyway(final DataSource ds) {
* @return EntityManager Factory
*/
@Bean(name = "entityManagerFactoryEcandidat")
@Primary
public LocalContainerEntityManagerFactoryBean entityManagerFactoryEcandidat() {
final DataSource ds = dataSourceEcandidat();
/* Si l'appli s'initialise, il faut lancer Flyway */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,13 @@
*/
package fr.univlorraine.ecandidat.controllers;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import fr.univlorraine.ecandidat.entities.ecandidat.SiScolEtablissement;
import fr.univlorraine.ecandidat.repositories.SiScolEtablissementRepository;
import fr.univlorraine.ecandidat.services.siscol.SiScolException;
import fr.univlorraine.ecandidat.services.siscol.SiScolGenericService;
Expand Down Expand Up @@ -59,21 +56,11 @@ public void testMethode() {
logger.debug("EnableTestMode : " + enableTestMode);
logger.debug("Début des tests");
try {
final List<SiScolEtablissement> listeSiScol = siScolService.getListSiScolEtablissement();
if (listeSiScol == null) {
return;
}
listeSiScol.forEach(etablissement -> {
try {
siScolEtablissementRepository.saveAndFlush(etablissement);
} catch (final Exception e) {
System.out.println(etablissement.getId().getCodEtb() + " / " + etablissement.getLibEtb());
}

});
} catch (final SiScolException ex) {
siScolService.deleteOpiPJ("1628", "DIDEN");
siScolService.getListSiScolAnneeUni().forEach(e -> System.out.println(e));
} catch (final SiScolException e) {
// TODO Auto-generated catch block
ex.printStackTrace();
e.printStackTrace();
}
logger.debug("Fin des tests");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import fr.univlorraine.ecandidat.entities.tools.EntityPushEntityListener;
import fr.univlorraine.ecandidat.entities.ecandidat.tools.EntityPushEntityListener;
import lombok.Data;
import lombok.EqualsAndHashCode;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import fr.univlorraine.ecandidat.entities.tools.EntityPushEntityListener;
import fr.univlorraine.ecandidat.entities.tools.LocalDateTimePersistenceConverter;
import fr.univlorraine.ecandidat.entities.tools.LocalTimePersistenceConverter;
import fr.univlorraine.ecandidat.entities.ecandidat.tools.EntityPushEntityListener;
import fr.univlorraine.ecandidat.entities.ecandidat.tools.LocalDateTimePersistenceConverter;
import fr.univlorraine.ecandidat.entities.ecandidat.tools.LocalTimePersistenceConverter;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import fr.univlorraine.ecandidat.entities.tools.LocalDateTimePersistenceConverter;
import fr.univlorraine.ecandidat.entities.ecandidat.tools.LocalDateTimePersistenceConverter;
import lombok.Data;
import lombok.EqualsAndHashCode;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

import fr.univlorraine.ecandidat.entities.tools.EntityPushEntityListener;
import fr.univlorraine.ecandidat.entities.tools.LocalDateTimePersistenceConverter;
import fr.univlorraine.ecandidat.entities.ecandidat.tools.EntityPushEntityListener;
import fr.univlorraine.ecandidat.entities.ecandidat.tools.LocalDateTimePersistenceConverter;
import lombok.Data;
import lombok.EqualsAndHashCode;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import fr.univlorraine.ecandidat.entities.tools.EntityPushEntityListener;
import fr.univlorraine.ecandidat.entities.tools.LocalDatePersistenceConverter;
import fr.univlorraine.ecandidat.entities.tools.LocalDateTimePersistenceConverter;
import fr.univlorraine.ecandidat.entities.ecandidat.tools.EntityPushEntityListener;
import fr.univlorraine.ecandidat.entities.ecandidat.tools.LocalDatePersistenceConverter;
import fr.univlorraine.ecandidat.entities.ecandidat.tools.LocalDateTimePersistenceConverter;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import fr.univlorraine.ecandidat.entities.tools.LocalDatePersistenceConverter;
import fr.univlorraine.ecandidat.entities.ecandidat.tools.LocalDatePersistenceConverter;
import fr.univlorraine.ecandidat.utils.bean.export.ExportListCandidatureAdresse;
import lombok.Data;
import lombok.EqualsAndHashCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import fr.univlorraine.ecandidat.entities.tools.EntityPushEntityListener;
import fr.univlorraine.ecandidat.entities.tools.LocalDatePersistenceConverter;
import fr.univlorraine.ecandidat.entities.tools.LocalDateTimePersistenceConverter;
import fr.univlorraine.ecandidat.entities.ecandidat.tools.EntityPushEntityListener;
import fr.univlorraine.ecandidat.entities.ecandidat.tools.LocalDatePersistenceConverter;
import fr.univlorraine.ecandidat.entities.ecandidat.tools.LocalDateTimePersistenceConverter;
import fr.univlorraine.ecandidat.utils.ConstanteUtils;
import lombok.Data;
import lombok.EqualsAndHashCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import fr.univlorraine.ecandidat.entities.tools.EntityPushEntityListener;
import fr.univlorraine.ecandidat.entities.tools.LocalDatePersistenceConverter;
import fr.univlorraine.ecandidat.entities.tools.LocalDateTimePersistenceConverter;
import fr.univlorraine.ecandidat.entities.ecandidat.tools.EntityPushEntityListener;
import fr.univlorraine.ecandidat.entities.ecandidat.tools.LocalDatePersistenceConverter;
import fr.univlorraine.ecandidat.entities.ecandidat.tools.LocalDateTimePersistenceConverter;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import fr.univlorraine.ecandidat.entities.tools.EntityPushEntityListener;
import fr.univlorraine.ecandidat.entities.tools.LocalDateTimePersistenceConverter;
import fr.univlorraine.ecandidat.entities.ecandidat.tools.EntityPushEntityListener;
import fr.univlorraine.ecandidat.entities.ecandidat.tools.LocalDateTimePersistenceConverter;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
Expand Down
Loading

0 comments on commit 0444ccc

Please sign in to comment.