Apache Tomcat 에서 Connection Pool 의 정보를 얻는 방법
1. org.apache.tomcat.dbcp.dbcp2.BasicDataSource 에 대해서
org.apache.tomcat.dbcp.dbcp2.BasicDataSource는
(javax.sql.DataSource 와 비교해서)
더 많은 메소드들을 제공하고 있고,
그 중에서는 사용중(active)이거나 대기중(idle)인 연결의 갯수와 같이
Connection Pool 의 사용현황에 관한 것들도 있다.
| String source = "java:/comp/env/jdbc/graha"; |
| javax.naming.InitialContext cxt = new javax.naming.InitialContext(); |
| BasicDataSource ds = (BasicDataSource)cxt.lookup(source); |
| ds.getNumActive(); |
| ds.getNumIdle(); |
| ds.getMinIdle(); |
| ds.getMaxIdle(); |
| ds.getMaxTotal(); |
2. 여러개의 Connection Pool 현황을 조회하는 jsp 소스 코드
| <% |
| String[] jndis = new String[] { |
| "jdbc/graha", |
| "jdbc/graha_sample" |
| }; |
| %> |
| <% |
| if(jndis != null && jndis.length > 0) { |
| %> |
| <style> |
| table.datasource td { |
| text-align:center; |
| height:25px; |
| } |
| table.datasource td.jndi { |
| text-align:left; |
| } |
| table.datasource td.total { |
| font-weight:bold; |
| } |
| </style> |
| <table class="datasource"> |
| <tr> |
| <th class="jndi">JNDI</th> |
| <th class="NumActive">Num Active</th> |
| <th class="NumIdle">Num Idle</th> |
| <th class="MinIdle">Min Idle</th> |
| <th class="MaxIdle">Max Idle</th> |
| <th class="MaxTotal">Max Total</th> |
| <th class="MaxOpenPreparedStatements">Max Open Prepared Statements</th> |
| </tr> |
| <% |
| int numActive = 0; |
| int numIdle = 0; |
| javax.naming.InitialContext cxt = new javax.naming.InitialContext(); |
| org.apache.tomcat.dbcp.dbcp2.BasicDataSource ds = null; |
| for(int i = 0; i < jndis.length; i++) { |
| String source = null; |
| if(jndis[i].startsWith("java:")) { |
| source = jndis[i]; |
| } else { |
| source = "java:/comp/env/" + jndis[i]; |
| } |
| try { |
| ds = (org.apache.tomcat.dbcp.dbcp2.BasicDataSource)cxt.lookup(source); |
| } catch (javax.naming.NamingException e) { |
| ds = null; |
| } |
| if(ds == null) { |
| %> |
| <tr> |
| <td colspan="7">Error : <%=jndis[i]%></td> |
| </tr> |
| |
| <% |
| } else { |
| numActive += ds.getNumActive(); |
| numIdle += ds.getNumIdle(); |
| %> |
| <tr> |
| <td class="jndi"><%=jndis[i]%></td> |
| <td class="NumActive"><%=ds.getNumActive()%></td> |
| <td class="NumIdle"><%=ds.getNumIdle()%></td> |
| <td class="MinIdle"><%=ds.getMinIdle()%></td> |
| <td class="MaxIdle"><%=ds.getMaxIdle()%></td> |
| <td class="MaxTotal"><%=ds.getMaxTotal()%></td> |
| <td class="MaxOpenPreparedStatements"><%=ds.getMaxOpenPreparedStatements()%></td> |
| </tr> |
| <% |
| } |
| ds = null; |
| } |
| cxt.close(); |
| cxt = null; |
| %> |
| <tr> |
| <td class="jndi total">Total</td> |
| <td class="NumActive total"><%=numActive%></td> |
| <td class="NumIdle total"><%=numIdle%></td> |
| <td class="MinIdle total"></td> |
| <td class="MaxIdle total"></td> |
| <td class="MaxTotal total"></td> |
| <td class="MaxOpenPreparedStatements total"></td> |
| </tr> |
| </table> |
| <% |
| } |
| %> |