Multiple JDBC Clients - How to configure multiple DataSources in Spring

Dan Vega
11 Dec 202324:24

Summary

TLDR本视频由Dan Vega主讲,他是Broadcom的Spring开发者代言人。视频主题围绕如何在Spring Boot 3.2中使用JDBC客户端管理多个数据源。Dan从创建一个新的Spring Boot项目开始,逐步讲解了如何配置和使用两个不同的数据源。他详细介绍了每个数据源的设置过程,并展示了如何为每个数据库配置和使用JDBC客户端。此外,Dan还提供了一些代码示例来帮助理解如何在实践中应用这些概念。这个视频是对那些希望在Spring应用程序中管理多个数据库的开发者的有价值的指南。

Takeaways

  • 😀 使用Spring Boot 3.2版本可以通过JDBC客户端连接多个数据源。
  • 🌐 在Spring Boot中配置单个数据源非常简单,但配置多个数据源需要手动设置。
  • 🔧 多个数据源配置涉及创建不同的数据源实例,并指定每个实例的配置属性。
  • 🛠️ 使用`@Primary`注解可以指定一个主要的数据源,其他数据源则作为次要数据源。
  • 📁 对于每个数据源,需要分别配置其URL、用户名和密码等属性。
  • 🗂️ 可以通过`application.properties`文件来分别配置每个数据源的属性。
  • 🚀 创建项目时,可以在start.spring.io上选择Spring Web和JDBC API依赖来支持多数据源配置。
  • 📚 使用H2数据库进行演示,但实际应用中可以连接到不同类型的数据库。
  • 💾 对于每个数据源,可以通过特定的schema.sql文件来初始化数据库模式。
  • 👨‍💻 示例项目展示了如何使用JDBC客户端为每个数据源执行CRUD操作。
  • 🔍 使用H2控制台可以查看和管理内存中的数据库实例。
  • 📖 通过构造不同的数据源Bean和JDBC客户端Bean,可以在Spring应用中灵活地管理多个数据库连接。

Q & A

  • Dan Vega在视频中介绍了哪个主题?

    -Dan Vega介绍了在Spring Boot 3.2中使用JDBC客户端处理多个数据源的主题。

  • Spring Boot中配置多个数据源的挑战是什么?

    -Spring Boot通过自动配置简化了单个数据源的设置,但当需要配置多个数据源时,必须手动进行配置。

  • 为什么Dan Vega决定在演示中使用两个内存H2数据库?

    -Dan Vega选择使用两个内存H2数据库来简化演示,便于讲解如何连接到多个数据源。

  • 在项目设置中,为什么需要指定使用Java 21或Java 17?

    -指定使用Java 21或Java 17是为了确保项目与Spring Boot 3.2及其功能的兼容性。

  • 如何在Spring Boot项目中配置第一个数据源?

    -在application.properties文件中配置第一个数据源,包括数据库URL、用户名和密码。

  • 为什么需要为不同的数据源创建不同的schema文件?

    -需要为不同的数据源创建不同的schema文件,以便能够分别管理和初始化每个数据源的数据库结构。

  • 如何在Spring Boot中创建和配置JDBC客户端以连接到特定的数据源?

    -通过在配置类中定义Bean,并使用@Qualifier注解指定连接到特定数据源的JDBC客户端。

  • 如何验证两个不同的数据源已正确配置并连接到了相应的数据库?

    -可以通过检查每个数据源的连接信息,例如通过打印数据源的URL,来验证它们是否已正确配置并连接到了预期的数据库。

  • 在这个项目中,'subscriber'和'post'数据模型分别用于什么目的?

    -'subscriber'数据模型用于保存博客的订阅者信息,而'post'数据模型用于保存博客帖子的内容。

  • 为何Dan Vega强调在YouTube视频中留下问题和反馈的重要性?

    -Dan Vega强调这一点是因为用户的问题和反馈可以提供有价值的见解,帮助改进内容,并解答社区成员可能有的疑问。

Outlines

00:00

😀 Spring Boot 3.2与多数据源配置

Dan Vega,Broadcom的Spring开发者倡导者,在这段视频中讨论了如何在Spring Boot 3.2中使用JDBC客户端配置多个数据源。这个话题源于YouTube上的一个观众提问,关于如何处理多数据库和多架构的情况。视频首先展示了在start.spring.io创建一个新项目,配置两个不同的数据源,并介绍了如何使用JDBC客户端与它们交互。此外,Dan Vega解释了为什么在配置多个数据源时需要手动设置,以及如何使用Java 21(虽然Java 17也适用于此示例)来创建这个项目。

05:00

🔧 实现Spring Boot中的多数据源

视频的第二部分详细介绍了在Spring Boot应用程序中配置和使用多个数据源的过程。Dan Vega演示了如何通过构造器注入来获取JDBC客户端实例,并创建了基本的CRUD方法来与数据库交云。接着,他通过命令行运行器测试了与第一个数据库的连接,并在遇到一些初始问题后成功解决。此外,他还展示了如何使用Spring Boot的自动配置来创建数据源,并解释了如果需要进行更复杂的配置时该如何操作。

10:01

🌐 构建多数据库订阅者管理功能

在第三段视频中,Dan Vega着手构建一个涉及多数据库的博客应用程序的订阅者部分。他创建了一个新的subscriber记录和服务,同时设置了两个不同的数据库模式。视频中还展示了如何在application.properties文件中为两个不同的数据源设置独立的配置。接着,他创建了一个新的数据源配置类,定义了两个数据库的连接,并讨论了如何使用Spring的配置属性来指定各自的数据库。

15:02

🛠️ 配置和测试多数据源连接

第四段视频深入讨论了如何在Spring Boot应用程序中配置和测试多数据源连接。Dan Vega展示了如何在配置类中为每个数据库创建和配置数据源属性和Hikari数据源。他还演示了如何使用不同的数据源初始化脚本来设置每个数据库的模式。此外,他通过添加标记和限定符来确保每个服务都使用正确的JDBC客户端。最后,Dan运行了应用程序,验证了两个不同的数据源是否正确配置并连接。

20:02

🚀 完成多数据源集成和总结

视频的最后一部分,Dan Vega完成了对多数据源集成的演示,并对整个过程进行了总结。他展示了如何在应用程序中创建两个JDBC客户端,分别连接到两个不同的数据源,并确保了订阅者服务正确地使用了其对应的数据源。他通过打印出从两个数据库中检索到的数据来演示了这一点,并进一步验证了两个数据库连接的正确性。最后,Dan Vega鼓励观众通过多种渠道向他提出问题,并对视频内容做了总结。

Mindmap

Keywords

💡数据源

数据源是应用程序用来连接和访问数据库的配置。视频中配置了两个不同的数据源,一个用于博客文章,一个用于订阅者信息。它展示了如何在Spring Boot中配置和使用多个数据源。

💡JDBC客户端

JDBC客户端是用于访问数据库的组件。视频中为每个数据源配置了对应的JDBC客户端,它们可以独立地访问各自的数据源。

💡自动配置

Spring Boot可以自动配置单数据源。视频中展示了如何手动配置多个数据源以覆盖自动配置。

💡方言

方言指的是不同数据库的特有功能。视频使用了两个H2内存数据库作为示例,它们有相同的方言。

💡Bean

Bean是Spring中的一个对象,视频中配置了多个Bean来定义不同的数据源、JDBC客户端等。

💡 schema

数据库schema,用于定义数据库中的对象如表、视图等。视频中为每个数据源创建了单独的schema。

💡主数据源

当存在多个数据源时,需要定义一个主数据源。主数据源通常用于实体的默认持久化。

💡限定词

由于存在多个相似的Bean,视频中使用@Qualifier限定词来区分不同的Bean。

💡嵌入式数据库

嵌入式数据库H2,它们以内存形式存在。视频中使用H2来模拟多个数据源。

💡元数据

数据库元数据,描述了数据库的结构信息。视频中使用它来验证不同数据源的配置。

Highlights

Dan Vega, Spring Developer Advocate at Broadcom, discusses multiple data sources using the JDBC client in Spring Boot 3.2.

The ease of setting up a data source in Spring Boot due to autoconfiguration.

Introduction to creating a new project on start.spring.io for handling multiple data sources.

Explanation of using Spring Boot 3.2 and JDBC client for the project.

Setting up the project metadata and selecting dependencies on start.spring.io.

Creation of a simple blogging application as an example to demonstrate handling multiple data sources.

Configuration of the application.properties file for the first data source.

Introduction and setup of the JDBC client for database operations.

Demonstration of inserting and retrieving data from the first database.

Explanation of creating and configuring a second data source for subscribers.

Discussion on the necessity of distinct configurations for multiple data sources in Spring.

Demonstration of the separation and management of two different schemas for the databases.

Setting up and using the JDBC client for the second data source.

Explanation of overriding default data source configurations for multiple databases.

Verification of the application's connection to two separate databases and the retrieval of data from both.

Encouragement for viewers to ask questions and the availability of different platforms for discussion.

Transcripts

play00:00

welcome back everyone Dan Vega here

play00:01

spring developer Advocate at broadcom

play00:03

today we're going to be talking about

play00:04

multiple data sources using the jdbc

play00:07

client in Spring boot 3.2 now recently I

play00:10

made a video on jdbc client versus

play00:13

spring data jdbc and I got a lot of

play00:16

really great feedback so first off

play00:18

thanks for that uh but today we're going

play00:20

to answer one of those questions and

play00:22

this question comes from YouTube comes

play00:24

from Abraham says first of all thank you

play00:27

very much for sharing this video it was

play00:29

very help ful I wonder what would be the

play00:32

solution for jdbc client if we had

play00:35

multiple databases and multiple schemas

play00:38

and this is an interesting question

play00:40

right because we know that uh to get a

play00:43

data source up and running in Spring

play00:44

boot is pretty trivial because of

play00:46

autoconfiguration we see some things we

play00:48

configure some things and you have a

play00:50

database up and running as soon as you

play00:52

need to configure something though we

play00:54

kind of back off and let you configure

play00:56

it as needed and that's what happens

play00:58

when you need to configure fure multiple

play01:00

data sources in spring so we're going to

play01:03

take a look at an example of this today

play01:04

we're going to start a new project over

play01:06

at start. spring.io we'll set up a

play01:09

project that talks to two different data

play01:11

sources and we'll see how we can

play01:14

configure those data sources and then

play01:16

use a jdbc client for each of those so

play01:19

uh I hope you learned something new

play01:21

today with that let's get to it all

play01:23

right so we're going to start here at

play01:25

start. spring.io we're going to create a

play01:27

new Maven project you need spring boot

play01:29

3.2 because we're going to be using the

play01:31

jtbc client but I guess this would be

play01:33

the same if you were before 3.2 and you

play01:36

wanted to use multiple data sources for

play01:38

something like the rest template so I'm

play01:40

going to fill in some metadata here I'm

play01:42

going to say dev. danan Vega we'll say

play01:45

the artifact is

play01:48

multiple DS

play01:50

demo and we're going to choose Java 21

play01:53

you could probably use Java 17 for this

play01:55

example I'm going to go ahead and pick

play01:57

spring web I'm also going to pick

play02:00

uh jtbc's API so jdbc API that will

play02:04

bring in the jdbc support that has like

play02:07

the jdbc template and what you need to

play02:09

connect to a data source and then I'm

play02:11

just going to use an H2 database here uh

play02:14

I contemplated using both the like a

play02:17

postest database and an H2 I think I'll

play02:20

simplify it for this one uh just so we

play02:22

can take a look at talking to multiple

play02:24

data sources so we'll use uh two

play02:26

different inmemory H2 databases and

play02:28

that's really all we need to get this

play02:30

project up and running I'm going to go

play02:32

ahead and click generate this will

play02:33

download a zip you can open it up in

play02:36

whatever ID you're most productive in

play02:39

I'm going to open it up in tell J idea

play02:41

and with that let's go ahead and write

play02:43

some code all right so here we are in

play02:44

our application this is going to be a

play02:46

simple blogging application we're going

play02:47

to use that same uh example to kind of

play02:50

walk through this we're going to talk to

play02:53

uh a single data source first let's set

play02:55

up that one this is going to be used for

play02:57

like saving posts the second data source

play03:00

will be for maybe saving subscribers to

play03:02

our blog and so that will use a separate

play03:05

database but before we get into any of

play03:07

that let's just get that first one out

play03:08

of the way so I'm going to go ahead and

play03:10

create a new package here I'm going to

play03:12

call this post inside of there I am

play03:15

going to create a new post record so I'm

play03:19

come in here and say post record and

play03:21

this is going to have let's just paste

play03:24

this in here an ID title slug date time

play03:28

to read and and tags all right so we're

play03:31

going to go into application. properties

play03:33

here and set up that first data source

play03:35

so I'm going to go in here and say

play03:37

spring. datas source. URL is going to be

play03:40

equal to

play03:42

H2 sorry H2 memory we'll call this uh

play03:46

blog so I have my first data source and

play03:48

then I need a username of SE essay and a

play03:51

password of nothing now normally you

play03:53

don't need to do this you can actually

play03:55

just change uh the generate so I might

play03:58

come in here and say say

play04:01

spring dot generate unique name equals

play04:05

the false and then you can set the name

play04:07

I'm doing this because we're going to

play04:09

see some uh distinction between the

play04:12

first one and the second one when we get

play04:14

to adding that second database so I have

play04:16

that in place I'm going to need a schema

play04:19

so I'm going to come in here and create

play04:21

a new resource now normally I would just

play04:23

name this schema.sql but because we have

play04:25

two different schemas here I'm going to

play04:26

be a little bit more explicit I'm going

play04:28

to go ahead and call this the blog

play04:30

schema.sql and I'm going to go ahead and

play04:33

insert some code here this is just

play04:36

creating a new table called post with

play04:38

those columns in it I'm also going to

play04:40

insert a single Post in there this is my

play04:42

Hello World post and if we go ahead and

play04:46

save that we should be good finally I'm

play04:48

going to need a way to read and persist

play04:51

data to the database I'm going to go

play04:53

ahead and create a post service this is

play04:56

a class this is going to be marked with

play04:58

the at service annotation

play05:00

we are going to get an instance of the

play05:03

jdbc client we'll call this jdbc client

play05:06

we'll get it through Constructor

play05:08

injection and then we just need a couple

play05:10

of methods to be able to uh access and

play05:13

persist data to and from the database so

play05:16

I've done uh videos on this in the past

play05:18

even in the last one we took a look at

play05:19

it I'm not going to get into kind of how

play05:21

the jdbc client is working we're going

play05:24

to talk more about how to configure

play05:25

multiple ones of these so I have some uh

play05:28

basic cred methods in here find all find

play05:31

by ID create update and delete so with

play05:34

that I think we have everything we need

play05:36

to uh at least test out this initial

play05:39

connection to the first database so if I

play05:42

go into

play05:43

application I can go ahead and create a

play05:46

new command line Runner so I'm going to

play05:48

say Bean command line Runner command

play05:50

line Runner and then we are going to

play05:53

return

play05:55

args co-pilot doing its thing that's not

play05:58

what I want

play06:00

um and then there so I need to get an

play06:02

instance of that post

play06:04

repository uh post repository did I not

play06:08

I'm sorry Post Service Dan come on you

play06:12

got this post service Post Service uh

play06:14

and now we can go ahead and use that so

play06:17

all I want to do is get out all the

play06:19

posts so post service. findall go ahead

play06:22

and print them out and we should be able

play06:24

to go ahead and run this application uh

play06:27

if everything works we should see them

play06:29

and we did not so let's figure out

play06:31

what's going on uh URL must start with

play06:34

jdbc did I not start with jdbc I did not

play06:38

so gdbc colon H2 in memory blog let's

play06:43

try that again all right we got another

play06:45

error here table post not found let's

play06:49

find out what's going on with our schema

play06:52

drop table if exists posts create our

play06:55

table posts insert into post create

play06:59

table post that looks fine uh insert

play07:03

into

play07:05

post that looks okay okay so um we are

play07:10

going to have to name this schema for

play07:11

now I forgot in my Infinite Wisdom here

play07:16

we need to pick this up by convention we

play07:18

will rename it later so that we can be

play07:20

distinct and have two separate schemas

play07:22

that's what I have it named in the final

play07:25

uh code that of course you can find in

play07:27

the description below but that post

play07:30

table was not there because we weren't

play07:31

reading a schema to go ahead and set

play07:33

that up so let's see if we can go ahead

play07:35

and do that and now we get our single

play07:38

post that we are um outputting there to

play07:42

the command line all right so we know

play07:43

that that works but I want to kind of

play07:44

point out one other thing here so I'm

play07:46

going to create another Bean I'm going

play07:48

to create another command line Runner

play07:50

we'll call this the DS command line

play07:53

Runner uh stands for data source so

play07:56

we're going to return args I don't want

play07:58

to print anything

play08:00

out so let's just say that and what I'm

play08:03

going to do is I'm going to get access

play08:05

to the data source so we talked about

play08:07

this in the previous video if you want

play08:08

to kind of go back and look at that you

play08:10

can kind of the history of being able to

play08:12

connect to a database in Java in Spring

play08:15

what that means what do you need to make

play08:17

that happen and one of those things that

play08:19

you need is a data source now you would

play08:22

normally have to create this on your own

play08:24

in an application like Java but in

play08:26

Spring Boop because we use Auto

play08:28

configuration we see that you have uh H2

play08:31

on the class path uh there's the jwc API

play08:34

there um and because of that we will go

play08:37

ahead and create a data source for you

play08:38

this uh reads the properties from

play08:40

application. properties it knows how to

play08:43

connect to a database now we create that

play08:44

data source be for you uh this is great

play08:48

but if you have to uh go ahead and do

play08:51

some configurations you might have to

play08:52

create your own data source or in this

play08:54

case create two different data source

play08:56

beans to talk to two different data

play08:58

sources but I just want to show you the

play09:00

original data source here uh this is the

play09:03

data source that uh was created by

play09:05

Spring and as you can see copilot is on

play09:09

its game today I want to just print out

play09:12

the connection info so get connection.

play09:15

metadata. G.G URL will tell us exactly

play09:19

where this database is connecting to and

play09:22

if we look at this we can see of course

play09:24

we know this but jwc H2 in memory blog

play09:28

this going to be important because what

play09:30

I want to do is over basically override

play09:33

the connection to the database we can

play09:34

create our own data source Bean so we

play09:36

can then create a second data source

play09:39

beam uh this is going to be important

play09:41

I'll come back to this method so we can

play09:43

actually get access to both data sources

play09:46

and see that the connection URLs are

play09:48

indeed pointing to the right database

play09:51

all right so with that let's go ahead

play09:53

and reconfigure this uh to use our own

play09:55

data source uh and then we can go ahead

play09:57

and add a second data source all right

play09:59

so let's close some of this down I'm

play10:01

going to introduce a new package here

play10:03

let's go ahead and call this

play10:05

subscriber we're going to create a new

play10:08

uh subscriber record in here we'll call

play10:10

this subscriber we'll say this is a

play10:13

record I'll go ahead and paste some info

play10:15

in here we'll go ahead and create a

play10:17

subscriber service so

play10:20

subscriber service we'll this will be a

play10:24

class we'll mark this with that service

play10:27

and then we'll come back to this in a

play10:29

second so we're going to need to um

play10:32

basically update this I want to rename

play10:35

this to kind of what I was talking about

play10:37

before we're going to have a Blog

play10:39

specific schema so blog. schema.sql and

play10:42

then I'm going to introduce a new schema

play10:44

here so uh

play10:47

subscriber

play10:49

schema.sql and now we have uh two

play10:52

different two different uh schemas for

play10:54

our application I'm going to go ahead

play10:57

and paste this in in and there we go so

play11:00

we have ID name email and the primary

play11:03

keyb and ID so we'll insert into

play11:06

subscriber we're just going to insert

play11:07

one single record that will give us

play11:10

enough to kind of uh give some

play11:11

distinction between the two so now we in

play11:14

the application. properties we currently

play11:16

have the spring. DAT Source now spring.

play11:19

dat source is how we configure a single

play11:21

data source in our application but we're

play11:24

going to need to take a different

play11:25

approach here if we want to go ahead and

play11:27

set up multiple data sour our es so I'm

play11:30

going to go ahead and paste in my

play11:32

properties and we'll walk through them

play11:34

first off I'm going to enable the H2

play11:36

console we want to be able to take a

play11:38

look at the database and see everything

play11:40

and then I'm going to set up two

play11:41

separate connections to two different

play11:43

databases and I'm going to use my own

play11:46

properties here I'm going to say app.

play11:49

dat source and then I'm going to name

play11:51

each database so this could work for

play11:53

three or four if you wanted to but here

play11:55

we have two data sources the first one

play11:58

is blog same properties as before um

play12:01

username and password uh actually we

play12:03

want to

play12:05

say let's say we'll probably need two

play12:08

different connections ah actually let's

play12:10

just leave it at let's say I say

play12:14

blog I say subscriber and then no

play12:18

password for each of them so we have two

play12:20

separate connections here but if we go

play12:23

ahead and run the application now

play12:24

nothing's going to happen right we're

play12:26

still we we see H2 on the class path in

play12:30

fact uh we're not defining anything

play12:32

under spring. data source so a randomly

play12:35

generated uh ID for the data source will

play12:37

get created and no schema will get

play12:39

picked up and things just won't work yet

play12:42

so how do we go ahead and make this

play12:45

happen all right so what we need to do

play12:46

in our main package here is create a new

play12:49

configuration class I'm going to call

play12:51

this uh let's call this data source

play12:55

configuration this is of course going to

play12:57

be a configuration class I'm going to

play13:00

proxy Bean methods equals false and we

play13:03

can are off and running so what we need

play13:06

to do is Define our different

play13:09

connections to our different database so

play13:10

I'm going to start here with our blog

play13:13

and we need to go ahead and set this up

play13:15

so I'm going to create a new Bean now if

play13:18

we're going to create two different data

play13:19

sources we need to mark one as primary

play13:22

this is the primary data source and we

play13:25

need to say where is it going to get its

play13:27

configuration properties from now

play13:29

remember we just Define that in app.

play13:32

datas source. blog so we have that here

play13:36

uh what we're going to do is we're going

play13:38

to say yep copot you are on your money

play13:41

here um so data source properties is how

play13:44

we kind of fill out the data source

play13:46

properties and we're going to return new

play13:49

data source properties but again those

play13:51

are getting pulled from app. datas

play13:53

source. blog right so we we have our

play13:57

properties but we need an actual

play13:59

connection to the database now so we're

play14:01

going to create one more Bean we'll go

play14:03

ahead and say that this is the primary

play14:07

and we are going to use a data

play14:10

source I'm sorry

play14:13

whop we are going to say this is a

play14:17

Hikari

play14:19

Hikari data source and this is going to

play14:24

be called blog data source let's see

play14:27

co-pilot you are saying data source

play14:30

properties that's the blog data source

play14:32

properties this looks pretty good let me

play14:33

just double check it yep so we're just

play14:35

giving it a type we're building it and

play14:37

we're returning it so this will create

play14:40

our data source now there's one more

play14:42

step needed because we are using kind of

play14:45

a special schema right we renamed it we

play14:48

basically want to create this data

play14:50

source and point it to that schema so it

play14:52

can get initialized if we weren't using

play14:54

a separate schema and we were using like

play14:57

something like spring data jpu a where

play14:59

that schema was created for us we could

play15:01

probably skip this step step but we need

play15:04

to do this here so I'm going to say app

play15:06

Bean this is a data source script uh is

play15:11

that database initializer yes um I think

play15:16

you are on your game today co-pilot and

play15:19

again this is kind of pulling from a

play15:20

preious previous project as I as I set

play15:23

this tutorial up so that's what's

play15:24

happening here so we're going to create

play15:26

some new settings we're going to say hey

play15:28

go ahead and look for that schema

play15:29

location in the class path because it's

play15:31

under Source main resources look for

play15:33

something called blog schema.sql go

play15:36

ahead and uh set the mode to embedded

play15:39

because that is what we are using and

play15:42

then this is we don't want to do this

play15:44

here we actually just want to call this

play15:46

data source and let's get this through

play15:49

here and data source now the problem

play15:53

with this is going to be that at the end

play15:57

of the day we might have two two data

play15:59

sources right we only have one right now

play16:00

but we're going to end up having two so

play16:02

I need to be very specific with what

play16:05

data source this is and all I'm going to

play16:07

do is say blog data source so now it

play16:11

knows exactly which one so that gets

play16:13

wired in here automatically for us and

play16:15

then we could pass it to our data source

play16:17

script initializer which just takes the

play16:19

data source and what settings really the

play16:22

settings here is just hey this is

play16:24

embedded this is the schema where I want

play16:26

you to look so with that being said um

play16:30

what are we

play16:33

what's oh

play16:35

I'm we want configuration that's why

play16:38

that didn't work so with that said I

play16:41

think we can um go ahead let's see here

play16:46

let's go ahead and try and run our

play16:48

application and see if it still works as

play16:51

it did before and it does so we still

play16:54

have our um blog post and we are still

play16:57

using our uh connection to our inmemory

play17:01

blog database so great that is data

play17:04

source one now we can start to Define

play17:07

data source 2 which is our subscribers

play17:10

so this is going to look pretty similar

play17:12

I'm just going to say public data source

play17:15

properties uh we'll say data source

play17:18

properties actually let's say subscriber

play17:20

data source properties this is going to

play17:23

be a new data source properties and just

play17:26

like before we need to go ahead and say

play17:29

that this is a bean and where are we

play17:31

going to pull remember we did this

play17:33

before with at configuration properties

play17:36

we are pulling this from app.at source.

play17:39

subscribers that is where that's coming

play17:41

from all right next we need to create

play17:42

another Bean this is going to be the

play17:44

actual data source so this will return

play17:46

I'm just going to show you a different

play17:48

way of doing this um let's see

play17:50

subscriber data source this looks pretty

play17:53

good uh subscriber data source so the

play17:57

qualifier again because we have multiple

play17:59

data source properties now we need to be

play18:01

specific we are going to say hey uh the

play18:04

subscriber data source properties is

play18:07

what you want to use here and then we're

play18:09

going to use a new data source Builder

play18:13

this is just a different example of a

play18:15

way to do this uh looks like I

play18:17

misspelled that uh so we're going to

play18:19

pass in the URL uh and we're going to

play18:22

get the URL from those subscriber

play18:24

properties the the username and the

play18:26

password again just a different way of

play18:28

building a data source and this one it's

play18:30

a a hiari data source this is just a

play18:32

plain data source so just two different

play18:34

ways to do that here so finally we need

play18:36

to do the same thing as we did before we

play18:39

want a data source script database

play18:42

initializer because we want to go ahead

play18:45

and use a different schema for this

play18:47

database so here we're going to go ahead

play18:50

and make sure we have that qualifier

play18:52

again this is the subscriber data source

play18:55

and we're going to set our uh schema

play18:57

location to the class CL path of

play19:00

subscribers. schema let me just make

play19:02

sure I made that plural I did not so

play19:05

let's go ahead and refactor this so it's

play19:08

the same as what I have in the GitHub

play19:11

repository so let's go ahead and

play19:12

refactor that uh we're saying

play19:15

subscribers D schema.sql

play19:18

blog. schema.sql yes okay so so far so

play19:23

good this is exactly what the data

play19:25

source configuration is going to look at

play19:27

look like

play19:29

but now we need to do a couple of things

play19:31

right so first off back in our post

play19:34

service we have this jdbc client we need

play19:37

to be very specific with what jdbc

play19:40

client this is so here I'm going to add

play19:44

a qualifier and I'm going to say this is

play19:47

the blog gdbc client which I don't think

play19:50

we've created yet so let's do that so

play19:53

let's go back over to our application

play19:56

and let's just create each of these I'm

play19:58

going to create a new Bean again this

play20:00

can go in any configuration class I'm

play20:02

kind of doing this for Simplicity sake

play20:04

in the form of a demo but uh yeah you

play20:07

can do this anywhere so this is going to

play20:09

be a jdbc client because again this is

play20:12

similar to the data source when when you

play20:15

only have a single data source we create

play20:17

an instance of the jtbc client that uses

play20:20

that default data source if you have

play20:23

multiple data sources you need to be

play20:24

very specific we need to create our own

play20:26

jdbc client and tell it which database

play20:30

it's going to use so we're going to

play20:32

create a couple of jdbc clients here one

play20:34

is going to be the jdbc client here

play20:37

we're going to pass in a qualifier again

play20:40

we want to be very specific we're saying

play20:43

blog data source and the way that you

play20:45

create a new jdb jdbc client is by using

play20:49

this static Factory method here called

play20:52

create and then you pass in a data

play20:54

source so we want to create a second

play20:56

Bean we this will be of jdbc client

play20:59

we'll call this the subscriber jdbc

play21:02

client right I just want to make sure

play21:05

it's singular yes we're going to add a

play21:07

qualifier here called subscriber data

play21:10

source now we have two days data sources

play21:12

here and then we're going to call the

play21:14

create method and pass in that data

play21:16

source so it looks like we're getting

play21:18

there we're almost there um back in our

play21:20

subscriber service we don't have

play21:22

anything here yet we'll do something

play21:24

similar we'll get a jdbc client we'll

play21:27

have the qualifier for the correct one

play21:30

in my findall method all I'm doing is

play21:32

selecting ID name and email from

play21:34

subscriber and I'm turning that into a

play21:36

list we know that in the schema there's

play21:38

only one so we only have a single row

play21:41

there uh but that's okay let's take a

play21:43

look I'm getting some some little

play21:45

squigglies here telling me something's

play21:47

not

play21:49

right and that's because this is at Bean

play21:53

Dan okay that's a little bit better so

play21:55

now back in my application I'm going to

play21:58

do a couple things one I want to get

play22:00

both data sources here I want to say at

play22:04

qualifier is the blog data source right

play22:11

so let's call this blog data source now

play22:14

I can change this to blog data source

play22:17

and I'm going to get the other one as

play22:19

well which is the subscriber data source

play22:22

so that down here I can basically say uh

play22:25

what is the subscriber data source get

play22:27

that connection and get the metadata URL

play22:30

so let's rerun this and see if those two

play22:32

URLs point to the right database and

play22:34

they do so what this is telling me right

play22:37

now is we've successfully created the

play22:40

appropriate beans to connect to multiple

play22:43

data sources so to round this kind of

play22:45

demo out we have this post service I'm

play22:48

going to get my subscriber service call

play22:52

this subscriber

play22:54

service why did it erase that and what I

play22:58

want to do is get all the subscribers so

play23:00

I'll call subscriber service. findall

play23:03

and we'll go ahead and print those out

play23:05

and let's see what that looks like and

play23:08

there we go so we've printed out all our

play23:10

posts we printed out all our subscribers

play23:13

we have connections to two different

play23:14

databases and if you wanted to kind of

play23:17

examine this further the H2 console is

play23:19

enabled so you can go over to the

play23:21

browser and take a look at each database

play23:24

separately if you wanted to all right

play23:26

and there you have it how to connect to

play23:28

multiple data sources in Spring and then

play23:31

use the jdbc client for multiple data

play23:33

sources I thought that was a really

play23:35

interesting question I love getting

play23:37

questions like that so hey if you're

play23:39

watching a YouTube video like this one

play23:41

and you have questions please leave them

play23:43

in the comments below you can also find

play23:45

me at danve gmail.com you can find me on

play23:48

Twitter Spring Office hours.i is our

play23:52

podcast so go ahead and check that out

play23:54

there are a bunch of different ways you

play23:55

can give me questions and I'm trying to

play23:57

find a good way to kind of wrangle those

play23:59

questions into a single place where I

play24:01

can get to them quicker so please bear

play24:03

with me if you do with that if you learn

play24:06

something new today friends do me a big

play24:08

favor give me a thumbs up on this video

play24:11

subscribe to the channel and as always

play24:13

happy

play24:22

coding

Rate This

5.0 / 5 (0 votes)

Do you need a summary in English?