nc9中国设计秀
</parameter>
nc9中国设计秀
</ResourceParams>
nc9中国设计秀
</Context>
nc9中国设计秀
下面是一些参数的说明:
nc9中国设计秀
<Context path="/quality" docBase="quality" debug="0" reloadable="true" crossContext="true">
nc9中国设计秀
其中:
nc9中国设计秀
1) path 指定路径,这里设定的是$CATALINA_HOME/webapps下的quality目录;
nc9中国设计秀
2) docBase 文件根目录。
nc9中国设计秀
3) reloader 当网页被更新时是否重新编译。
nc9中国设计秀
4) maxActive 连接池的最大数据库连接数。设为0表示无限制。
nc9中国设计秀
5) maxIdle 数据库连接的最大空闲时间。超过此空闲时间,数据库连接将被标记为不可用,然后被释放。设为0表示无限制。
nc9中国设计秀
6) maxWait 最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。
nc9中国设计秀
7) removeAbandoned 回收被遗弃的(一般是忘了释放的)数据库连接到连接池中。
nc9中国设计秀
8) removeAbandonedTimeout 数据库连接过多长时间不用将被视为被遗弃而收回连接池中。
nc9中国设计秀
9) logAbandoned 将被遗弃的数据库连接的回收记入日志。
nc9中国设计秀
10) driverClassName JDBC驱动程序。
nc9中国设计秀
11) url 数据库连接字符串
nc9中国设计秀
在$CATALINA_HOME/webapps/quality/WEB-INF/web.xml里设置被引用的资源:
nc9中国设计秀
下面是配置代码,必须放在<web-app>和</web-app>里。
nc9中国设计秀
<!-- Database Config start -->
nc9中国设计秀
<resource-ref>
nc9中国设计秀
<description>connectDB test</description>
nc9中国设计秀
<res-ref-name>jdbc/connectDB</res-ref-name>
nc9中国设计秀
<res-type>javax.sql.DataSource</res-type>
nc9中国设计秀
<res-auth>Container</res-auth>
nc9中国设计秀
</resource-ref>
nc9中国设计秀
<!-- Database Config end -->
nc9中国设计秀
下面是一下参数的必要说明:
nc9中国设计秀
1) description 对被引用的资源的描述。
nc9中国设计秀
2) res-ref-name 资源名称。见上面的<ResourceParams name="jdbc/connectDB">
nc9中国设计秀
3) res-type 资源类型。见上面的<Resource name="jdbc/connectDB" auth="Container" type="javax.sql.DataSource"/>
nc9中国设计秀
在JSP中使用资源:
nc9中国设计秀
这是在$CATALINA_HOME/webapps/quality下的某级子目录里的jsp网页文件部分代码:
nc9中国设计秀
<%@ page contentType="text/html;charset=GBK"%>
nc9中国设计秀
<%@ page errorPage="error.jsp"%>
nc9中国设计秀
<%@ page import="javax.naming.*"%>
nc9中国设计秀
<%@ page import="javax.sql.*"%>
nc9中国设计秀
<%@ page import="java.sql.*"%>
nc9中国设计秀
<html>
nc9中国设计秀
<head>
nc9中国设计秀
</head>
nc9中国设计秀
<body>
nc9中国设计秀
<%
nc9中国设计秀
………………nc9中国设计秀
………………nc9中国设计秀
// 数据库操作nc9中国设计秀
Context ctx=null;nc9中国设计秀
Connection cnn=null;nc9中国设计秀
Statement stmt=null;nc9中国设计秀
ResultSet rs=null;nc9中国设计秀
trync9中国设计秀
{nc9中国设计秀
ctx=new InitialContext();nc9中国设计秀
if(ctx==null)nc9中国设计秀
throw new Exception("没有匹配的环境");nc9中国设计秀
DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/connectDB");nc9中国设计秀
if(ds==null)nc9中国设计秀
throw new Exception("没有匹配数据库");nc9中国设计秀
nc9中国设计秀
cnn=ds.getConnection();nc9中国设计秀
stmt=cnn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);nc9中国设计秀
rs=stmt.executeQuery("select * from table1");nc9中国设计秀
………………nc9中国设计秀
………………nc9中国设计秀
}nc9中国设计秀
finallync9中国设计秀
{nc9中国设计秀
if(rs!=null)nc9中国设计秀
rs.close();nc9中国设计秀
if(stmt!=null)nc9中国设计秀
stmt.close();nc9中国设计秀
if(cnn!=null)nc9中国设计秀
cnn.close();nc9中国设计秀
if(ctx!=null)nc9中国设计秀
ctx.close();nc9中国设计秀
}nc9中国设计秀
%>nc9中国设计秀
</body>nc9中国设计秀
</html>nc9中国设计秀
代码说明:nc9中国设计秀
DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/connectDB");nc9中国设计秀
上面这句应用了刚才设的资源。nc9中国设计秀
资源使用完要释放,尤其是Context资源,见try-catch-finally部分的finally代码段,这是一种好的习惯。资源释放时如果释放了上级资源,下级资源将先被释放。如:释放了ctx,那么资源释放顺序将是rs,stmt,cnn,ctx。换句话说,如果释放了ctx,那么rs,stmt和cnn都将不可用了。nc9中国设计秀
这里的释放资源只是将数据库连接返回连接池中,并不是把资源真正释放掉,见数据库连接池概述。nc9中国设计秀
nc9中国设计秀