Necesito leer una base de datos que se encuentra en otro schema diferente al que estoy usando. Entonces googleando encontré estos 2 tutoriales de como configurar 2 datasources en spring boot (https://medium.com/@joeclever/using-multiple-datasources-with-spring-boot-and-spring-data-6430b00c02e7 y https://www.baeldung.com/spring-data-jpa-multiple-databases). Mi aplicación es un api rest hecho en spring-boot con jpa/hibernate y oracle. Al levantar tomcat me sale este error y aborta:
APPLICATION FAILED TO START
Description:
2019-07-02 19:29:54.720 ERROR 780 --- [ost-startStop-1] com.zaxxer.hikari.HikariConfig : HikariPool-1 - jdbcUrl is required with driverClassName.
Les pongo mis clases de configuración. Si falta alguna me la piden por favor. Esta clase configura un datasource
@Configuration
@PropertySource({"classpath:application.properties"})
@EnableJpaRepositories(basePackages = "com.adalove.gestor.core.dao.model.genesys", entityManagerFactoryRef = "genesysEntityManager", transactionManagerRef = "genesysTransactionManager")
public class PersistenceGenesysAutoConfiguration {
@Autowired
private Environment env;
public PersistenceGenesysAutoConfiguration() {
super();
}
@Bean
public LocalContainerEntityManagerFactoryBean genesysEntityManager() {
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(genesysDataSource());
em.setPackagesToScan("com.adalove.gestor.core.dao.model.genesys");
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
final HashMap<String, Object> properties = new HashMap<String, Object>();
//properties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
properties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));
em.setJpaPropertyMap(properties);
return em;
}
@Bean
@ConfigurationProperties(prefix="spring.second-datasource")
public DataSource genesysDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
public PlatformTransactionManager genesysTransactionManager() {
final JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(genesysEntityManager().getObject());
return transactionManager;
}
}
Esta clase configura el otro datasource
:
@Configuration
@PropertySource({"classpath:application.properties"})
@EnableJpaRepositories(
basePackages = "com.adalove.gestor.core.dao.repository.gestor",
entityManagerFactoryRef = "gestorEntityManager",
transactionManagerRef = "gestorTransactionManager"
)
public class PersistenceGestorAutoConfiguration {
@Autowired
private Environment env;
public PersistenceGestorAutoConfiguration() {
super();
}
@Primary
@Bean
@ConfigurationProperties(prefix="spring.datasource")
public DataSource gestorDataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean
public LocalContainerEntityManagerFactoryBean gestorEntityManager() {
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(gestorDataSource());
em.setPackagesToScan("com.adalove.gestor.core.dao.model.gestor");
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
final HashMap<String, Object> properties = new HashMap<String, Object>();
//properties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
properties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));
em.setJpaPropertyMap(properties);
return em;
}
@Primary
@Bean
public PlatformTransactionManager gestorTransactionManager() {
final JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(gestorEntityManager().getObject());
return transactionManager;
}
}
Esta es la parte del application.properties
de donde toma las propiedades:
spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect
spring.datasource.url= jdbc:oracle:thin:@192.168.0.44:1521:XE
spring.datasource.username=GESTOR
spring.datasource.password=password
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
## Configuracion a GENESYS
spring.second-datasource.url= jdbc:oracle:thin:@192.168.0.44:1521:XE
spring.second-datasource.username=GENCONF
spring.second-datasource.password=password
La clase CustomUserDetailsService
que menciona que supuestamente tiene el error es la siguiente:
package com.adalove.gestor.core.security;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import
org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.adalove.gestor.core.dao.model.gestor.User;
import com.adalove.gestor.core.dao.repository.gestor.UserRepository;
import com.adalove.gestor.core.exception.AppException;
@Service
public class CustomUserDetailsService implements UserDetailsService {
@Autowired
UserRepository userRepository;
@Override
@Transactional
public UserDetails loadUserByUsername(String usernameOrEmail)
throws UsernameNotFoundException {
User user = userRepository.findByUsernameOrEmail(usernameOrEmail, usernameOrEmail)
.orElseThrow(() ->
new UsernameNotFoundException("No se encontró un usuario con el nombre: " + usernameOrEmail)
);
if (!user.isActive())
throw new AppException("El Usuario no se encuentra activo!.");
return UserPrincipal.create(user);
}
// This method is used by JWTAuthenticationFilter
@Transactional
public UserDetails loadUserById(Long id) {
User user = userRepository.findById(id).orElseThrow(
() -> new UsernameNotFoundException("User not found with id : " + id)
);
return UserPrincipal.create(user);
}
}
Bueno, espero me puedan ayudar...estoy bastante desesperado porque los artículos que puse de referencia no parecen complicados pero proyecto no anda y no encuentro mucha mas información de como usar dos schemas distintos en spring boot. GRACIAS