1. JNDI:
JNDI is ( Java Naming and Directory Inteface)Java Name directory interface.
The role of JNDI is to bring resources into the server.You can think of JNDI as a repository.Put Java objects into JNDI.
2. Origin of data sources:
In java development, use JDBC operations data base Several steps:
1. Use Class.forName (full path name of the class): to load the database driver.
2. Get the Connection connection object for the database.DriverManager.getConnection().
3. Operating databases: querying databases or updating database contents,
4. Close the database connection: use the close method.
Note: There are four steps to get one database connection at a time, but among them, [1], [2], [4] are common operations for all databases, and only [3] are different steps for operating databases.It takes time to get the connection object of the database and to close the connection of the database.Causes poor performance.
If we have already created many connection objects in the beginning and put them in a public place, when there is a request to connect to the database, we will pull a connection from this public place, operate the database, complete the database operation, not close the connection, but put it in the public warehouse, which will bring up the thing of the database connection pool, that is, store moreConnection object.
3. Configure the connection pool of a database using JNDI in two ways: (global JNDI configuration and non-global JNDI configuration)
(1) Non-global JNDI configuration: he only configures data sources for a Web project
1. Import the jar package file to link the database.
For example, sqlserver import: sqljdbc4.jar package
Oracle Import: ojdbc14.jar package
MySQL Import: mysql -connector-java-5.0.8.jar package
2. Configure the connection pool for the database in JNDI:
Create a context.xml file in META-INF in the WEB project.Connection pool information for setting up a database
3. Configure in the web.xml file (optional or nonexistent): if any:
4. If Spring is used: in the applicationContext.xml or in your own Spring configuration file: import the JNDI configuration information
//Get connection pool information for the JNDI-configured database in content.xml.jndi-name must be the same as the name value in JNDI
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/sqlserver" />
//Use JdbcTemplate to operate the database.
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
Use IOC to get a jdbcTemplate object in a java file.Finally, use JdbcTemplate to manipulate the database
5. If not used spring In a java file:
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env"); //Fixed, no modification required
DataSource ds = (DataSource)envContext.lookup(jdbc/sqlserver);
(2) Global JNDI:
1. Add under the GlobalNamingResources tag in the server.xml file under conf in the tomcat installation directory (a partial JNDI resource match in the context.xml file)
Set information).
2. Configuring in the context under META-INF in a Web project
//global s should have the same name as the server.xml name, and the name here indicates that the name is currently being used.
<ResourceLink name="jdbc/sqlserver" global="jdbc/sqlserver" type="javax.sql.DataSource"/>
3. The remaining JNDI is the same as the local JNDI, and 3, 4, 5 can be used directly, that is, the local 2 is replaced by the global [1], [2].