Data Connection Pool JNDI, connection jndi

Source: Internet
Author: User

Data Connection Pool JNDI, connection jndi

There are many methods for database connection. We have already introduced the JDBC database connection method before. In development, we often use DataBaseConnectionPool (database connection pool, DBCP ). What is the database connection pool? What is its advantage over jdbc database connection? How can it be used?


First, let's take a look at the JDBC connection. Every time a user accesses a database, JDBC needs to create a database connection for us. We use it and then close it. When we add a breakpoint test, we can find that the process of creating a database connection in this process is time-consuming, rather than data operation in the database. Therefore, the efficiency of JDBC is greatly reduced. In addition, if too many people access the database concurrently and the number of connections is too large, the performance will be reduced, and more seriously, the database may crash. However, the database connection pool solves these two problems.

 

2. What is the database connection pool?

As its name implies, it is a pool that stores connections from multiple databases! When we access the database, we can directly obtain the connection object from this pool, saving the initialization process and greatly improving the efficiency. In addition, this pool can control the number of database connections. We can put at least a few in it, and create again if it is not enough. We can create at most a few to control the number of connections. It is such a technology. Specifically, the connection pool is a cache technology because the pool is stored in the cache.


3. What are the advantages of the connection pool?

It actually solves the disadvantage of JDBC. The connection object is prepared in advance, which improves access efficiency, controls the number of connections, and prevents excessive user access at the same time, resulting in greatly reduced performance and database crashes.


4. How to Use DBCP?

Writing a database connection pool is troublesome, and you can also explain how it works. Therefore, we generally use the ready-made products. All kinds of servers are provided for us. Here we will first look at how to use the database connection pool of the Tomcat server.


1. Create a context in the catalina_home/webapps/webapp/meta-inf/directory of tomcat. xml file to compile DBCP, which is for this application (of course, it can also be found in tomcat's catalina_home/conf/context. write in xml, this is for this tomcat server), The write content is as follows:


[Html]View plaincopyprint?
  1. <? Xml version = "1.0" encoding = "GBK"?>
  2. <! -- This is the configuration file of the data connection pool, which can be defined only by name. -->
  3. <Context reloadable = "true">
  4. <Resource name = "jdbc/l.pdf"
  5. Auth = "Container"
  6. Type = "javax. SQL. DataSource"
  7. MaxActive = "100"
  8. MaxIdle = "30"
  9. MaxWait = "10000"
  10. Username = "lsung"
  11. Password = "lsung"
  12. DriverClassName = "oracle. jdbc. driver. OracleDriver"
  13. Url = "jdbc: oracle: thin: @ 192.168.1.27: 1521: L133"
  14. />
  15. </Context>



We can see from the name that all attributes of dbcp are being set, such as the maximum number of connections, the minimum number of connections, and the maximum wait time. After the setting, we can use it.


So how to use it? JNDI is involved here. JNDI is one of the J2EE13 specifications and is also very important. Here I will give a brief introduction to JNDI:

  • JNDI(Java Naming and Directory Interface)JavaNaming and Directory Service APIs
  • Reduces coupling between programs
  • Reducing device and device Coupling
  • The Tomcat server implements the JNDI service and starts the Tomcat server, which is equivalent to starting the JNDI server.
  • JNDI is a set of specifications developed by SUN, which can be compared with JDBC. JDBC is also a set of specifications. We can obtain data in the database by calling the JDBC interface. Similarly, we call the JNDI interface to obtain the "resource", which is not in the underlying database.
  • JNDI provides a service that binds the "name" and "resource". Then, programmers can call the lookup method for the jndi interface to find related resources.

 

To understand

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.