Data Source and connection pool, data source connection pool

Source: Internet
Author: User
Tags driver manager manage connection

Data Source and connection pool, data source connection pool

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 do not need to write other connection database code, and we can directly obtain the connection from the data source. Moreover, no matter what kind of database is used, all get database connection objects in the same way.

Data Source objects are generally created by containers and can be obtained using JNDI in a program. 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 connect, it applies to the connection pool. If the connection pool has idle connections, it will be allocated to the application. If not, it may need to wait in the queue. If you get a Connection object from the Connection pool and wait until it is used up, you can return the Connection to the Connection pool by calling the close () method of Connection, in this way, the connection object is programming idle, and you can wait for the next connection request. The connection object in the connection pool will always be connected to the database, so that frequent Connection boxes are avoided. However, these connection objects occupy a certain amount of memory space. Therefore, we need to consider 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.

The data source and connection pool are used to create connection objects, and the other is used to manage connection objects.

To use a data source in Tomcat, follow these steps:

1. Add the database driver file to Tomcat and import the database driver package under the lib directory of Tomcat.

2. Configure the conf/context. xml file of Tomcat and add the following code to the file:

<Context><Resource name="jdbc/books" auth="Container" type="javax.sql.DataSource"maxActive="100" maxIdle="30" maxWait="10000" username="myhr" password="myhr"driverClassName="oracle.jdbc.OracleDriver"url="jdbc:oracle:thin:@localhost:1521:orcl"/></Context>

Name: The JNDI name of the Resource. Auth: specify the Resource Manager (Container: created and managed by the Container, and Application: created and managed by the WEB Application ). Type: Specifies the java class to which the 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: specifies the maximum idle time in the connection pool. If this time is exceeded, an exception is thrown. The value-1 indicates an indefinite wait.

3. Use JNDI to obtain the connection object.

import java.sql.Connection;import java.sql.SQLException;import javax.naming.Context;import javax.naming.InitialContext;import javax.naming.NamingException;import javax.sql.DataSource;public class JNDIConnection {public Connection JNDIgetConnection() {Connection conn = null;Context ic;try {ic = new InitialContext();DataSource ds = (DataSource) ic.lookup("java:comp/env/jdbc/books");conn = ds.getConnection();} catch (NamingException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();}return conn;}}

Then we can use the connection object and perform a series of operations on the database.


1. What is the data source? 2. What is the data source configuration? 3 What is the relationship between the data source and the connection pool?

Using TCP to connect your applications to the database is expensive (time-consuming). Microsoft developers can use the connection pool to repeatedly use the database connection. A TCP connection is created only when no valid connection exists in the connection pool. When a connection is closed, it will be put into the pool, and it will still maintain the connection with the database, which can reduce the number of TCP connections to the database.

Of course, you should pay attention to the connections that you forget to close. You should close them immediately after each connection is used up. I want to emphasize that no matter what people say. the GC (Garbage Collector) in the. net framework will call the Close or Dispose method of the connection object after you use up the connection object to explicitly Close your connection. Do not expect the CLR to close the connection within the expected time. Although the CLR will eventually destroy the object and close the EDGE connection, we cannot determine when it will actually do these things.

To optimize the connection pool, there are two rules. First, open the connection, process the data, and then close the connection. If you have to open or close the connection multiple times in each request, this is better than always opening an EDGE connection, and then passing it to each method. Second, use the same connection string (or use the same user ID when you use integrated authentication ). If you do not use the same connection string, for example, you use a connection string based on the login user, this will not take advantage of the connection pool optimization function. If you use the integration argument, you cannot make full use of the optimization function of the connection pool because there are many users .. Net clr provides a data performance counter, which is very useful when we need to track program performance characteristics, including connection pool tracking.

No matter when your application will be connected to resources on another machine, such as databases, you should focus on optimizing the time spent on resource connection, receiving and sending data, and the number of rounds. Optimize every process hop in your application. It is the starting point to improve the performance of your application.

The Application Layer connects to the data layer, transmits data to the corresponding class instance, and the business processing logic. For example, in Community Server, You need to assemble a Forums or Threads set and then apply the business logic, such as authorization. More importantly, you need to complete the cache logic here.

The data source is the same as the data connection pool.

For example:
ODBC programming language interface for database connection

ODBC Overall Structure
Applications
Execute the processing and call the odbc function, submit the SQL statement, and retrieve the result.

Driver Manager
Manage the communication between the application and the driver, load and uninstall the driver according to the application,
Process odbc function calls and send them to the driver.

Driver
Process odbc function calls, submit SQL statements to the data source, and return results to the application. If necessary,
The driver modifies an application request to make the request consistent with the syntax supported by the relevant DBMS.

Data Source
DBMS to be accessed by users and related OS

Application
|
|
Driver Manager
|
|
Driver Driver
|
Data Source

Database Connection Pool Overview:
Database connection is a key, limited, and expensive resource, which is particularly prominent in the Web applications of multiple users. Database connection management can significantly affect the scalability and robustness of the entire application, and affect the performance indicators of the program. The database connection pool is designed to address this issue.
The database connection pool is responsible for allocating, managing, and releasing database connections. It allows applications to repeat an existing database connection instead of reestablishing one; release the database connection with idle time exceeding the maximum idle time to avoid database connection omissions caused by the absence of the database connection. This technology can significantly improve the performance of database operations.
During database connection pool initialization, a certain number of database connections are created in the connection pool. The number of connections to these databases is set by the minimum number of database connections. Whether or not these database connections are used, the connection pool will always have at least so many connections. The maximum number of database connections in the connection pool limits the maximum number of connections that the connection pool can occupy. When the number of connections requested by the application to the connection pool exceeds the maximum number of connections, these requests will be added to the waiting queue. The following factors must be taken into account when setting the minimum number of connections and maximum number of connections in the database connection pool:
1) The minimum number of connections is the database connection that has been maintained by the connection pool. Therefore, if the application uses a small amount of database connections, a large amount of database connection resources will be wasted;
2) the maximum number of connections is the maximum number of connections that can be applied by the connection pool. If the number of database connection requests exceeds this limit, the subsequent database connection requests will be added to the waiting queue, which will affect subsequent database operations.
3) if the difference between the minimum number of connections and the maximum number of connections is too large, the first connection request will be profitable. A connection request that exceeds the minimum number of connections is equivalent to creating a new database connection. However, these connections that are larger than the minimum number of connections will not be released immediately after use. They will be placed in the connection pool and will be released after repeated use or idle timeout .... Remaining full text>

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.