The data originates from the connection pool

Source: Internet
Author: User
Tags manage connection

Why use data sources and connection pooling

The applications we are currently developing are basically data-based and need to be connected to the database frequently. If you connect to the database each time and then close, this performance is bound to be limited. Therefore, we must try to reuse the database connection. Therefore, the concept of data source and connection pool is proposed for this case. You can use data sources and connection pooling to achieve the purpose of reusing database connections.

Data source and connection pooling concepts

In fact, data sources and connection pooling are two different concepts. Some people will confuse them. The data source is used to connect to the database and obtain the connection object, which is represented in Java using the Javax.sql.DataSource interface. With the data source, we do not need to write another connection data code, directly from the data source to get a connection. And, regardless of the database used, the database connection object is obtained in the same way. So how does the data source object get it? Typically created by a container, we use Jndi in the program to get it. Connection objects created from the data source can be automatically placed in the connection pool for unified management. Connection pooling is used to manage connection objects, connection pooling can be obtained from the data source, the connection pool can have several database connection objects, these connection objects can be reused. When an application needs to connect, it requests the connection pool, and if there is an idle connection in the connection pool, it is assigned to the application, and if not, it may need to wait in the waiting queue. And if you get a connection object from the connection pool and wait until it is finished, you can return the connection to the connection pool by calling the close () method of connection, so that the connection object is programmed to be idle and can wait for the next request. Connection objects in the connection pool remain connected to the database so that frequent connections and shutdowns are avoided. However, these connection objects occupy a certain amount of memory space. Therefore, we want to determine the maximum number of connections in the connection pool based on the concurrency of the database and the hardware conditions of the server. Data sources and connection pools, one for creating connection objects, and one for managing connection objects.

To configure a data source in Tomcat
Learn what data sources and connection pooling are, and then learn how to use Tomcat to configure data sources and how to use them in your programs. The steps for using a data source in Tomcat are as follows: (1) Adding a database driver (2) to the Tomcat configuration Tomcatconf/context.xml

1 <Context>2 <Resourcename= "Jdbc/books"3 Auth= "Container"type= "Javax.sql.DataSource"maxactive= "+"Maxidle= "+"maxwait= "10000"username= "PBDEVJ"Password= "pwd1234"4 Driverclassname= "Oracle.jdbc.OracleDriver"5 URL= "Jdbc:oracle:thin: @localhost: 1521:orcl"/>6 </Context>

name Specifies the Jndi name of the resource
Auth Specify manager for resource management (Container: Created and managed by container | Application: Created and managed by web apps)
type Specifies the Java class to which resource belongs
maxactive Specifies the maximum number of database connections that are active in the connection pool
maxidle Specifies the maximum number of database connections that are idle in the connection pool
maxwait Specifies the maximum length of time that a connection in a connection pool is idle, an exception is thrown over this time, and a value of 1 indicates that it can wait indefinitely

(3) Configuring the application's Web. xml file (optional)
(Note: The new version of Tomcat does not need to be configured as follows)

(4) Use Jndi (Java naming and connection interface) to get the connection object

Import Javax.naming.context;import javax.naming.initialcontext;import javax.sql.datasource;//initialization Context IC = new InitialContext ();
Gets the data source object associated with the data source DataSource Source = (DataSource) ic.lookup ("Java:comp/env/jdbc/books"); Connection Connection = Source.getconnection ();

The data originates from the connection pool

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.