JDBC Advanced Applications-Data sources (connection pooling)

Source: Internet
Author: User
Tags connection pooling manage connection

Data source (Connection pool)

(1) When we used JDBC for database access, we first obtained a JDBC connection, and after performing additions and deletions, we closed the database connection.
(2) But the ResultSet, statement, and conncetion we use are time-consuming and resource-intensive to create and close. Especially in enterprise applications, it is generally a lot of people at the same time access, if everyone access to them to establish a new connection, and after the use of the closed, if a connection is established and closed in 0.1 seconds, then 1000 people is 100 seconds, if it is to host tens of thousands of people at the same time access to the Sina site.
As a result, JDBC can handle small applications and, if it is enterprise-level development, requires a more efficient approach.
(3) Database connection pooling is a solution.
First, when the program starts, we generate some connections, and if a user request arrives, they are used directly, and then put back in the pool, saving the time it takes to build the connection and close the connection.

Enterprise-level development requirements for the model layer:

Complete CRUD operations on the database
Ability to handle various errors that occur in the database can be flexibly modified configuration
Easy-to-use tools available

High performance requires an efficient, robust data access layer where traditional JDBC is unable to meet demand.

Working with data sources (connection pooling)

Definition: Data Source, also known as Connection pool (DBCP, Database Connection Pool)
To manipulate the database, instead of creating the connection directly, "request" a connection in the connection pool. If the connection pool is free of connection, the connection is returned, otherwise a new connection is created. After use, the program "frees" the connection, and the connection pool calls the connection multiple times and delivers other thread usage to reduce the number of creation and disconnection attempts.

Data Source (DataSource)

The Javax.sql.DataSource interface is responsible for establishing a connection to the database
Getting a connection from a Tomcat data source
To save the connection in the connection pool
Connection pooling is provided by the container to manage connection objects in the pool

Note: If there are already several connections in the connection pool, the application obtains an idle connection directly from the connection pool, then accesses the database, obtains the result of the access, and puts the connection back into the pool.

How to get DataSource objects
Data source is provided by Tomcat and cannot be created in the program
Using Jndi to get datasource references

What is Jndi

JNDI (Java naming and directory Interface,java naming and directory interfaces) is a set of APIs that access naming and directory services in Java applications
The lookup () method of the Javax.namming.Context interface

Get a Connection object using Jndi
//Use Jndi and data sources to import packages import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; Public  class titlesbean {    PublicList GetTitles () {Try{//Get connection objects through context, DataSourceContext IC =NewInitialContext (); DataSource Source = (DataSource) ic.lookup ("Java:comp/env/jdbc/books");        Connection Connection = Source.getconnection (); Titlesquery = Connection.preparestatement ("SELECT * FROM Titles"); ResultSet results = Titlesquery.executequery ();//Assign a value to the properties of the Bookbean object and add it to the Titleslist}Catch(SQLException exception)         {Exception.printstacktrace (); }Catch(Namingexception namingexception) namingexception.printstacktrace (); }finally{Closeconn (); }   }   }
Configuration for Jndi configuration in Tomcat Conf/context.xml


Adding database driver files
Add a database-driven. jar file to the lib of Tomcat
Configuration of the application's Web. xml file
configuring in Web. xml

JDBC Advanced Applications-Data sources (connection pooling)

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.