是否有多个JdbcTemplate实例? [英] Multiple JdbcTemplate instances or not?

查看:156
本文介绍了是否有多个JdbcTemplate实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我的理解, DataSource JdbcTemplates 都是 threadsafe ,所以您可以配置 JdbcTemplate 的单个实例,然后将此共享引用安全地注入多个DAO(或存储库)。另外 DataSource 应该是一个Spring单例,因为它管理连接池。

From what I understand, both DataSource and JdbcTemplates are threadsafe, so you can configure a single instance of a JdbcTemplate and then safely inject this shared reference into multiple DAOs (or repositories). Also DataSourceshould be a Spring singleton, since it manages the connection pool.

官方 Spring文档JdbcTemplate最佳实践解释替代方案(手册的摘录用斜体字,方括号之间的注释:

The official Spring Documentation JdbcTemplate best practices explains the alternatives (excerpts from the manual are in italics, and my notes between square brackets:


  • 在你的数据源中配置一个数据源Spring配置文件,然后依赖注入共享的DataSource bean到你的DAO类; JdbcTemplate是在DataSource的setter中创建的。 [使用XML配置,这会导致多个JdbcTemplate实例,因为在数据源setter中有新的JdbcTemplate(dataSource)]

  • 使用组件扫描和注释支持依赖注入。在这种情况下你使用@Repository注释该类(这使得它成为co的候选者mponent-scanning)并使用@Autowired注释DataSource setter方法。 [也是这种情况导致多个JdbcTemplate实例]

  • 如果您使用的是Spring的JdbcDaoSupport类,并且您的各种JDBC支持的DAO类从它扩展,然后您的子类从JdbcDaoSupport类继承setDataSource(..)方法。您可以选择是否继承此类。 JdbcDaoSupport类仅作为方便提供。 [因为你为每个扩展它的类都有一个JdbcDaoSupport实例,所以对于派生类的每个实例都有一个JdbcTemplate实例(参见 JdbcDaoSupport的源代码)]

  • configure a DataSource in your Spring configuration file, and then dependency-inject that shared DataSource bean into your DAO classes; the JdbcTemplate is created in the setter for the DataSource. [with XML configuration and this leads to multiple JdbcTemplate instances, since in the datasource setter there is new JdbcTemplate(dataSource)]
  • use component-scanning and annotation support for dependency injection. In this case you annotate the class with @Repository (which makes it a candidate for component-scanning) and annotate the DataSource setter method with @Autowired. [also this case leads to multiple JdbcTemplate instances]
  • If you are using Spring's JdbcDaoSupport class, and your various JDBC-backed DAO classes extend from it, then your sub-class inherits a setDataSource(..) method from the JdbcDaoSupport class. You can choose whether to inherit from this class. The JdbcDaoSupport class is provided as a convenience only. [since you've an instance of JdbcDaoSupport for each class extending it, there is an instance of JdbcTemplate too for each instance of the derived class (see source code for JdbcDaoSupport)]

但是,稍后的注释会阻止刚出现的所有选项:

However, a later note, discourages all the options just presented:


配置完成后,JdbcTemplate实例就是线程安全的。如果您的应用程序访问多个数据库(可能需要多个数据源,以及随后多个不同配置的JdbcTemplates),您可能需要多个JdbcTemplate实例。

Once configured, a JdbcTemplate instance is threadsafe. You may want multiple JdbcTemplate instances if your application accesses multiple databases, which requires multiple DataSources, and subsequently multiple differently configured JdbcTemplates.

在其他单词,刚才提出的所有选项将导致多个JdbcTemplate实例(每个DAO一个),并且在文档说明在使用单个数据库时不需要这样做。

In other words, all the options just presented will result in having multiple JdbcTemplate instances (one per DAO), and just after the docs says that is not necessary when working with a single database.

我要做的是直接将 JdbcTemplate 注入需要它的各种DAO,所以我的问题是,是吗?好的吗?而且,您是否也认为Spring参考文档是自相矛盾的?或者是我的误解?

What I would do is inject directly JdbcTemplate to the various DAOs needing it, so my question is, is it OK to do so? And also, do you also think that the Spring reference documentation is self-contradicting? Or is my misunderstanding?

推荐答案

IMO,将JdbcTemplate注入您的(多个)DAO是没有问题的。当您需要运行数据库查询时,该模板用于将DAO连接到物理资源(数据库连接)。因此,如果SessionFactory和TransactionManager配置正确,您将不会遇到并发问题 - Spring管理您使用持久层所需的bean的生命周期。使用模板的优点是:

IMO, there is no problem to inject JdbcTemplate to your (multiple) DAO(s). The template is used to "wire" your DAO to the physical resource (db connection) when you need to run db query. So if the SessionFactory and the TransactionManager are properly configured you will not run into concurrency problems - Spring manages the lifecycle of the beans you need for working with you persistence layer. The advantages of using a template are:


  1. JDBC模板管理与数据库自动交互所需的物理资源,例如:创建和释放数据库连接。

  2. Spring JDBC模板将标准JDBC SQLExceptions转换为RuntimeExceptions。这使您可以更灵活地对错误做出反应。 Spring JDBC模板还将特定于供应商的错误消息转换为更易理解的错误消息

这篇关于是否有多个JdbcTemplate实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆