Steps for configuring the database connection pool in java

Source: Internet
Author: User

What is a database connection pool?
The idea of database connection pool technology is very simple. database connections are stored as objects in a Vector object. Once a database connection is established, different database access requests can share these connections, by reusing these established database connections, you can overcome these shortcomings and greatly save system resources and time.

In actual application development, especially in WEB application systems, If JSP, Servlet, or EJB uses JDBC to directly access data in the database, every data access request must go through the steps of establishing a database connection, opening a database, accessing data, and closing a database connection. Connecting to and opening a database is a resource-consuming and time-consuming task, if such database operations occur frequently, the system performance will inevitably drop sharply, and even cause the system to crash. The database connection pool technology is the most commonly used method to solve this problem. In many application servers (such as Weblogic, WebSphere, and JBoss), this technology is basically provided and does not need to be programmed by yourself, however, it is necessary to gain an in-depth understanding of this technology.

The main operations of the database connection pool are as follows:
(1) Create a database connection pool object (server startup ).
(2) create an initial number of database connections (idle connections) based on the specified parameters ).
(3) For a database access request, a connection is directly obtained from the connection pool. If there is no idle connection in the database connection pool object and the number of connections does not reach the maximum (that is, the maximum number of active connections), create a new database connection.
(4) access the database.
(5) Close the database and release all database connections (close the database connection at this time, rather than actually close it, but put it into the idle queue. If the actual number of idle connections is greater than the initial number of idle connections, the connection is released ).
(6) release the database connection pool object (during server stop and maintenance, release the database connection pool object and release all connections ).
Configuration steps:
1. Add the following content to the <Context> node in Tomcat conf/content. xml:

<Resource name = "jdbc/news" auth = "Container" type = "javax. SQL. dataSource "maxActive =" 100 "maxIdle =" 30 "maxWait =" 10000 "username =" sa "password =" 120010 "driverClassName =" com. microsoft. sqlserver. jdbc. SQLServerDriver "url =" jdbc: sqlserver: // localhost: 1433; DatabaseName = news "/>

Name: It will be used to call the resource later.
Type: the class in the java extension package used to connect to the database.
MaxActive: Maximum number of connections
MaxIdle: the maximum number of connections when idle
Maxwait: idle judgment criteria
Note:
1. Chinese characters are not allowed in the configuration file. Each attribute is separated by a space.
2. You do not need to store the jar package of the corresponding connected database in the tomcat/lib folder.
The link to oracle is as follows:

<Resource type = "javax. SQL. dataSource "name =" jdbc/news "url =" jdbc: oracle: thin: @ 192.168.2.102: 1521: orcl "driverClassName =" oracle. jdbc. driver. oracleDriver "password =" bg "username =" test "maxWait =" 10000 "maxIdle =" 30 "maxActive =" 100 "auth =" Container "/>
2. Add the following code to the web. xml file of the project:
<Resource-ref>
<Res-ref-name> jdbc/news </res-ref-name>
<Res-type> javax. SQL. DataSource </res-type>
<Res-auth> Container </res-auth>
</Resource-ref>

Here, the name in the <res-ref-name> label is the name in the content. xml file.

3. Obtain the Connection object in the database Connection pool.

Context context = new InitialContext ();
Export CEDs = (DataSource) context. lookup ("java: comp/env/jdbc/news ");
Connectionconn = ds. getConnection ();

Context is the class under javax. namingx
DataSource is a class under javax. SQL
In "java: comp/env/jdbc/news": java: comp/env is a fixed method. jdbc/news is the jndi name (the name configured in Tomcat)

If we do not configure the data source in web. xml, we use spring to control the database link steps:
1. Add the following nodes to the configuration file:
<Bean id = "dataSource" class = "org. springframework. jndi. JndiObjectFactoryBean">
<Property name = "jndiName" value = "java: comp/env/jdbc/news"/>
</Bean>

After configuring the SQL statement query data:

The Dao class inherits the JdbcDaoSupport class (class in spring. jar)
Int count = this. getJdbcTemplate (). queryForInt ("select * from users ");

So we can get it done! Of course, there may be many other methods to configure. I will only list the above methods. If you have any questions, I will be happy to discuss them with you!
---- Ydcun

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.