在Spring框架中创建多个数据源是昂贵的。

huangapple go评论82阅读模式
英文:

Creating multiple DataSource in Spring framework is expensive

问题

在我的Spring MVC应用程序中,我有两个不同的MySQL数据库连接。
一个用于整个数据存储,另一个用于登录历史记录,每天在特定时间段插入数据。

我已经为jdbcTemplate创建了两个单独的数据源(一个通过spring.xml,另一个通过配置类)。这两个数据源都会在Tomcat启动时创建。

1)在一个应用程序中创建多个数据源(一个通过spring.xml,另一个通过@Configuration类)是否会很昂贵?
2)是否最好只在插入数据时连接和关闭第二个数据库(使用普通的JDBC连接)?
3)在一个应用程序中同时使用spring.xml和基于注解的配置是否是不良实践?

有人可以帮助我吗?

英文:

In my spring mvc application , I have two different MySql database connections.
One is for whole data storage and other one is for login history- which insert data daily in a specific period.

I have created two separate Datasource (one via spring.xml and other via configuration class) for jdbcTemplate. Both will created at the time of tomcat start.

  1. Does creating multiple Datasource (one via spring.xml and other one via @configuration class) in one application is expensive ?
  2. Will it be better to connect and close (normal jdbc connection) the second database only at the time insertion ?
  3. Is it bad practice to use both spring.xml and annotation based configuration in one application?

Can someone help me for this?

答案1

得分: 1

  1. 创建多个数据源(一个通过spring.xml,另一个通过@Configuration类)在一个应用程序中会很昂贵吗?
    => 不会,不过应该使用@Configuration类,我认为这样更容易管理。

  2. 是否在仅在插入时连接并关闭第二个数据库(普通jdbc连接)会更好?
    => 不会,不应该这样做。初始化连接的代价相当高,你可以为两个数据源都初始化连接池。

  3. 在一个应用程序中同时使用spring.xml和基于注解的配置是否是不良实践?
    => 同问题1,两者都可以。

英文:
  1. Does creating multiple Datasource (one via spring.xml and other one via @configuration class) in one application is expensive ?
    => No, It doesn't. but should use Configuration classes, It's easier to manage IMO

  2. Will it be better to connect and close (normal jdbc connection) the second database only at the time insertion ?
    => No, It won't. To init a connection quite costly, you can init connection pools for both DS

  3. Is it bad practice to use both spring.xml and annotation based configuration in one application?
    => Same 1, both are OK

huangapple
  • 本文由 发表于 2020年10月7日 21:17:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/64244861.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定