How can I configure multiple databases with springboot one being JPA and the other 5 plain jdbc? - spring-boot

For a project with a springboot backend, I have to use 1 database in Postgresql to save information and to add a history feature to the application and 5 other databases in Oracle on which I only have readonly access on views. For those 5 I only have to get ponctual values and the databases are huge so it doesn't make sense to have repositories and entities. But I can't find any information on how to configure multiple databases if one uses ORM and the others don't.
application.properties
#informations for the history database
spring.datasource.url = jdbc:postgresql://localhost:5432/NA_db
spring.datasource.username = userNA
spring.datasource.password = userNApwd
spring.datasource.driver-class-name=org.postgresql.Driver
spring.session.jdbc.initialize-schema: always
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
DataSourceConfiguration.java
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.zaxxer.hikari.HikariDataSource;
#Configuration
#EnableTransactionManagement
#EnableJpaRepositories(entityManagerFactoryRef = "historyEntityManagerFactory", transactionManagerRef = "historyTransactionManager", basePackages = "mypackage.history.dao")
#EntityScan("mypackage.history.model.entities")
public class DataSourceConfig {
#Primary
#Bean
#ConfigurationProperties("spring.datasource")
public DataSourceProperties SourceProperties() {
return new DataSourceProperties();
}
#Primary
#Bean(name = "historyDatasource")
#ConfigurationProperties(prefix = "spring.datasource.configuration")
public DataSource dataSource() {
return SourceProperties().initializeDataSourceBuilder().type(HikariDataSource.class).build();
}
#Primary
#Bean(name = "historyEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean barEntityManagerFactory(EntityManagerFactoryBuilder builder,
#Qualifier("historyDatasource") DataSource dataSource) {
return builder.dataSource(dataSource).packages("mypackage.history.model.entities")
.persistenceUnit("historyPU").build();
}
#Primary
#Bean(name = "historyTransactionManager")
public PlatformTransactionManager barTransactionManager(
#Qualifier("historyEntityManagerFactory") EntityManagerFactory barEntityManagerFactory) {
return new JpaTransactionManager(barEntityManagerFactory);
}
}
Connection and Statement creation for the other databases
Class.forName("oracle.jdbc.OracleDriver");
connectionYG = DriverManager.getConnection(urlYG, user, password);
stmtYG = connectionYG.createStatement();
connectionZA = DriverManager.getConnection(urlZA, user, password);
stmtZA = connectionZA.createStatement();
connectionZB = DriverManager.getConnection(urlZB, user, password);
stmtZB = connectionZB.createStatement();
connectionZC = DriverManager.getConnection(urlZC, user, password);
stmtZC = connectionZC.createStatement();
connectionZD = DriverManager.getConnection(urlZD, user, password);
stmtZD = connectionZD.createStatement();
Here is what I tried with a configuration class for the history database and instantiation of the connections directly where I need them in my service class.
Here is what I get :
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
I am pretty sure I just don't configurate my databases the correct way but I can't find the correct informations. Have you any ideas on how am I supposed to do ?

Related

Spring boot JPA repository committing code even if #Transactional placed in Service layer

See below spring boot code
I have used JPA repository.
Controller.
Service.
Repository
BaseController
package com.controller;
import com.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class BaseController {
#Autowired
private StudentService studentService;
#GetMapping(value = "/addStudent", produces = MediaType.APPLICATION_JSON_VALUE)
#ResponseBody
public ResponseEntity<String> base() {
studentService.save();
return new ResponseEntity<String>("SUCCESS", HttpStatus.OK);
}
}
StudentService.java
package com.service;
import com.model.Student;
import com.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
#Service("studentService")
public class StudentServiceImpl implements StudentService {
#Autowired
private StudentRepository studentRepository;
#Override
#Transactional
public Student save() {
Student student = new Student();
student.setFirstName("ABC");
student.setLastName("PQR");
studentRepository.save(student);
int i = 10 / 0; //Error code
return student;
}
}
StudentRepository
package com.repository;
import com.model.Student;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
#Repository("studentRepository")
public interface StudentRepository extends CrudRepository<Student, Long> {
public List<Student> findAll();
}
Application.properties
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
#maximum number of milliseconds that a client will wait for a connection
spring.datasource.hikari.connection-timeout = 20000
#minimum number of idle connections maintained by HikariCP in a connection pool
spring.datasource.hikari.minimum-idle= 10
#maximum pool size
spring.datasource.hikari.maximum-pool-size= 10
#maximum idle time for connection
spring.datasource.hikari.idle-timeout=10000
# maximum lifetime in milliseconds of a connection in the pool after it is closed.
spring.datasource.hikari.max-lifetime= 1000
#default auto-commit behavior.
spring.datasource.hikari.auto-commit =false
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.jdbcUrl=jdbc:mysql://localhost:3306/demo?autoReconnect=true&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.show-sql=true
spring.jpa.properties..hibernate.format_sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.ddl-auto=update
After executing save method from StudentRepository, data get inserted
immediately into database. no rollback or any other isolation levels are
working in StudentServiceImpl.java even if Error code is there.
I have tried to set "spring.datasource.hikari.auto-commit =true" setting value true, Placed #Transaction at top of the StudentServiceImpl.java class but still it didn't worked.
You do not need to mess with any hikari settings, and certainly not with autoCommit(true) as this DISABLES transactions. Delete all these properties.
Where is the "error" in your code? Spring rolls back on unchecked exceptions being thrown (not checked ones or errors), I cannot see that in your code.
What behavior do you expect? It looks fine to me.
It's probably beacause of Open Jpa in View behaviour.
Write the following line in your properties file:
spring.jpa.open-in-view=false
take a look at this if you want to know more.

How to connect to PostgreSQL in Spring Boot 2 using a Bean

I would like to connect to PostgreSQL using a bean (#Bean) and Spring Data.
At this bean, I would like to use my own customer properties in application.properties file, like these properties below
my.db.user=postgres
my.db.password=root
my.db.url=jdbc:postgresql://localhost:5432/mydb
I can't use use default properties (spring.datasource.url , spring.datasource.username, etc .. )
Spring Boot 2 by default uses Hikari to manager connections.
So, you can do it.
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
#Configuration
public class DatabaseConfig {
#Bean
#Primary
#ConfigurationProperties("spring.datasource")
public HikariDataSource dataSource() {
return DataSourceBuilder.create()
.type(HikariDataSource.class)
.build();
}
}
application.properties
spring.datasource.platform=postgres
spring.datasource.jdbc-url=jdbc:postgresql://localhost:5432/dbname
spring.datasource.username=postgres
spring.datasource.password=123456
If you wanna use tomcat-jdbc, add the following config.
spring.datasource.type=org.apache.tomcat.jdbc.pool.DataSource
Reference link : https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.0.0-M2-Release-Notes#default-connection-pool
You can create your own DataSource with your custom database properties the following way.
#Bean
#ConfigurationProperties(prefix="my.db")
public DataSource myDataSource() {
return DataSourceBuilder.create().build();
}
Have a look at the following description to configure two databases for a full example, this explains how you can use your own properties
two databases example

Spring Boot Hibernate auto configuration vs manual annotated config

I have a Spring Boot Web app that tries to access my database using Hibernate.
I also have a different, none spring boot application that tries to access the same database.
I am trying to configure the spring boot application using the auto configuration approach, defining my DB properties in the application.properties file.
I am configuring the none spring boot app using an annotated class.
For some reason, the Spring Boots auto configuration is acting differently to the annotated class configuration.
If I connect to the database for the first time and create the schema using hibernate ddl, and then I reconnect using the other way of configuration, I get ddl errors, and for example enumerated columns stop working.
Can someone explain why I get different behaviour using the two ways of configuring spring and hibernate and how do I get them to act in the same way? Could this have something to do with the ejb naming strategy property?
Here are the two ways I'm configuring the JPA:
application.properties:
spring.datasource.url=jdbc:hsqldb:file:databaseFiles/hibData/;hsqldb.write_delay_millis=0
spring.datasource.root=sa
spring.datasource.password=1
spring.datasource.driverClassName=org.hsqldb.jdbcDriver
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate5.SpringSessionContext
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.HSQLDialect
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.properties.hibernate.jdbc.batch_size=50
spring.jpa.properties.hibernate.order_inserts=true
spring.jpa.properties.hibernate.order_updates=true
spring.jpa.properties.hibernate.jdbc.batch_versioned_data=true
Config class:
import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.Properties;
#Configuration
#EnableTransactionManagement
public class DatabaseHibConfig {
#Bean
public DataSource getDataSource(){
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName("org.hsqldb.jdbcDriver");
ds.setUrl("jdbc:hsqldb:file:databaseFiles/hibData/;hsqldb.write_delay_millis=0");
ds.setUsername("sa");
ds.setPassword("1");
return ds;
}
#Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory, DataSource dataSource){
JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
jpaTransactionManager.setDataSource(dataSource);
jpaTransactionManager.setEntityManagerFactory(entityManagerFactory);
return jpaTransactionManager;
}
#Bean
LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource(dataSource);
entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
entityManagerFactoryBean.setPackagesToScan("mediabeast.data.hibernate.model");
Properties jpaProperties = new Properties();
jpaProperties.put("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
jpaProperties.put("hibernate.hbm2ddl.auto","update");
jpaProperties.put("hibernate.show_sql","true");
entityManagerFactoryBean.setJpaProperties(jpaProperties);
return entityManagerFactoryBean;
}
}
adding the following properties to both configs seemed to fix the errors that stopped the code from working.
spring.jpa.properties.hibernate.physical_naming_strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.properties.hibernate.implicit_naming_strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl
however I am still getting the "object already exists" ddl errors on startup when switching configs.

spring-boot 1.5.2+.RELEASE: Use more than one datasource with the same connection string

We have a spring boot application using hikariCP as connection pooler and multiple datasources to handle long jobs processing (basically it uses the same connection string with longer timeouts and a reduced number of connections)
Since 1.5.2.RELEASE upgrade, only the first datasource annotated with #Primary has its housekeeping and pool threads starting. The secondary one is simply discarded, although the debug shows the datasource initialization being executed.
With 1.5.1.RELEASE this worked properly, both group of threads would start.
Here are our datasources definitions
import java.util.Properties;
import javax.sql.DataSource;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import lombok.AllArgsConstructor;
#Profile("hikari")
#Validated
#AllArgsConstructor(onConstructor = #__(#Autowired))
#Component
class HikariDataSourceConfig
{
private final DataSourceConfig dataSourceConfig;
protected Properties setDataSourceProperties()
{
final Properties dataSourceProperties = new Properties();
dataSourceProperties.put("driverType", "thin");
dataSourceProperties.put("user", this.dataSourceConfig.getUsername());
dataSourceProperties.put("password", this.dataSourceConfig.getPassword());
dataSourceProperties.put("serverName", this.dataSourceConfig.getServer());
dataSourceProperties.put("portNumber", String.valueOf(this.dataSourceConfig.getPort()));
dataSourceProperties.put("databaseName", this.dataSourceConfig.getSid());
return dataSourceProperties;
}
protected HikariDataSource initializeDataSource(final int maximumPoolSize)
{
final HikariDataSource dataSource = new HikariDataSource();
dataSource.setMaximumPoolSize(maximumPoolSize);
dataSource.setValidationTimeout(this.dataSourceConfig.getValidationTimeout() * 1000L);
dataSource.setDataSourceClassName("oracle.jdbc.pool.OracleDataSource");
dataSource.setConnectionTimeout(this.dataSourceConfig.getConnectionTimeout() * 1000L);
dataSource.setMaxLifetime(this.dataSourceConfig.getMaxLifetime() * 1000L);
dataSource.setIdleTimeout(this.dataSourceConfig.getIdleTimeout() * 1000L);
dataSource.setAutoCommit(false);
return dataSource;
}
#Bean(destroyMethod = "close")
#Primary
public DataSource dataSource()
{
final HikariDataSource dataSource = initializeDataSource(this.dataSourceConfig.getMaximumPoolSize());
dataSource.setPoolName(DataSourceConfig.CONNECTION_POOL_DEFAULT_NAME);
dataSource.setLeakDetectionThreshold(this.dataSourceConfig.getLeakDetectionThreshold() * 1000L);
dataSource.setDataSourceProperties(setDataSourceProperties());
return dataSource;
}
#Bean(name = DataSourceConfig.DATASOURCE_ALT_NAME, destroyMethod = "close")
public DataSource slowDataSource()
{
final HikariDataSource dataSource = initializeDataSource(this.dataSourceConfig.getWorkingQueueSize());
dataSource.setMinimumIdle(1);
dataSource.setPoolName(DataSourceConfig.CONNECTION_POOL_ALT_NAME);
dataSource.setLeakDetectionThreshold(this.dataSourceConfig.getSlowJobLeakDetectionThreshold() * 1000L);
dataSource.setDataSourceProperties(setDataSourceProperties());
return dataSource;
}
}
Is there a way to configure spring to allow cloned datasources again?
Versions used:
HikariCP: 2.6.1
Spring-boot: 1.5.3.RELEASE
Java: 1.8.121
-- Edit
It fails for both 1.5.2.RELEASE and 1.5.3.RELEASE
-- Edit 2
I tried to bind the second datasource to a different database, but the same issue occurs, the secondary datasource pool does not start.

embedded tomcat valve spring boot

I'm trying to configure the LogbackValve for getting access logs in case my Spring Boot based web application is running from embedded Tomcat. Following is the code for configuration:
import javax.servlet.Servlet;
import org.apache.catalina.startup.Tomcat;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import ch.qos.logback.access.tomcat.LogbackValve;
#Configuration
public class EmbeddedTomcatConfigurator {
#Bean
#ConditionalOnClass({ Servlet.class, Tomcat.class })
#ConditionalOnBean(value = LogbackValve.class)
public TomcatEmbeddedServletContainerFactory tomcatEmbeddedServletContainerFactory(LogbackValve logbackValve) {
TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
factory.addContextValves(logbackValve);
return factory;
}
#Bean
#ConditionalOnProperty(name = "embedded.tomcat.logback.access.config.path")
public LogbackValve logbackValve(#Value("${embedded.tomcat.logback.access.config.path:}") String fileName) {
LogbackValve logbackValve = new LogbackValve();
logbackValve.setFilename(fileName);
return logbackValve;
}
}
However, everytime I start the application using "mvn spring-boot:run" in debug mode, I see logs saying, "LogbackValve not found" when trying to create instance of "tomcatEmbeddedServletContainerFactory" bean. However, another log statement indicates creation of this bean. Due to this, it always initializes the bean defined in the auto-configuration class "org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration".
For now, I've modified my class as :
import javax.servlet.Servlet;
import org.apache.catalina.startup.Tomcat;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import ch.qos.logback.access.tomcat.LogbackValve;
#Configuration
public class EmbeddedTomcatConfigurator {
#Bean
#ConditionalOnClass({ Servlet.class, Tomcat.class })
#ConditionalOnProperty(name = "embedded.tomcat.logback.access.config.path")
public TomcatEmbeddedServletContainerFactory tomcatEmbeddedServletContainerFactory(#Value("${embedded.tomcat.logback.access.config.path:}") String logbackAccessPath) {
TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
factory.addContextValves(getLogbackValve(logbackAccessPath));
return factory;
}
private LogbackValve getLogbackValve(String fileName) {
LogbackValve logbackValve = new LogbackValve();
logbackValve.setFilename(fileName);
return logbackValve;
}
}
I've already asked this question on Git and it has been resolved. But, here, the point I'm trying to bring up is, why the #ConditionalOnBean(value = LogbackValve.class) isn't detecting the bean, which has been defined as well.

Resources