Relationship between data sources and connection pools

Source: Internet
Author: User

Today, I have been strugglingData SourceAndConnection PoolWhat is the relationship between these two things? The reason is that the connection pool is clearly configured in spring, but the term is called its data source, and the data source is not configured.

Let's leave the spring configuration aside. Let's talk about the common data sources and connection pools on the market:

Data source:JDBC data source and JNDI Data Source, ODBC data sources, etc. (frequently used in simhei );

Connection Pool:C3p0 connection pool, DBCP connection pool, and porxool connection pool(Simhei is commonly used)


So what is the relationship between them?

Data SourceIs the source of the index data, such as the database.
Connection PoolIt refers to such a "pool". Everything in the pool is a "connection" connecting to the data source, so that when others want to connect to the data source, they can obtain from this "pool, once used up, it will be released for use by other users.


In the process of dealing with databases, each database operation requires a connection, which is a waste of resources. Therefore, the solution is to establish a connection pool. A certain number of connections are put into the connection pool during initialization. When necessary, the connection is retrieved from the pool. after use, the connection is put back into the pool.

Therefore, the connection pool is not necessary, but used to improve efficiency.

1. Use the JDBC data source (no connection pool)

Class.forName("com.mysql.jdbc.Driver");String url="jdbc:mysql://localhost:3306/test";Connection connection =  DriverManager.getConnection(url,"root" ,"123");

Therefore, we can see that without using the connection pool, we only need the above several lines of code to get the connection, but remember, this is a waste of resources.


2. Use the JNDI Data Source

The use of the JNDI data source must be placed in the WEB Project (regardless of the spring configuration), that is, the configuration of the server is required, which makes me explore for a long time.

Since it is going through the server, I am Tomcat and add the following to context. XML in conf:

 <Resource name="mydataSource" auth="Container" type="javax.sql.DataSource"                   url="jdbc:mysql://localhost:3306/test"                driverClassName="com.mysql.jdbc.Driver"                password="123"                username="root"                initialSize="2"                maxActive="3"                maxIdle="1"                minIdle="1"                maxWait="10000"                removeAbandoned="true"                logAbandoned="true"                removeAbandonedTimeout="60"                timeBetweenEvictionRunsMillis="900000"                minEvictableIdleTimeMillis="1800000"                numTestsPerEvictionRun="100"                validationQuery="select count(0) from dual"                poolPreparedStatements="true"                maxOpenPreparedStatements="100"/>

Explanation of the above Configuration

Database Problems: Username, password, driverclassname, URL

 JNDI Problems:Name, type, factory

Tomcat allows other connections, but the DBCP connection pool is used by default. That is, the factory is org. Apache. tomcat. DBCP. DBCP. basicdatasourcefactory by default.

Connection Control and connection Return Policy:Maxactive, maxidle, minidle, maxwait

To cope with network instability:Testonborrow, validationquery

Connection leakage policy:Removeabandoned, removeabandonedtimeout, logabandoned


Many materials on the Internet:When using the JNDI data source, you also need to go to the Tomcat server. XML and project web. add other things to XML. Baidu can be added to any website, but it is not required in practice.

The following code calls the JNDI Data source:

 Context it = new  InitialContext(); DataSource ds = (DataSource) it.lookup("java:comp/env/mydataSource"); Connection conn = ds.getConnection();

Note: The content in the lookup method Java: COMP/ENV/is fixed, followed by the specific datasource name


The next article will detail how spring configures the data source (connection pool)



 

This article is from the "bulajunjun" blog, please be sure to keep this source http://5148737.blog.51cto.com/5138737/1430384

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.