Configure the data source and connection pool in Tomcat and tomcat Data Source
(1) Why do I need to configure the data source and connection pool?
We know that every time the java program connects to the database, we need to request a connection to the database, open the read data, and then close it,
In this way, each user needs to make a response to the server when accessing the server. In this way, the server is under great pressure and the efficiency will decrease,
To solve this problem, we can let the database open the connection in advance and wait for the user to connect. When there is a user connection, we can just send the existing connection of the database to the user.
This connection is called a "connection pool". When there are enough connections in the connection pool, the connection pool will be allocated to the user. If not, the user will be waiting in the "| queue pool;
(2) implementation process:
Method 1: Configure the server. xml file (Open server. xml and find the part of the code added to the Host)
<Host name = "localhost" appBase = "webapps" unpackWARs = "true" autoDeploy = "true"> <! -- SingleSignOn valve, share authentication between web applications Documentation at:/docs/config/valve.html --> <! -- <Valve className = "org. apache. catalina. authenticator. SingleSignOn"/> --> <! -- Access log processes all example. documentation at:/docs/config/valve.html Note: The pattern used is equivalent to using pattern = "common" --> <Valve className = "org. apache. catalina. valves. accessLogValve "directory =" logs "prefix =" localhost_access_log. "suffix = ". txt "pattern =" % h % l % u % t "% r" % s % B "/> <Context path =" "docBase =" lecheng "debug =" 0" reloadable = "true"/> <! -- Set the data source and connection pool --> <! -- Name: Set the name (jndi) auth for the Data source: indicates who manages the data source (the Container is tomcat) type: type --> <Resource name = "xuliugen" auth = "Container" type = "javax. SQL. response se "/> <ResourceParams name =" xuliugen "> <! -- Factory settings --> <parameter> <name> factory </name> <value> org. apache. commons. dbcp. basicperformancefactory </value> </parameter> <! -- Driver settings. Put the driver in tomcat's common/lib or the site's own lib --> <parameter> <name> driverClassName </name> <value> com. microsoft. jdbc. sqlserver. SQLServerDriver </value> <! -- The content inside is the driver of your own database --> </parameter> <! -- Set url --> <parameter> <name> url </name> <value> jdbc: microsoft. sqlserver: // 127.0.0.1: 1443; DatabaseName = spdb </value> </parameter> <! -- Set the username for database connection --> <parameter> <name> username </name> <value> root </value> </parameter> <! -- Set the database connection password --> <parameter> <name> password </name> <value> root </value> </parameter> <! -- Maximum number of activated connections in the connection pool --> <parameter> <name> maxActive </name> <value> 200 </value> </parameter> <! -- Maximum number of reserved (Space) connections in the connection pool --> <parameter> <name> maxIdle </name> <value> 10 </value> </parameter> <! -- Maximum wait time in the connection pool, calculate by second --> <parameter> <name> maxWait </name> <value>-1 </value> </parameter> </ResourceParams> </Context> </Host>
The following code is used in the database connection class:
Package com. tsinghua; import java. SQL. *; import javax. SQL. *; import javax. naming. *; public class ConnDB {private Connection ct = null; public Connection getConn () {// use the data source connection pool to connect to the database try {// create a Context con = new javax. naming. initialContext (); // obtain the data source DataSource ds = (DataSource) con through con. lookup ("java: comp/env/xuliugen"); // The "xuliugen" here is the name of your data source ct = ds. getConnection (); System. out. println ("connection pool method");} catch (Exception ex) {ex. printStackTrace () ;}return ct ;}}
The following figure shows the effect in the background:
Method 2: log on to the administration tool page in tomcat5.0.
How can I configure Tomcat data sources and connection pools?
What is the original data configuration of different TOMCAT versions? Are you using Tomcat 6 or Tomcat 5?
I'll give it to you. My name is atat6.0.
(1) Step 1: add the database connection information to the tomcat configuration file (context. xml ).
<Resource name = "jdbc/dataBase1" auth = "Container" type = "javax. SQL. DataSource"
DriverClassName = "oracle. jdbc. driver. OracleDriver" // your database driver name;
Url = "jdbc: oracle: thin: @ 192.168.20.90: 1521: carddb" Your Database URL Connection;
Username = "card" // User Name
Password = "password" // password
MaxActive = "20" // The maximum number of database connections in the connection pool. If it is set to 0, there is no limit.
MaxIdle = "10 "//
MaxWait = "10000" type = "codeph" text = "/codeph"/>
(2) Configure reference in web. xml
<Resource-ref>
<Description> DB Connection </description>
<Res-ref-name> jdbc/dataBase1 </res-ref-name>
<Res-type> javax. SQL. DataSource </res-type>
<Res-auth> Container </res-auth>
</Resource-ref>
(3) Part 3 test code
Test code:
Import javax. naming. Context;
Import javax. naming. InitialContext;
Import javax. naming. NamingException;
Import javax. SQL. DataSource;
Public class DataSourceTest {
Public static Connection getCon (){
Connection conn = null;
Try {
Context ctx = new InitialContext ();
DataSource ds = (DataSource) ctx. lookup ("java:/comp/env/jdbc/dataBase1 ");
Conn = ds. getConnection ();
} Catch (NamingException e ){
E. printStackTrace ();
} Catch (SQLException e ){
E. printStackTrace ();
}
Return conn;
}
}... Remaining full text>
How does MySql use Tomcat to configure the data source in the database connection pool?
Configuration in server. xml
<Context docBase = "XXX" path = "/XXX" reloadable = "true" source = "com. ibm. wtp. web. server: XXX">
<Resource auth = "Container"
DriverClassName = "com. mysql. jdbc. Driver" maxActive = "10"
MaxIdle = "10" maxWait = "-1"
Name = "jdbc/ifcp" password = "123"
Type = "javax. SQL. DataSource" url = "jdbc: mysql: // localhost: 3306/XXX? CharactorEncoding = utf8"
Username = "root"
RemoveAbondoned = "true"
/>
</Context>
XXX is your project path.
Web. xml does not seem to require special configuration. You can add the following
<Resource-ref>
<Description> Mysql Datasource example </description>
<Res-ref-name> jdbc/ifcp </res-ref-name>
<Res-type> javax. SQL. DataSource </res-type>
<Res-auth> Container </res-auth>
<Res-sharing-scope> unretriable </res-sharing-scope>
</Resource-ref>