View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
10   */
11  package psiprobe.beans.accessors;
12  
13  import java.sql.SQLException;
14  import java.util.Properties;
15  
16  import oracle.jdbc.pool.OracleConnectionCacheManager;
17  import oracle.jdbc.pool.OracleDataSource;
18  
19  import psiprobe.UtilsBase;
20  import psiprobe.model.DataSourceInfo;
21  
22  /**
23   * Accesses oracle.jdbc.pool.OracleDataSource.
24   *
25   * <p>
26   * Oracle connection pool is quite different from any other available for Tomcat. Datasources are
27   * run by static OracleConnectionCacheManager, whereby application context scope datasource would
28   * have a named entry in OracleConnectionCacheManager.
29   * </p>
30   *
31   * <p>
32   * Datasources do not have information about pool as such, therefore this accessor has to do quite
33   * tedious job of verifying whether the datasource has a record in the cache manager or not. The
34   * pool information is subsequently retrieved from the relevant cache manager entry.
35   * </p>
36   *
37   * @deprecated As of 21c, this is deleted by oracle, therefore please transition to ucp which we
38   *             support now.
39   */
40  @Deprecated
41  public class OracleDatasourceAccessor implements DatasourceAccessor {
42  
43    @Override
44    public DataSourceInfo getInfo(Object resource) throws SQLException {
45      DataSourceInfo dataSourceInfo = null;
46  
47      if (canMap(resource)) {
48        OracleDataSource source = (OracleDataSource) resource;
49        OracleConnectionCacheManager occm =
50            OracleConnectionCacheManager.getConnectionCacheManagerInstance();
51        Properties cacheProperties = source.getConnectionCacheProperties();
52        String cacheName = source.getConnectionCacheName();
53        cacheName = cacheName != null && occm.existsCache(cacheName) ? cacheName : null;
54  
55        if (cacheProperties != null) {
56  
57          dataSourceInfo = new DataSourceInfo();
58          if (cacheName != null) {
59            dataSourceInfo.setBusyConnections(occm.getNumberOfActiveConnections(cacheName));
60            dataSourceInfo.setEstablishedConnections(occm.getNumberOfAvailableConnections(cacheName)
61                + dataSourceInfo.getBusyConnections());
62          } else {
63            dataSourceInfo.setBusyConnections(0);
64            dataSourceInfo.setEstablishedConnections(0);
65          }
66  
67          dataSourceInfo
68              .setMaxConnections(UtilsBase.toInt(cacheProperties.getProperty("MaxLimit"), -1));
69          dataSourceInfo.setJdbcUrl(source.getURL());
70          dataSourceInfo.setUsername(source.getUser());
71          dataSourceInfo.setResettable(true);
72          dataSourceInfo.setType("oracle-jdbc");
73        }
74      }
75      return dataSourceInfo;
76    }
77  
78    @Override
79    public boolean reset(Object resource) throws SQLException {
80      if (canMap(resource)) {
81        ((OracleDataSource) resource).close();
82        return true;
83      }
84      return false;
85    }
86  
87    @Override
88    public boolean canMap(Object resource) {
89      return "oracle.jdbc.pool.OracleDataSource".equals(resource.getClass().getName())
90          && resource instanceof OracleDataSource;
91    }
92  }