Let's find out what a database connection pool is.
The idea of database connection pooling is very simple, storing database connections as objects in a vector object, and once the database connection is established, the connections can be shared by different database access requests, which can be overcome by reusing these established database connections, Greatly saves system resources and time.
In the actual application development, especially in the Web application system, if the JSP, servlet or EJB use JDBC directly to access the data in the database, each data access request must undergo the steps of establishing the database connection, opening the database, accessing the data and shutting down the database connection, The connection and open the database is a resource-consuming and time-consuming work, if the frequent occurrence of such database operations, the system's performance will inevitably drop dramatically, and even lead to system crashes. Database connection pool technology is the most common way to solve this problem, in many application servers (such as: Weblogic,websphere,jboss), the basic provision of this technology, do not have to program themselves, but it is necessary to understand this technology.
The main operations of the database connection pool are as follows:
(1) Establish the Database connection pool object (server startup).
(2) Create the initial number of database connections (that is, the number of idle connections) according to the parameters specified beforehand.
(3) A connection is obtained directly from the connection pool for a database access request. Create a new database connection if there are no idle connections in the database connection pool object and the number of connections does not reach the maximum (that is, the maximum number of active connections).
(4) Access to the database.
(5) Close the database and release all database connections (at which point the shutdown database connection is not actually closed, but put into the idle queue.) If the actual number of idle connections is greater than the initial idle connection number, the connection is released.
(6) Release the database connection pool object (the server stops, maintains, releases the database connection pool object, and releases all connections).
Configuration steps:
1. Add the following in the <Context> node of Tomcat Conf/content.xml
<resource name= "Jdbc/news" auth= "Container" type= "Javax.sql.DataSource" maxactive= "maxidle=" "maxwait=" 10000 "username=" sa "password=" 120010 "driverclassname=" Com.microsoft.sqlserver.jdbc.SQLServerDriver "url=" JDBC: Sqlserver://localhost:1433;databasename=news "/>
Name: We're going to call resource when we use the
Type: Is the class under the Java expansion package used in the linked database
Maxactive: Maximum number of connections
Maxidle: The maximum number of connections in idle time
Maxwait: Idle judgment Standard
Attention:
1. Chinese is not allowed in the configuration file, separated by a space between the attributes
2. To put the corresponding linked database jar package into the Tomcat/lib folder, the project does not need to be stored
The following are also links to Oracle
<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= "auth=" "Container"/>
2. Add the following code to the web.xml 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>
The name in the <res-ref-name> tag is the name in the Content.xml file.
3. Get the Connection object in the database connection pool
Context Context=new InitialContext ();
Datasourceds= (DataSource) context.lookup ("Java:comp/env/jdbc/news");
Connectionconn = Ds.getconnection ();
The context is a class under JAVAX.NAMINGX
DataSource is a class under Javax.sql.
"Java:comp/env/jdbc/news": java:comp/env is a fixed notation jdbc/news is the Jndi name (the name that is configured in Tomcat)
If we are not configuring the data source in Web.xml, we use spring to control the database link method steps:
1. Add the following node to the configuration file
<bean id= "DataSource" class= "Org.springframework.jndi.JndiObjectFactoryBean" >
<property name= "Jndiname" value= "Java:comp/env/jdbc/news"/>
</bean>
Configured to get the SQL statement query data how many articles:
The direct DAO class inherits the Jdbcdaosupport class (class in Spring.jar) when invoked
int count = This.getjdbctemplate (). queryForInt ("SELECT * from users");
So we're done! Of course, there are many ways to configure, I only listed several, if there are any problems can be private I am happy to discuss with you!
----Ydcun