Connecting Spring to Existing JDBC Connection - spring

I've followed this tutorial through Spring's docs. It's nice, but I can not find anything online (and current) that shows how to connect to an existing DataSource through JDBC using Spring. Where can I configure this connection? This tutorial only shows how to create one (but where exactly is it even doing that?).

From your how-to link, you can see:
Spring Boot spots H2, an in-memory relational database engine,
and automatically creates a connection. Because we are using
spring-jdbc, Spring Boot automatically creates a JdbcTemplate.
The #Autowired JdbcTemplate field automatically loads it and
makes it available.
You can bypass the default DataSource by adding #EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class}) to your Application class. This link provides more detail on setting up a DataSource in Spring Boot.
...and the other sections are pretty useful too!

You are using Spring boot which contains embedded databases, in this case H2 database (check line "Spring Boot spots H2, an in-memory relational database engine, and automatically creates a connection.")
To provide custom connection and configure DataSource see this section https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html

Related

Spring WebFlux + reactive DB2 - how to migrate from Hikari datasource and JDBC?

I've migrated an entire project to reactive WebFlux, the only missing thing is the DB queries. At the moment I'm using JdbcTemplate and Hikari Datasources and I need to make DB calls reactive?
I tried to use vertx-db2-client but it seems it does not work fine with Webflux (or I'm not capable to use it correctly).
What options do I have? Can you show any working example with Webflux and DB2?
Thanks
You would need to find that the database DB2 is supported by Spring data reactive libraries. When I look at https://spring.io/projects/spring-data-r2dbc I don't see DB2 being in the list of database they support.

is it possible to connect to two databases one with spring jpa and another with spring jdbc

I have a requirement to connect to two databases using spring boot. I can able to connect to two different databases using spring jpa, but I want to connect two databases one with spring jpa and another with spring jdbc
I got the solution. I missed to add ComponentScan for JDBC connection configuration and removed #Primary annotation

HiKariCP for Bigquery

I am new to gcloud and trying to leverage bigquery for accessing the data. I am trying to implement a connection pooling mechanism for bigquery and found that Hikari (one of the default datasource provided by spring boot) as a viable option. Could you please guide me in setting up the Bigquery credentials in the Hikari datasource as I am not able to figure out the way to give the driver, url and credentials.
I imported the bigquery api and hikari api in my pom dependencies
FYI: I am trying to make use of springboot
I have managed to establish connection from spring boot to bigquery using simba JDBC drivers and successfully queried it.
Firstly, you need to include GCP starter dependency in your boot.
<artifactId>spring-cloud-gcp-dependencies</artifactId>
Dowload and add dependency jars of simba drivers in spring boot from the below link. Also refer installation guide in the link.
https://cloud.google.com/bigquery/docs/reference/odbc-jdbc-drivers
Secondly, create a service account that has access to bigquery and generate a key JSON file which is used to authenticate our spring boot application for connecting bigquery. Refer the below link.
https://cloud.google.com/bigquery/docs/reference/libraries#setting_up_authentication.
Lastly configure this key JSON file to be accessed by our spring book application using the simba driver JDBC installation guide.
Create a simba driver datasource and use this data source to create JDBC template bean. ( JDBC url formation is given in the installation guide of simba driver) . Thats it!! Run the application and see the results.
If you face any difficulties feel free to ask the specifics.

Spring-boot JPA connect to postgres where database and schema are provided at runtime

I am building a multi-tenant application, where the tenant database configuration are stored in Redis. Based on the request, I will query the Redis to get the database and schema configured for that tenant, this application is built on spring boot.
I took a look at spring boot's JPA sample, and did some more google to find a suitable solution for this, unfortunately, I couldn't find one.
Is this really possible to use JPA sample provided here ?
https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-data-jpa
Please suggest me any other best possible way to tackle this problem
Thanks
Spring Data JPA uses JPA annotations to define domain model. These JPA entities define schema for your database. Obviously you can't change these entities, nor DB schema on runtime.
So no, updating schema on runtime is not possible with Spring Boot + Spring Data JPA combo.
Database connection (e.g. DB URL, username password) could be more flexible. You would need to create DataSource beans programmatically and somehow programatically define which data source to use for each query per tenant. But this wasn't requirement of Spring nor JPA ecosystem, therefore you may face lot of issues.
I was working on such system before, but we were using plain SQL queries via JDBC and were creating DB connection programatically to achieve schema changes on runtime.

Spring boot application having second datasource failing caching

The presence of a second datasource in a spring boot application is failing caching with the exception:
java.lang.IllegalArgumentException: Cannot find cache named 'entity-name' for CacheEvictOperation
With one datasource it's working.
Both the datasource is auto-configured by spring boot.
Datasource one using mysql, declared as primary
Datasource two using mongodb
Is this a known case? Do I need to explicitly configure entity and transaction managers?
Spring Boot does not support auto-configuring more than one datasources for general purposes so you may want to revisit your configuration (or the description).
Yes, you need to configure the entity manager and transaction managers explicitly when you need to use more than one datasource.
Hopefully, this sample shows you how to do it.

Resources