数据源和连接池DataSource Objects and Connection Pools

https://docs.oracle.com/javaee/6/tutorial/doc/bncjj.html

DataSource Objects and Connection Pools

To store, organize, and retrieve data, most applications use a relational database. Java EE 6 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.

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.


         在JDBC API中,DataSource(数据源)对象用于访问databases数据库。DataSource拥有一些属性用来描述和表示真实的数据资源(这里应该是databases),这些属性主要包括数据库服务器地址,数据库名字,数据库服务器通信协议等。

         应用程序通过connection连接真实的数据资源,DataSource(数据源)是连接真实数据资源的媒介。在最基本的DataSource实现中,通过其getConnection方法即可连接真实数据资源(数据库)。

    数据源连接池( DataSource connection pooling)也能现实数据库连接。数据源连接池对象同样通过getConnection返回一个连接PooledConnection,该连接用法与普通DataSource(数据源)产生的connection用法一致,需要显式的关闭。不同的是,PooledConnection关闭时,该连接将被返回连接池中作为可重用的连接。在下一次getConnection调用时,只需从连接池中取出其中一个PooledConnection即可。如此一来,连接池能够避免每次数据请求时都创建一个新的物理连接,使应用程序更加高效。

        JDBC连接池(connection pool)是针对具体数据库的一组可重用连接( connections)组成,服务程序维持该连接池以提高效率,每次请求连接,只需要从池中取一个现有的连接即可。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值