JNDI-j2ee
2015-08-10 23:30
197 查看
Database Connection Pool (DBCP) Configurations | |||||||||||||||||||||||||||||||||||||||||||||||||||
The default database connection pool implementation in Apache Tomcat relies on the libraries from the Apache Commons project. The following libraries are used: Commons DBCP Commons Pool These libraries are located in a single JAR at $CATALINA_HOME/lib/tomcat-dbcp.jar. However, only the classes needed for connection pooling have been included, and the packages have been renamed to avoid interfering with applications. DBCP 1.3 provides support for JDBC 3.0.
|
<?xml version="1.0" encoding="UTF-8"?> <Context reloadable="true"> <!-- maxActive: Maximum number of database connections in pool. Make sure you configure your mysqld max_connections large enough to handle all of your db connections. Set to -1 for no limit. --> <!-- maxIdle: Maximum number of idle database connections to retain in pool. Set to -1 for no limit. See also the DBCP documentation on this and the minEvictableIdleTimeMillis configuration parameter. --> <!-- maxWait: Maximum time to wait for a database connection to become available in ms, in this example 10 seconds. An Exception is thrown if this timeout is exceeded. Set to -1 to wait indefinitely. --> <!-- username and password: MySQL username and password for database connections --> <!-- driverClassName: Class name for the old mm.mysql JDBC driver is org.gjt.mm.mysql.Driver - we recommend using Connector/J though. Class name for the official MySQL Connector/J driver is com.mysql.jdbc.Driver. --> <!-- url: The JDBC connection url for connecting to your MySQL database. --> <Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="javauser" password="javadude" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/javatest" /> </Context>
3. web.xml configuration
Now create aWEB-INF/web.xmlfor this test application.
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <description>MySQL Test App</description> <resource-ref> <description>DB Connection</description> <res-ref-name>jdbc/TestDB</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> </web-app> | ||
4. Test code
Now create a simpletest.jsppage for use later.
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <sql:query var="rs" dataSource="jdbc/TestDB"> select id, foo, bar from testdata </sql:query> <html> <head> <title>DB(JNDI) Test</title> </head> <body> <h2>Results</h2> <c:forEach var="row" items="${rs.rows}"> Foo: ${row.foo}<br/> Bar: ${row.bar}<br/> </c:forEach> </body> </html>
That JSP page makes use of JSTL's SQL and Core taglibs. You can get it from Apache Tomcat Taglibs - Standard Tag Library project — just make sure you get a 1.1.x release. Once you have JSTL, copy
[b]jstl.jar[/b] and
standard.jart(
Standard 1.1 | JSTL 1.1 | Servlet 2.4, JavaServer Pages 2.0 | download |
WEB-INF/libdirectory.
Finally deploy your web app into
$CATALINA_BASE/webappseither as a warfile called
DBTest.waror into a sub-directory called
DBTest
Once deployed, point a browser at
http://localhost:8080/DBTest/test.jspto view the fruits of your hard work.
相关文章推荐
- Linux神器之Strace的实践(Ubuntu上服务幽灵般的消失)
- [2015hdu多校联赛补题]hdu5303 Delicious Apples
- 随机生成多维数组
- VMware克隆CentOS6.4后 eth0无法初始化/找不到eth0的问题
- PHP不同域名cookie共享(单点登录实现原理)
- 如何建立自己的个人网站
- 第81讲:Scala中List的构造和类型约束逆变、协变、下界详解
- openstack之cinder
- azkaban源码解读(二):观察者模式在job执行过程中事件监听应用
- java中的方法大全
- LR的响应时间与使用IE所感受时间不一致的讨论(摘抄补充)
- iOS多线程中performSelector: 和dispatch_time的不同
- 使用 mysqlbinlog恢复数据
- 遍历的四种方式
- H3C DHCP
- linux read 用法
- react native 开发IOS
- 使用insertBefore实现insertAdjacentHTML()
- mysql中char,varchar,text
- MINA经典入门例子----Time Server