Java EE 7 教程 第二部分 平台基础 第3章 创建资源 第3.2节 数据源对象和连接池

原文:https://docs.oracle.com/javaee/7/tutorial/resource-creation002.htm
翻译:石卓林 shizhuolin@hotmail.com

3.2 DataSource Objects and Connection Pools

To store, organize, and retrieve data, most applications use a relational database. Java EE 7 components may access relational databases through the JDBC API. For information on this API, see http://www.oracle.com/technetwork/java/javase/tech/index-jsp-136101.html.

In the JDBC API, databases are accessed by using DataSource objects. A DataSource has a set of properties that identify and describe the real-world data source that it represents. These properties include such information as the location of the database server, the name of the database, the network protocol to use to communicate with the server, and so on. In the GlassFish Server, a data source is called a JDBC resource.

Applications access a data source by using a connection, and a DataSource object can be thought of as a factory for connections to the particular data source that the DataSource instance represents. In a basic DataSource implementation, a call to the getConnection method returns a connection object that is a physical connection to the data source.

A DataSource object may be registered with a JNDI naming service. If so, an application can use the JNDI API to access that DataSource object, which can then be used to connect to the data source it represents.

DataSource objects that implement connection pooling also produce a connection to the particular data source that the DataSource class represents. The connection object that the getConnection method returns is a handle to a PooledConnection object rather than being a physical connection. An application uses the connection object in the same way that it uses a connection. Connection pooling has no effect on application code except that a pooled connection, like all connections, should always be explicitly closed. When an application closes a connection that is pooled, the connection is returned to a pool of reusable connections. The next time getConnection is called, a handle to one of these pooled connections will be returned if one is available. Because connection pooling avoids creating a new physical connection every time one is requested, applications can run significantly faster.

A JDBC connection pool is a group of reusable connections for a particular database. Because creating each new physical connection is time consuming, the server maintains a pool of available connections to increase performance. When it requests a connection, an application obtains one from the pool. When an application closes a connection, the connection is returned to the pool.

Applications that use the Persistence API specify the DataSource object they are using in the jta-data-source element of the persistence.xml file:

<jta-data-source>jdbc/MyOrderDB</jta-data-source>


This is typically the only reference to a JDBC object for a persistence unit. The application code does not refer to any JDBC objects.

3.2 数据源对象和连接池

为了存储,组织,和检索数据, 多数应用使用关系数据库. Java EE 7 组件可以通过JDBC API访问关系数据库. 关于此API信息, 查看 http://www.oracle.com/technetwork/java/javase/tech/index-jsp-136101.html.

在JDBC API中,使用数据源对象访问数据库. 数据源作为真实数据代理,其存在识别和描述真实世界数据源的属性集. 这些属性包含例如数据库服务器位置,数据库名称,服务器网络通讯协议,等等. 在GlassFish服务器中,数据源被认为是JDBC资源.

应用程序连接数据源访问数据, 数据源组件对象可以被认为是连接到多种数据源工厂的实例代理. 在基本的数据源实现中, 调用getConnection方法返回一个连接到真实数据源的物理连接对象.

数据源对象可以在JNDI命名服务中注册. 如果这样, 应用就可以使用JNDI API访问此数据源对象, 其就成为连接到数据源的代理.

DataSource对象集中管理连接并且产生具体数据源的DataSource类代理. 连接对象的getConnection方法返回PooledConnection对象,而不是产生一个新的物理连接.应用程序在同一路径时使用的连接对象其实是同一个物理连接, 连接池在应用程序代码以外无效. 如同所有连接那样, 连接池的连接总是需要被明确关闭. 当应用程序关闭一个连接时,此连接返回到连接池,并设置为可用. 下次getConnection被调用时, 如果存在一个可用连接则从池中返回它. 因为连接池避免在每次请求时创建新连接,所以应用程序性能得到提高.

一个JDBC连接池是特定数据库的可重用连接组, 因为创建各自的新物理连接十分耗费时间. 服务器维持一个有效连接池以提高性能. 当请求连接时, 应用程序从池中获取一个连接. 当应用程序关闭连接时,此连接被返回到池中.

应用程序持久化API规定在persistence.xml文件的jta-data-source元素中使用DataSource对象:

<jta-data-source>jdbc/MyOrderDB</jta-data-source>


这个典型例子例子中只涉及JDBC持久化单元,在应用代码中并不关系到其他任何JDBC对象

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload the CAPTCHA.

Proudly powered by WordPress   Premium Style Theme by www.gopiplus.com
渝公网安渝公网安备 50010702500270号 渝ICP备09056628号-7