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:
XML Version = '1.0' Encoding = 'UTF-8'?>
factory name> com.ibm.db2.jcc.db2simpleDataSource value> -> org.apache.commons.dbcp.basicDataSourceFactory value> parameter> driverclassname name> com.ibm.db2.jdbc.net.db2driver value> parameter> URL name> jdbc: db2: // localhost: 6789 / alisi value > Parameter> Username name> Hain value> parameter> password name> bi2003 value> parameter > maxactive name> 20 value> parameter> maxidle name> 10 value> parameter> maxwait name> -1 value> parameter> resourceParams> context> 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 name> com.ibm.db2.jcc.db2simpleDataSource value> -> org.apache.commons.dbcp.basicDataSourceFactory value> parameter> driverclassname name> com.ibm.db2.jdbc.app.db2driver value> -> com.ibm.db2.jdbc.net.db2driver value> parameter> URL name> jdbc: db2: text value> -> jdbc: db2: // localhost: 6789 / alisi value> parameter> < Parameter> Username name> ha value> parameter> password name> 2005 value> parameter> maxactive name> 20 value> parameter> maxidle name> 10 value> parameter> maxwait Name> -1 value> parameter> resourceParams> context> 2 Note that you should join (not required) in each web application's web.xml file DB2 DataSource Example Description> JDBC / MYDB res-ref-name> javax.sql.datasource res-type> Container res-auth> resource-ref> 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 name> 20 value> parameter> maxidle name> 10 value> parameter> maxwait name> -1 value> parameter> 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.