JDBC 5 data source and data pool (web basic learning note 11), jdbc 5

Source: Internet
Author: User
Tags manage connection

JDBC 5 data source and data pool (web basic learning note 11), jdbc 5
I. Why are data sources and connection pools used?

Currently, the developed applications are basically data-based and require frequent database connection. If each operation is connected to the database and then disabled, the performance will be limited. Therefore, we must find a way to reuse
Database connection. In view of this situation, the concept of data source and connection pool is proposed. The data source and connection pool can be used to reuse database connections.

Ii. Concepts of data source and Connection Pool

In fact, the data source and connection pool are two different concepts. Some may confuse them.
The data source is used to connect to the database and obtain the Connection object, which is expressed in Java using the javax. SQL. DataSource interface. With the data source, we don't need to write other code for connecting to the data. We can directly obtain the connection from the data source. In addition, no matter what type of database is used, the same method is used to obtain the database connection object. How can we obtain the data source objects? Generally, it is created by a container. We use JNDI in the program to obtain it. The connection object created through the data source can be automatically put into the connection pool for unified management.

The Connection pool is used to manage Connection objects. The Connection pool can obtain connections from the data source. The Connection pool can have several database Connection objects that can be reused. When an application needs to be connected, it applies to the connection pool. If there is an idle connection in the connection pool, it is allocated to the application. If not, it may need to wait in the queue. If a Connection object is obtained from the Connection pool, the Connection can be returned to the Connection pool by calling the close () method of Connection after use, in this way, the connection object is programming idle, and you can wait for the next request. The connection object in the connection pool will always be connected to the database, so that frequent connections and shutdown are avoided. However, these connection objects occupy a certain amount of memory space. Therefore, we need to determine the maximum number of connections in the connection pool based on the concurrent access traffic of the database and the hardware conditions of the server.

3. Data Source and connection pool. One is used to create a connection object, and the other is used to manage the connection object.

 

4. to configure data source 4.1 in Tomcat, follow these steps:
  • (1) Add the database driver file to Tomcat
  • (2) Configure conf/context. xml of Tomcat
<Context><Resource name="jdbc/books"auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="pbdevj" password="pwd1234"driverClassName="oracle.jdbc.OracleDriver "url="jdbc:oracle:thin:@localhost:1521:orcl "/></Context>
Attribute name Description
Name The JNDI name of the specified Resource.
Auth

Specify the Manager for Resource management (Container: created by the Container
And Management | Application: created and managed by Web applications)

Type Java class to which the specified Resource belongs
MaxActive Specifies the maximum number of active database connections in the connection pool.
MaxIdle Specifies the maximum number of idle database connections in the connection pool.
MaxWait

The maximum time for idle connections in the connection pool.
An exception is thrown. The value is-1, indicating that you can wait for an indefinite period of time.

  • (3) configure the web. xml file of the application (optional) (Note: The following configuration is not required for the new version of Tomcat)

  • (4) obtain the connection object using JNDI
import javax.naming.Context;import javax.naming.InitialContext;import javax.sql.DataSource;//…Context ic = new InitialContext();DataSource source = (DataSource)ic.lookup("java:comp/env/jdbc/books");Connection connection = source.getConnection();

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.