Java connection pool technology
How to Use JNDI (Java Naming and Directory Interface) is an application Interface that provides developers with a universal and unified Interface for searching and accessing various Naming and Directory services, JDBC is built on the abstraction layer. JNDI provides a unified method for searching and accessing services on the network. By specifying a resource name, the name corresponds to one record in the database or naming service, and returns the information required for database connection establishment.
In actual development, we sometimes use the database connection pool provided by the server. For example, we hope that the Tomcat server can help us create a database connection pool at startup, in this case, you do not need to manually create a database connection pool in the application. You can directly use the database connection pool created by the Tomcat server. To create a database connection pool when the Tomcat server is restarted, You need to configure the Tomcat server.
The configuration process is as follows:
(1) Place the database Driver (jar package) in the lib folder under the Tomcat installation directory.
(2) manually create a context. xml file under the WebContent \ META-INF \ directory of the project, edit the content, the final content is as follows:
DriverClassName = "com. microsoft. sqlserver. jdbc. SQLServerDriver"
Url = "jdbc: sqlserver: // localhost: 1433; DatabaseName = test"/>
(3) then manually create a web. xml file under the WebContent \ META-INF \ directory above, edit the content, the final content is as follows:
DB Connection
Jdbc/test
Javax. SQL. DataSource
Container
(3) create a jsp page to test database access. The Code is as follows:
<% @ Page language = "java" contentType = "text/html; charset = UTF-8"
PageEncoding = "UTF-8" %>
<% @ Page import = "java. SQL. *, javax. SQL. *, javax. naming. *, com. microsoft. *" %>
Database connection test
<%
Connection con = null;
Try {
Context ctx = new InitialContext ();
DataSource ds = (DataSource) ctx. lookup ("java: comp/env/jdbc/test ");
Con = ds. getConnection ();
System. out. println ("connection pool connectioned !! ");
Con. close ();
} Catch (NamingException e ){
System. out. println (e. getMessage ());
} Catch (SQLException e ){
E. printStackTrace ();
} Finally {
Con. close ();
}
%>
So far, the database connection pool has been created.