简体   繁体   English

java 中的数据源是什么? 有人可以用简单的语言向我解释吗?

[英]What is a datasource in java? Can someone please explain me in simple language?

What is a DataSource in java? java 中的数据源是什么? Can someone please explain me in simple language?有人可以用简单的语言向我解释吗?

DataSource implementation classes allow you to use connection pool and loose coupling for connectivity . DataSource 实现类允许您使用连接池和松散耦合进行连接

  1. Most of the times we are looking for loose coupling for connectivity so that we can switch databases easily.大多数时候,我们正在寻找连接的松散耦合,以便我们可以轻松地切换数据库。

  2. Creating connections can be heavy process and it is not a good idea to let every part of program create its own connections which can lead to resource starvation and slow performance.创建连接可能是一个繁重的过程,让程序的每个部分都创建自己的连接不是一个好主意,这可能导致资源匮乏和性能下降。 that's why we use connection pooling.这就是我们使用连接池的原因。 most database drivers provide datasource implementation classes that can be used in connection pool.大多数数据库驱动程序都提供了可以在连接池中使用的数据源实现类。

Data Source is explained very well here.数据源在这里解释得很好。 Please go through the link Data Source in Java请通过 Java 中的链接数据源 go

DataSource is an abstraction, a way to represent getting access to data from some source with some connection. DataSource是一种抽象,一种表示通过某种连接从某个源访问数据的方式。 It frees you to not worry about,它让你不用担心,

  1. Having to think about how connection is established and will be maintained from many varying sources (think different databases, cloud storage resources)必须考虑如何建立连接以及如何从许多不同的来源维护连接(想想不同的数据库、云存储资源)
  2. Not worrying about what will happen if multiple clients request data from the same source and if those clients will get a proper connection object, or null - because the number of connections, there optimization and scheduling is a challenge on its own.不用担心如果多个客户端从同一源请求数据以及这些客户端是否会获得正确的连接 object 或 null 会发生什么 - 因为连接数、优化和调度本身就是一个挑战。
  3. In the same respect, easier to manage connection timeout settings.在同样的方面,更容易管理连接超时设置。

Database vendors and data providers need not worry about you learning their specific API if they can go ahead and implement the DataSource interface which describes how your service/server may connect to them, and you need not worrying about connection intricacies and so on.数据库供应商和数据提供者不必担心您了解他们的具体 API,如果他们可以提前 go 并实现描述您的服务/服务器如何连接到他们的DataSource接口,您不必担心连接复杂性等。

Here is a good read from the official documentation itself on Oracle .这是Oracle官方文档本身的一个很好的阅读

To answer your main question, I'll summarize in the below 3 points,为了回答您的主要问题,我将总结以下 3 点,

  1. An interface to be implemented by a vendor由供应商实现的接口
  2. A factory pattern for connections to the actual data source连接到实际数据源的工厂模式
  3. Its main functions to be implemented and of concern are just getConnection() and getConnection(String username, String password) where both return the Connection object ( read ).它要实现和关注的主要功能只是getConnection()getConnection(String username, String password) ,它们都返回Connection object ( 读取)。

According to documentation , the DataSouce interface is something like DriverManager .根据文档DataSouce接口类似于DriverManager In most applications, it is connection to the database.在大多数应用程序中,它是与数据库的连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM