Tomcat4 + JDK + DB2 connection pool implementation

xiaoxiao2021-03-05  26

1 configuration data source

If we are going to deploy a web application, we put the app in the Tomcat_Home / WebApps directory, there are two ways to reference the JNDI data source we have configured.

A: You can create a new xxx.xml file under Tomcat_Home / CONF / CATALINA / LOCALHOST, XXX is the name of the released web application, such as the web application I posted is jnditest, .xml file named jnditest.xml in this file plus:

If the web application I have released is TEST, .xml file is named Test.xml in this file:

factory com.ibm.db2.jcc.db2simpleDataSource -> org.apache.commons.dbcp.basicDataSourceFactory driverclassname com.ibm.db2.jdbc.net.db2driver URL jdbc: db2: // localhost: 6789 / alisi Username Hain password bi2003 maxactive 20 maxidle 10 maxwait -1 where E: / java / tomcat / webapps / test is the directory of your publishing application, so You can test it. Doing this is that every web application must create a file like Test.xml. B To avoid the XXX.xml file, we can modify the server.xml file under Tomcat_Home / Conf, modified as follows:

Between:

factory com.ibm.db2.jcc.db2simpleDataSource -> org.apache.commons.dbcp.basicDataSourceFactory driverclassname com.ibm.db2.jdbc.app.db2driver -> com.ibm.db2.jdbc.net.db2driver URL jdbc: db2: text -> jdbc: db2: // localhost: 6789 / alisi < Parameter> Username ha password 2005 maxactive 20 maxidle 10 maxwait -1 2 Note that you should join (not required) in each web application's web.xml file DB2 DataSource Example JDBC / MYDB javax.sql.datasource Container 3 test code write index.jsp file:

<% @ Page ContentType = "Text / HTML; Charset = GBK"%> <% @ page import = "java.sql. *"%> <% @ Page Import = "javax.naming. *"%> <% @ Page Import = "javax.sql. *"%> Hi, <% try {context initctx = new initialcontext (); context ctx = (context) INITCTX.LOOKUP ("java: comp / ENV"); // Get the connection pool Object Object Obj = (Object) CTX.lookup ("JDBC / MyDB"); // Type Conversion Javax.sql.dataSource DS = (Javax.sql.datasource) Obj; connection conn = ds.getConnection (); statement stmt = Conn.createStatement (); string strsql = "select * from tb_area_table"; ResultSet RS = stmt.executeQuery (strsql); while (rs.next ()) {Out.println (rs.getstring (1)); out.println (Rs.getstring (2));}} Catch (Exception EX) {System.out.println ("Errr:" EX);}%>

Enter http://127.0.0.1:8080/test/ if the data can be displayed successfully

4 Configuration Description:

Resource item (ie the DataSource object of the connection pool), there are 3 attributes Name, Auth, Type, name entry is the name definition of JNDI, the program can find this object through JNDI, here, JDBC / MYDB; Auth item, connection pool management Right attribute, here the value Container, this is the type of container management; TYPE item is the type of object, this is the value javax.sql.datasource, and the database connection pool is applained in the next domain content contains four parameters User, Password, driverclassname, drivername, in turn serve the database username, password, JDBC driver, and database address. Username, password is to access the database's username and password for accessing the database. DriverclassName, Here is two values ​​com.ibm.db2.jdbc.net.db2driver and com.ibm.db2.jdbc.app.db2driver Next is the URL, ie the database of the address JDBC: DB2: // localhost: 6789 / MASA_239 And JDBC: DB2: TEXT

Filled here is the access address of DB2

There are three parameters, all of which are connected, as follows: maxactive 20 maxidle 10 maxwait -1 maxactive is the maximum number of connections, 20 here, indicating the most There are 20 connections to the database. MaxIdle is the maximum number of idle connections, which is 10 here, indicating that the 10 idle connection can be maintained even when there is no connection request, and is not cleared, at any time in standby. About the status of the object, interested friends can look at the EJB information. MaxWait is the maximum number of seconds, here is the value -1, indicating unlimited waits until the timeout can be taken, that is, 9000 times timeout. About MaxActive and Maxidle's suggestions, for enterprise applications, the values ​​of both are generally close, or the same, the key is to analyze the size of the application.

转载请注明原文地址:https://www.9cbs.com/read-32053.html

New Post(0)