2018.11.06 / 10:26
How to Monitor a Tomcat JDBC Connection pool from a Servlet Example
hanulbit
Ãßõ ¼ö 310
How to Monitor a Tomcat JDBC Connection pool from a Servlet Example
With this example you can find out the active, idle connections, etc. from a tomcat connection pool. This is very useful to know if you are not closing all your connections or if you want to double check how the pool is working.
MonitorServlet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import java.io.PrintWriter; |
import java.lang.management.ManagementFactory; |
import java.util.Set; |
import javax.management.MBeanAttributeInfo; |
import javax.management.MBeanInfo; |
import javax.management.MBeanServer; |
import javax.management.ObjectName; |
import javax.servlet.ServletException; |
import javax.servlet.annotation.WebServlet; |
import javax.servlet.http.HttpServlet; |
import javax.servlet.http.HttpServletRequest; |
import javax.servlet.http.HttpServletResponse; |
@WebServlet("/poolmonitor") |
public class MonitorServlet extends HttpServlet { |
private static final long serialVersionUID = 1L; |
@Override |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) |
throws ServletException, IOException { |
PrintWriter writer = resp.getWriter(); |
writer.println("<!DOCTYPE html>"); |
writer.println("<html>"); |
writer.println("<body>"); |
writer.println("<p><h1>Tomcat Pool</h1></p><p>"); |
try { |
MBeanServer server = ManagementFactory.getPlatformMBeanServer(); |
Set<ObjectName> objectNames = server.queryNames(null, null); |
for (ObjectName name : objectNames) { |
MBeanInfo info = server.getMBeanInfo(name); |
if (info.getClassName().equals( |
"org.apache.tomcat.jdbc.pool.jmx.ConnectionPool")) { |
for (MBeanAttributeInfo mf : info.getAttributes()) { |
Object attributeValue = server.getAttribute(name, |
mf.getName()); |
if (attributeValue != null) { |
writer.println("" + mf.getName() + " : " |
+ attributeValue.toString() + "<br/>"); |
} |
} |
break; |
} |
} |
} catch (Exception e) { |
// TODO Auto-generated catch block |
e.printStackTrace(); |
} |
writer.println("</p></body>"); |
writer.println("</html>"); |
} |
} |
Context.xml (Tomcat Pool example)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<Resource auth="Container" driverClassName="oracle.jdbc.driver.OracleDriver" |
maxActive="50" |
maxIdle="5" |
minIdle="1" |
maxWait="30000" |
validationQuery="select 1 from dual" |
validationQueryTimeout="3" |
testWhileIdle="true" |
testOnBorrow="true" |
testOnReturn="false" |
validationInterval="30000" |
timeBetweenEvictionRunsMillis="30000" |
removeAbandoned="true" |
removeAbandonedTimeout="600" |
name="jdbc/test" |
password="test" |
type="javax.sql.DataSource" |
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory" |
url="jdbc:oracle:thin:@myoracleexample:1521:MYORACLE" |
username="test" |
/> |
Notes
- This example read the tomcat pool MBeans which follow the JMX specification.
- You can also use JConsole to monitor all the JMX beans available.