| 1 |
<?xml version="1.0"?> |
| 2 |
<!-- |
| 3 |
Licensed to the Apache Software Foundation (ASF) under one or more |
| 4 |
contributor license agreements. See the NOTICE file distributed with |
| 5 |
this work for additional information regarding copyright ownership. |
| 6 |
The ASF licenses this file to You under the Apache License, Version 2.0 |
| 7 |
(the "License"); you may not use this file except in compliance with |
| 8 |
the License. You may obtain a copy of the License at |
| 9 |
|
| 10 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
|
| 12 |
Unless required by applicable law or agreed to in writing, software |
| 13 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
See the License for the specific language governing permissions and |
| 16 |
limitations under the License. |
| 17 |
--> |
| 18 |
<jsp:root xmlns="http://java.sun.com/JSP/Page" version="1.2"> |
| 19 |
<jsp:directive.page contentType="text/html"/> |
| 20 |
<jsp:directive.page import="java.io.*"/> |
| 21 |
<jsp:directive.page import="java.sql.*"/> |
| 22 |
<jsp:directive.page import="javax.sql.*"/> |
| 23 |
<jsp:directive.page import="javax.naming.*"/> |
| 24 |
|
| 25 |
<!-- |
| 26 |
This jsp is used to test the abandoned connection feature of BasicDataSource. |
| 27 |
Add the following configuration to server.xml and load the jsp page. |
| 28 |
The connections aren't closed and are logged to stdout/stderr. |
| 29 |
|
| 30 |
<Resource name="jdbc/abandoned" auth="Container" type="javax.sql.DataSource"/> |
| 31 |
<ResourceParams name="jdbc/abandoned"> |
| 32 |
<parameter><name>username</name><value>sa</value></parameter> |
| 33 |
<parameter><name>password</name><value></value></parameter> |
| 34 |
<parameter><name>driverClassName</name><value>org.hsqldb.jdbcDriver</value></parameter> |
| 35 |
<parameter><name>url</name><value>jdbc:hsqldb:database</value></parameter> |
| 36 |
|
| 37 |
<parameter><name>removeAbandoned</name><value>true</value></parameter> |
| 38 |
<parameter><name>removeAbandonedTimeout</name><value>0</value></parameter> |
| 39 |
<parameter><name>logAbandoned</name><value>true</value></parameter> |
| 40 |
|
| 41 |
<parameter><name>maxActive</name><value>1</value></parameter> |
| 42 |
</ResourceParams> |
| 43 |
--> |
| 44 |
|
| 45 |
<jsp:declaration> |
| 46 |
<![CDATA[ |
| 47 |
public DataSource getDataSource(JspWriter out) throws Exception { |
| 48 |
Context ctx = null; |
| 49 |
|
| 50 |
try { |
| 51 |
ctx = new InitialContext(); |
| 52 |
} catch (NamingException e) { |
| 53 |
out.println("<br/>Couldn't build an initial context : " + e); |
| 54 |
return null; |
| 55 |
} |
| 56 |
|
| 57 |
try { |
| 58 |
Object value = ctx.lookup("java:/comp/env/jdbc/abandoned"); |
| 59 |
out.println("<br/>DataSource lookup"); |
| 60 |
out.println("<br/>jdbc value : " + value); |
| 61 |
out.println("<br/>jdbc class : " + value.getClass().getName()); |
| 62 |
out.println("<hr/>"); |
| 63 |
|
| 64 |
if (value instanceof DataSource) { |
| 65 |
return (DataSource) value; |
| 66 |
} |
| 67 |
else { |
| 68 |
return null; |
| 69 |
} |
| 70 |
|
| 71 |
} catch (NamingException e) { |
| 72 |
out.println("<br/>JNDI lookup failed : " + e); |
| 73 |
return null; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
private void getConnection1(DataSource ds, JspWriter out) throws Exception { |
| 78 |
System.err.println("BEGIN getConnection1()"); |
| 79 |
out.println("<br/>BEGIN getConnection1()"); |
| 80 |
|
| 81 |
Connection conn = ds.getConnection(); |
| 82 |
System.err.println("conn: " + conn); |
| 83 |
out.println("<br/>conn: " + conn); |
| 84 |
|
| 85 |
System.err.println("END getConnection1()"); |
| 86 |
out.println("<br/>END getConnection1()"); |
| 87 |
} |
| 88 |
|
| 89 |
private void getConnection2(DataSource ds, JspWriter out) throws Exception { |
| 90 |
System.err.println("BEGIN getConnection2()"); |
| 91 |
out.println("<br/>BEGIN getConnection2()"); |
| 92 |
|
| 93 |
Connection conn = ds.getConnection(); |
| 94 |
System.err.println("conn: " + conn); |
| 95 |
out.println("<br/>conn: " + conn); |
| 96 |
|
| 97 |
System.err.println("END getConnection2()"); |
| 98 |
out.println("<br/>END getConnection2()"); |
| 99 |
} |
| 100 |
|
| 101 |
private void getConnection3(DataSource ds, JspWriter out) throws Exception { |
| 102 |
System.err.println("BEGIN getConnection3()"); |
| 103 |
out.println("<br/>BEGIN getConnection3()"); |
| 104 |
|
| 105 |
Connection conn = ds.getConnection(); |
| 106 |
System.err.println("conn: " + conn); |
| 107 |
out.println("<br/>conn: " + conn); |
| 108 |
|
| 109 |
System.err.println("END getConnection3()"); |
| 110 |
out.println("<br/>END getConnection3()"); |
| 111 |
} |
| 112 |
]]> |
| 113 |
</jsp:declaration> |
| 114 |
|
| 115 |
<html> |
| 116 |
<head> |
| 117 |
<title>DBCP Abandoned Connection Test</title> |
| 118 |
</head> |
| 119 |
<body> |
| 120 |
<h1>DBCP Abandoned Connection Test</h1> |
| 121 |
<hr/> |
| 122 |
<jsp:scriptlet> |
| 123 |
DataSource ds = getDataSource(out); |
| 124 |
if (ds != null) { |
| 125 |
getConnection1(ds, out); |
| 126 |
getConnection2(ds, out); |
| 127 |
getConnection3(ds, out); |
| 128 |
} |
| 129 |
</jsp:scriptlet> |
| 130 |
<hr/> |
| 131 |
<br/> |
| 132 |
OK |
| 133 |
</body> |
| 134 |
</html> |
| 135 |
</jsp:root> |