Use JNDI to configure the Tomcat data source [improve database access efficiency]

Source: Internet
Author: User
Principle: multiple database connections are established in datasource and saved in the database connection pool. When a program accesses the database, it only needs to take idle database connections from the connection pool. The access ends, the resources are destroyed, and the database connection returns to the connection pool again, this saves a lot of time and resources compared to accessing the database directly each time. Well, it feels good ~
JNDI (Java Naming and Directory Interface) is a standard extension of the Java platform. It provides a set of interfaces, classes, and namespaces. Like many other Java technologies, jdni is a provider-based technology that exposes an API and a service supply interface (SPI ). This means that any name-based technology can provide services through JNDI, As long as JNDI supports this technology. Currently, the technology supported by JNDI includes LDAP, CORBA Common Object Service (COS) Name Service, RMI, NDS, DNS, and Windows registry. Many J2EE technologies, including EJB, rely on JNDI to organize and locate entities.
Oh ~ The concept of JNDI is long ~ Haha, friends can think of it as a technology that binds objects and names. The object factory is responsible for producing objects that are tied to a unique name, an external resource can be referenced by an object by its name.

The context interface is provided in the javax. Naming package, providing two useful methods:
<1> void BIND (string name, object)
Bind the name to the object. All intermediate context and target context (specified by all components other than the final atomic component of the name) must exist. PS: The name cannot be blank ~
<2> Object Lookup (string name)
Retrieves the specified object. If the name is blank, a new instance of the context is returned (the instance indicates the same naming context as the context, but its environment can be modified independently and can be accessed concurrently ).

Engineering Drawing in the external resource access object Factory:

Example:
====================== Add the following code snippet to the server. in XML,

<! -- Configure datasource. Add the following code into server. xml -->

<Context Path = "/Bookstore" docbase = "Bookstore" DEBUG = "0"
Reloadable = "true">

<! -- Data source name -->
<Resource Name = "JDBC/bookdb"
Auth = "Container"
Type = "javax. SQL. datasource"/>

<Resourceparams name = "JDBC/bookdb">
<Parameter>
<Name> factory </Name>
<Value> org. Apache. commons. DBCP. basicperformancefactory </value>
</Parameter>

<! -- Maximum number of DB connections in pool. Make sure you
Configure your mysqld max_connections large enough to handle
All of your DB connections. set to 0 for no limit.
-->

<! -- Maximum number of active connections -->
<Parameter>
<Name> maxactive </Name>
<Value> 100 </value>
</Parameter>

<! -- Maximum number of idle dB connections to retain in pool.
Set to 0 for no limit.
-->

<! -- Maximum number of idle database connections -->
<Parameter>
<Name> maxidle </Name>
<Value> 30 </value>
</Parameter>

<! -- Maximum time to wait for a DB connection to become available
In MS, in this example 10 seconds. An exception is thrown if
This timeout is exceeded. Set to-1 to wait indefinitely.
Maximum time to wait for a DB connection to become available
In MS, in this example 10 seconds. An exception is thrown if
This timeout is exceeded. Set to-1 to wait indefinitely.
-->

<! -- Maximum time for idle database -->
<Parameter>
<Name> maxwait </Name>
<Value> 10000 </value>
</Parameter>

<! -- MySQL db username and password for DB connections -->

<! -- Specify the username and password used to connect to the database -->
<Parameter>
<Name> username </Name>
<Value> dbuser </value>
</Parameter>
<Parameter>
<Name> password </Name>
<Value> 1234 </value>
</Parameter>

<! -- Class name for mm. MySQL JDBC driver -->

<! -- Specify the JDBC driver -->
<Parameter>
<Name> driverclassname </Name>
<Value> com. MySQL. JDBC. Driver </value>
</Parameter>

<! -- The JDBC connection URL for connecting to your MySQL db.
The autoreconnect = true argument to the URL makes sure that
Mm. MySQL JDBC driver will automatically reconnect if mysqld closed
Connection. mysqld by default closes idle connections after 8 hours.
-->

<! -- Specify the URL to connect to the database -->
<Parameter>
<Name> URL </Name>
<Value> JDBC: mysql: // localhost: 3306/bookdb? Autoreconnect = true </value>
</Parameter>
</Resourceparams>

</Context>

====================== Add the following code snippet to Web. xml ====================

<! -- Add the following code into <webapp> in Web. xml -->

<Resource-ref>
<Description> dB connection </description>

// Description of the referenced Resource
<Res-ref-Name> JDBC/bookdb </RES-ref-Name>
// Specify the JNDI name of the referenced resource, which corresponds to the name attribute in the <resource> element
<Res-type> javax. SQL. datasource </RES-type>
// Specify the Class Name of the referenced resource, which corresponds to the type attribute in the <resource> element
<Res-auth> container </RES-auth>
// Specify the manager for managing referenced resources, which corresponds to the auth attribute in the <resource> element.
</Resource-ref>

 

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.